'/////////////////////////////////////
'自定義消息框上的按鈕的文字
'\\\\\\\\\\\\\\\\\\
'鬼龍之舞
'2004-3-1
'以下代碼在模塊
Option Explicit
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 UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private hHook As Long
Private Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If nCode = HCBT_ACTIVATE Then
'HCBT_ACTIVATE時wParam是將被激活的窗口的句柄
Dim hwnd As Long
hwnd = wParam
'設定按鈕的文字
SetDlgItemText hwnd, vbOK, ByVal "確定[&O]"
SetDlgItemText hwnd, vbCancel, ByVal "取消[&C]"
SetDlgItemText hwnd, vbYes, ByVal "是[&Y]"
SetDlgItemText hwnd, vbNo, ByVal "否[&N]"
SetDlgItemText hwnd, vbAbort, ByVal "放棄[&A]"
SetDlgItemText hwnd, vbIgnore, ByVal "忽略[&I]"
SetDlgItemText hwnd, vbRetry, ByVal "重試[&R]"
'不要再Hook,以免帶來不必要的麻煩
UnhookWindowsHookEx hHook
End If
HookProc = 0
End Function
'使用和VB的MsgBox一樣的名字,如果你不想使用自定文字格式,只需要移除本模塊即可
Public Function MsgBox(Prompt As String, Optional Button As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
If Len(Title) = 0 Then Title = App.Title
hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, App.hInstance, App.ThreadID)
'注意下面的第一個參數最好傳遞一個窗口的句柄,當然,讓它作為0也可以
'MsgBox= MessageBox(0, Prompt, Title, Button) '不推薦這樣做
MsgBox = MessageBox(Form1.hwnd, Prompt, Title, Button)
End Function
'窗體部分,
'就如你所見一樣,只是像平常一樣的用法
Option Explicit
Private Sub Command1_Click()
If MsgBox("你好", vbOKCancel + vbInformation) = vbOK Then
Call MsgBox("你按了OK", vbInformation)
End If
End Sub