|
网上琢磨了半天,终于摸清楚了。上代码。
感谢下面的网址上来源代码启示。
http://www.vbgood.com/thread-72093-1-1.html
http://www.office-cn.net/forum.p ... 3D1&page=1&
https://blog.csdn.net/guorong520/article/details/89400745
从网上下载下来的是两个按钮的,想搞多个按钮的,下面代码是三个按钮的。
直接复制下面所有代码到模块内,运行test
整个代码的框架就看第3个链接一目了然。语言都有类似性。
如果需要换图标,也可以换的。
'================以下为Msgbox自定义按键文字代码 1/3 Parts 之Declare 部份 ↓↓↓↓↓↓↓↓↓↓==========================
Private sButton1 As String
Private sButton2 As String
Private sButton3 As String
Private sCaption As String
Private sText As String
Private Const MB_ICONQUESTION As Long = &H20& 'QUESTION图标 这里不要改动
'Private Const MB_OKCANCEL As Long =&H1& 'OKCANCEL 这里的赋值不要改动
Private Const MB_YESNOCANCEL As Long = &H3& 'YESNOCANCEL 这里赋值不要改动
Private Const MB_TASKMODAL As Long = &H2000&
Private Const IDPROMPT = &HFFFF&
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
'Private Const HCBT_ACTIVATE = 6
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type
Private MSGHOOK As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Const IDOK = 1
Public Const IDCANCEL = 2
Public Const IDABORT = 3
Public Const IDRETRY = 4
Public Const IDIGNORE = 5
Public Const IDYES = 6
Public Const IDNO = 7
'================以上为Msgbox自定义按键文字代码 1/3 Parts 之Declare 部份 ↑↑↑↑↑↑↑==========================
'================以上为Msgbox自定义按键文字代码 2/3 Parts 之Function 部份 ↓↓↓↓↓↓↓==========================
Private Function myMessageBox(hwndThreadOwner As Long, hwndOwner As Long, strCaption As String, strText As String, strButton1 As String, strButton2 As String, strButton3 As String) As Long
'上面的声明根据按钮的多少增删
sButton1 = strButton1 '根据按钮的多少增删
sButton2 = strButton2 '根据按钮的多少增删
sButton3 = strButton3 '根据按钮的多少增删
sCaption = strCaption
sText = strText
Dim hInstance As Long, hThreadId As Long
hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
With MSGHOOK
.hwndOwner = hwndOwner
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
End With
' myMessageBox = MessageBox(hwndOwner, Space$(120), Space$(120), MB_OKCANCEL Or MB_ICONQUESTION)
myMessageBox = MessageBox(hwndOwner, Space$(120), Space$(120), MB_YESNOCANCEL Or MB_ICONQUESTION)
End Function
Private Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = HCBT_ACTIVATE Then
SetWindowText wParam, sCaption
SetDlgItemText wParam, IDYES, sButton1 '第1个按键
SetDlgItemText wParam, IDNO, sButton2 '第2个按键
SetDlgItemText wParam, IDCANCEL, sButton3 '第3个按键
SetDlgItemText wParam, &HFFFF&, sText
UnhookWindowsHookEx MSGHOOK.hHook '这句Unhook千万不能误删,否则会有意想不到的后果。
End If
MsgBoxHookProc = False
End Function
'================以上为Msgbox自定义按键文字代码 2/3 Parts 之Function 部份 ↑↑↑↑↑↑↑==========================
'================以上为Msgbox自定义按键文字代码 3/3 Parts 之调用过程 部份 ↓↓↓↓↓↓↓==========================
Sub test()
'这一过程为验证测试代码
Dim msg As Long
msg = myMessageBox(0, GetDesktopWindow(), "Question", "Which one do you love,Word or Excel or PPT?", "Excel", "Word", "PPT")
'MsgBox msg
If msg = 6 Then myMessageBox 0, GetDesktopWindow(), "I love Excel", "Which Version?", "Excel 97", "Excel 2007", "Excel 2016"
If msg = 7 Then myMessageBox 0, GetDesktopWindow(), "I love Word", "Which Version?", "Word 97", "Word 2007", "Word 2016"
If msg = 2 Then myMessageBox 0, GetDesktopWindow(), "I love PPT", "Which Version?", "PPT 97", "PPT 2007", "PPT 2016"
End Sub
'================以上为Msgbox自定义按键文字代码 3/3 Parts 之调用过程 部份 ↑↑↑↑↑↑↑==========================
|
评分
-
4
查看全部评分
-
|