更动MsgBox Button 标题(CBT Hook)

来源:cww

如果我们使用MsgBox,并设定其Buttons为vbYesNo,则出来的Button有 "是(Y)" 与 "否(N)"
,有没有可能变成"Yes(Y)"和 "No(N)"呢,或者有没有办法设定MsgBox的位置呢?如果我们
可以在MsgBox显示出来之前取得其hWnd,便可以进而更动其位置或Button的标题了;其实这
真的有些困难,因为MsgBox一呼叫,该Process便进入等待MsgBox结束的状态,连Timer都
不会动,所以想取得MsgBox的进一步资讯似乎很难。所以只好借重CBT Hook。

CBT Hook是做什麽用的呢?它是before activating, creating, destroying,
minimizing, maximizing, moving, or sizing a window; before completing a
system command; before removing a mouse or keyboard event from the
system message queue; before setting the keyboard focus; or before
synchronizing with the system message queue.在以上的状况发生之前拦截到讯息。
所以了,这就达到我们的目的 -->可在MsgBox display前取得其hWnd了。取得hWnd後再
使用EmunChildWindows()取得MsgBox的Control视窗,并取得"Button"型态者,进而判
断其Caption,并进行更改之。

这个程式重点在说明CBT Hook,我想没有人会为了更动MsgBox内的Button而如此做,应
会自行写一个Form,而来呼叫它,并可设定其位置。

'以下在Form
Private Sub Command1_Click()
Call EnableHook
MsgBoxTitle = "MyTitle5"
MsgBox "haha", vbYesNo, MsgBoxTitle
Call FreeHook

End Sub

'以下程式在.Bas
 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
 Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
 Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As Long
 Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
 Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
 Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lparam As Long) As Long
 Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

 Public Const HCBT_ACTIVATE = 5
 Public Const WH_CBT = 5

 Public hHook As Long
 Public MsgBoxTitle as String

 Public Sub EnableHook()
 If hHook = 0 Then
    hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, 0, App.ThreadID)
 End If
 End Sub
 Public Sub FreeHook()
 If hHook <> 0 Then
    Call UnhookWindowsHookEx(hHook)
    hHook = 0
 End If
 End Sub

 Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
 If nCode < 0 Then
    HookProc = CallNextHookEx(hHook, nCode, wParam, lparam)
    Exit Function
 End If
 '当MsgBox Activate之前去更改Button的标题
 If nCode = HCBT_ACTIVATE Then
    Dim str5 As String
    Dim len5 As Long, i As Long
    str5 = String(255, 0)
    len5 = 256
    i = GetWindowText(wParam, str5, len5)
    str5 = Left(str5, InStr(1, str5, Chr(0)) - 1)
    '如果Active Window上的标题是MsgBox上的标题
    '
    If str5 = MsgBoxTitle Then
       '取得MsgBox上的所有子window
       Call EnumChildWindows(wParam, AddressOf ChgButtonTitle, 0)
    End If
 End If
 HookProc = 0 '令待完成的动作继续完成,若为1,则取消原本要完成的动作
 End Function

 Private Function ChgButtonTitle(ByVal hwnd As Long, ByVal lparam As Long) As Long
 Dim clsName As String, len5 As Long, i As Long
 Dim Title As String
 If hwnd = 0 Then
    ChgButtonTitle = 0
    Exit Function
 End If
 clsName = String(255, 0)
 len5 = 256
 i = GetClassName(hwnd, clsName, 256)
 clsName = Left(clsName, InStr(1, clsName, Chr(0)) - 1)
 Title = String(255, 0)
 i = GetWindowText(hwnd, Title, 256)
 Title = Left(Title, InStr(1, Title, Chr(0)) - 1)
 '找到Button型态的子Window,并更改其上的标题
 If clsName = "Button" Then
    If Title = "是(&Y)" Then
       Call SetWindowText(hwnd, "Yes(&Y)")
    Else
       If Title = "否(&N)" Then
	  Call SetWindowText(hwnd, "No(&N)")
       End If
    End If
 End If
 ChgButtonTitle = 1 '表示继续找下一个子Window
 End Function