C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 155 を表示中)
■90190 / )  Re[1]: メッセージボックスでの条件判断2
□投稿者/ 粗品 (9回)-(2019/02/15(Fri) 18:12:03)
No90181 (あんみつ さん) に返信
> これと同じことを "はい(Y)"を"A"に、"いいえ(N)"を"B"に、"キャンセル"を"C"に

以前試したものですが、メッセージボックスの文字のみ書き換えるようになります。
ググりながら作ったので細かなところまでは理解していないのですが、
メッセージボックスを表示する前にHookを実行しておくとボタンの文字列が変わります。

ただ、テキストが入れ替わるだけなので長い文字に変えてもボタンの幅は変わりませんし、
キャンセルボタンはあくまでもキャンセルボタンなのでEscキーに反応します。

"Aボタン(&A)","Bボタン(&B)","Cボタン(&C)"とかにしておけばキーボードのABCには反応します。

あとこれを呼び出す限りは文字列はこれで固定です。
変更したい場合は文字列を渡すとか改造できるのではないでしょうか。

なおMessageBoxEx.Hookを呼び出さなければ普通のメッセージボックスが出ます。



−−−−−−−

Public Class MessageBoxEx

    Private Shared m_hHook As Integer

    Private Const IDOK As Short = 1
    Private Const IDCANCEL As Short = 2
    Private Const IDABORT As Short = 3
    Private Const IDRETRY As Short = 4
    Private Const IDIGNORE As Short = 5
    Private Const IDYES As Short = 6
    Private Const IDNO As Short = 7

    Private Const MSGOK As String = "OK牧場"
    Private Const MSGCANCEL As String = "C" '"キャンセル"
    Private Const MSGABORT As String = "中止(&A)"
    Private Const MSGRETRY As String = "再試行(&R)"
    Private Const MSGIGNORE As String = "無視(&I)"
    Private Const MSGYES As String = "A" '"はい(&Y)"
    Private Const MSGNO As String = "B" '"いいえ(&N)"

    Private Const WH_CBT As Short = 5
    Private Const GWL_HINSTANCE As Short = (-6)
    Private Const HCBT_ACTIVATE As Short = 5

    Private Declare Function GetCurrentThreadId Lib "kernel32" () As Integer
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
    Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Integer, ByVal nIDDlgItem As Integer, ByVal lpString As String) As Integer
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As CallBack, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer

    Private Delegate Function CallBack(ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Shared Function MsgBoxHookProc(ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        If uMsg = HCBT_ACTIVATE Then
            SetDlgItemText(wParam, IDOK, MSGOK)
            SetDlgItemText(wParam, IDCANCEL, MSGCANCEL)
            SetDlgItemText(wParam, IDABORT, MSGABORT)
            SetDlgItemText(wParam, IDRETRY, MSGRETRY)
            SetDlgItemText(wParam, IDIGNORE, MSGIGNORE)
            SetDlgItemText(wParam, IDYES, MSGYES)
            SetDlgItemText(wParam, IDNO, MSGNO)

            UnhookWindowsHookEx(m_hHook)
        End If
        MsgBoxHookProc = 0
    End Function

    Public Shared Function Hook(ByRef hwndThreadOwner As Integer) As Integer
        Dim temp As CallBack
        Dim hInstance As Integer
        Dim hThreadId As Integer

        temp = AddressOf MsgBoxHookProc
        hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
        hThreadId = GetCurrentThreadId()

        m_hHook = SetWindowsHookEx(WH_CBT, temp, hInstance, hThreadId)

        Return m_hHook
    End Function

End Class

−−−−−−−

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim m_hHook As Integer = MessageBoxEx.Hook(Me.Handle.ToInt32)

    If m_hHook.Equals(0) Then
        MessageBox.Show("SetWindowsHookEx Failed")
        Exit Sub
    End If

    Dim result As DialogResult = MessageBox.Show("どれを選びますか?", "〇〇〇", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk)

    Console.WriteLine(result.ToString)
End Sub

返信 編集キー/


管理者用

- Child Tree -