Custom button captions in .NET messagebox?

后端 未结 8 1642
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 05:24

Is there an easy way to display a messagebox in VB.NET with custom button captions? I came across What is an easy way to create a MessageBox with custom button text in

相关标签:
8条回答
  • 2020-12-03 05:48

    C# code to accomplish the same thing can be found in an article under MSDN forum, https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3087899&SiteID=1.

    0 讨论(0)
  • 2020-12-03 05:49

    The solution by Daniel Nolan, code in VB.Net

    <DllImport("kernel32.dll")> _
    Private Shared Function GetCurrentThreadId() As UInteger
    End Function
    
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    End Function
    
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
    End Function
    
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hInstance As IntPtr, ByVal threadId As Integer) As Integer
    End Function
    
    <DllImport("user32.dll")> _
    Private Shared Function SetDlgItemText(ByVal hWnd As IntPtr, ByVal nIDDlgItem As Integer, ByVal lpString As String) As Boolean
    End Function
    
    Private Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    
    Shared dlgHookProc As HookProc
    
    Private Const WH_CBT As Long = 5
    Private Const HCBT_ACTIVATE As Long = 5
    
    Private Const ID_BUT_OK As Integer = 1
    Private Const ID_BUT_CANCEL As Integer = 2
    Private Const ID_BUT_ABORT As Integer = 3
    Private Const ID_BUT_RETRY As Integer = 4
    Private Const ID_BUT_IGNORE As Integer = 5
    Private Const ID_BUT_YES As Integer = 6
    Private Const ID_BUT_NO As Integer = 7
    
    Private Const BUT_OK As String = "Save"
    Private Const BUT_CANCEL As String = "Cancelar"
    Private Const BUT_ABORT As String = "Stop"
    Private Const BUT_RETRY As String = "Continue"
    Private Const BUT_IGNORE As String = "Ignore"
    Private Const BUT_YES As String = "Si"
    Private Const BUT_NO As String = "No"
    
    Private Shared _hook As Integer = 0
    
    Private Shared Function DialogHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
      If nCode < 0 Then
        Return CallNextHookEx(_hook, nCode, wParam, lParam)
      End If
    
      If nCode = HCBT_ACTIVATE Then
        SetDlgItemText(wParam, ID_BUT_OK, BUT_OK)
        SetDlgItemText(wParam, ID_BUT_CANCEL, BUT_CANCEL)
        SetDlgItemText(wParam, ID_BUT_ABORT, BUT_ABORT)
        SetDlgItemText(wParam, ID_BUT_RETRY, BUT_RETRY)
        SetDlgItemText(wParam, ID_BUT_IGNORE, BUT_IGNORE)
        SetDlgItemText(wParam, ID_BUT_YES, BUT_YES)
        SetDlgItemText(wParam, ID_BUT_NO, BUT_NO)
      End If
    
      Return CallNextHookEx(_hook, nCode, wParam, lParam)
    End Function
    
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
      dlgHookProc = New HookProc(AddressOf DialogHookProc)
    
      _hook = SetWindowsHookEx(CInt(WH_CBT), dlgHookProc, IntPtr.op_Explicit(0), CInt(GetCurrentThreadId()))
    
      Dim dlgEmptyCheck As DialogResult = MessageBox.Show("Text", "Caption", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3)
    
    
      If dlgEmptyCheck = DialogResult.Abort Then
      End If
    
      UnhookWindowsHookEx(_hook)
    End Sub
    
    0 讨论(0)
提交回复
热议问题