-
Notifications
You must be signed in to change notification settings - Fork 1
/
modMessageBox.bas
91 lines (76 loc) · 3.84 KB
/
modMessageBox.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Attribute VB_Name = "modMessageBox"
Option Explicit
Private Declare Function MessageBoxEx Lib "user32" Alias "MessageBoxExA" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal uType As Long, ByVal wLanguageId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () 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 SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Const GWL_HINSTANCE = (-6)
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10
Private Const HCBT_ACTIVATE = 5
Private Const WH_CBT = 5
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim hHook As Long
Dim parenthWnd As Long
Public Function MessageBox(ByVal hWnd As Long, ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal Title As String = "", Optional ByVal HelpFile As String, Optional ByVal Context, Optional ByVal centerForm As Boolean = False) As VbMsgBoxResult
Dim ret As Long
Dim hinst As Long
Dim Thread As Long
'Set up the CBT hook
On Error Resume Next
If Title = "" Then Title = App.Title
parenthWnd = hWnd
hinst = GetWindowLong(hWnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
If centerForm Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hinst, Thread)
Else
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hinst, Thread)
End If
ret = MessageBoxEx(hWnd, Prompt, Title, Buttons, 0)
MessageBox = ret
End Function
Private Function WinProcCenterScreen(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim X As Long, Y As Long
On Error Resume Next
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
Call GetWindowRect(wParam, rectMsg)
X = Screen.Width / Screen.TwipsPerPixelX / 2 - (rectMsg.Right - rectMsg.Left) / 2
Y = Screen.Height / Screen.TwipsPerPixelY / 2 - (rectMsg.Bottom - rectMsg.Top) / 2
Call SetWindowPos(wParam, 0, X, Y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)
'Release the CBT hook
Call UnhookWindowsHookEx(hHook)
End If
WinProcCenterScreen = False
End Function
Private Function WinProcCenterForm(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT
Dim rectMsg As RECT
Dim X As Long, Y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
On Error Resume Next
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
Call GetWindowRect(parenthWnd, rectForm)
Call GetWindowRect(wParam, rectMsg)
X = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - ((rectMsg.Right - rectMsg.Left) / 2)
Y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - ((rectMsg.Bottom - rectMsg.Top) / 2)
'Position the msgbox
Call SetWindowPos(wParam, 0, X, Y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)
'Release the CBT hook
Call UnhookWindowsHookEx(hHook)
End If
WinProcCenterForm = False
End Function