home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD50754222000.psc / basMessageBox.bas < prev    next >
Encoding:
BASIC Source File  |  2000-01-25  |  3.6 KB  |  79 lines

  1. Attribute VB_Name = "basMessageBox"
  2. 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
  3. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
  4. Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  5. Declare Function GetCurrentThreadId Lib "KERNEL32" () As Long
  6. 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
  7. 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
  8. Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  9.  
  10. Const GWL_HINSTANCE = (-6)
  11. Const SWP_NOSIZE = &H1
  12. Const SWP_NOZORDER = &H4
  13. Const SWP_NOACTIVATE = &H10
  14. Const HCBT_ACTIVATE = 5
  15. Const WH_CBT = 5
  16.  
  17. Type RECT
  18.     left As Long
  19.     top As Long
  20.     Right As Long
  21.     Bottom As Long
  22. End Type
  23.  
  24. Dim hHook As Long
  25. Dim parenthWnd As Long
  26.  
  27. 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 = True) As VbMsgBoxResult
  28. Dim ret As Long
  29.     Dim hInst As Long
  30.     Dim Thread As Long
  31.     'Set up the CBT hook
  32.     parenthWnd = hwnd
  33.     hInst = GetWindowLong(hwnd, GWL_HINSTANCE)
  34.     Thread = GetCurrentThreadId()
  35.     If centerForm = True Then
  36.         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterForm, hInst, Thread)
  37.     Else
  38.         hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProcCenterScreen, hInst, Thread)
  39.     End If
  40.     
  41.     ret = MessageBoxEx(hwnd, Prompt, Title, Buttons, 0)
  42.     MessageBox = ret
  43. End Function
  44.  
  45. Private Function WinProcCenterScreen(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  46.     Dim rectForm As RECT, rectMsg As RECT
  47.     Dim x As Long, y As Long
  48.     If lMsg = HCBT_ACTIVATE Then
  49.         'Show the MsgBox at a fixed location (0,0)
  50.         GetWindowRect wParam, rectMsg
  51.         x = Screen.Width / Screen.TwipsPerPixelX / 2 - (rectMsg.Right - rectMsg.left) / 2
  52.         y = Screen.Height / Screen.TwipsPerPixelY / 2 - (rectMsg.Bottom - rectMsg.top) / 2
  53.         SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  54.         'Release the CBT hook
  55.         UnhookWindowsHookEx hHook
  56.     End If
  57.     WinProcCenterScreen = False
  58. End Function
  59.  
  60. Private Function WinProcCenterForm(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  61.     Dim rectForm As RECT, rectMsg As RECT
  62.     Dim x As Long, y As Long
  63.     'On HCBT_ACTIVATE, show the MsgBox centered over Form1
  64.     If lMsg = HCBT_ACTIVATE Then
  65.         'Get the coordinates of the form and the message box so that
  66.         'you can determine where the center of the form is located
  67.         GetWindowRect parenthWnd, rectForm
  68.         GetWindowRect wParam, rectMsg
  69.         x = (rectForm.left + (rectForm.Right - rectForm.left) / 2) - ((rectMsg.Right - rectMsg.left) / 2)
  70.         y = (rectForm.top + (rectForm.Bottom - rectForm.top) / 2) - ((rectMsg.Bottom - rectMsg.top) / 2)
  71.         'Position the msgbox
  72.         SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  73.         'Release the CBT hook
  74.         UnhookWindowsHookEx hHook
  75.      End If
  76.      WinProcCenterForm = False
  77. End Function
  78.  
  79.