home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / Print_Tree2211109102011.psc / modMessages.bas < prev    next >
BASIC Source File  |  2011-08-29  |  20KB  |  467 lines

  1. Attribute VB_Name = "modMessages"
  2. ' ***************************************************************************
  3. '  Module:      modMessages.bas
  4. '
  5. '  Purpose:     This module contains routines designed to provide standard
  6. '               formatting for message boxes.  One routine can change the
  7. '               captions on a message box.
  8. '
  9. ' ===========================================================================
  10. '    DATE      NAME / DESCRIPTION
  11. ' -----------  --------------------------------------------------------------
  12. ' 18-Sep-2002  Kenneth Ives  kenaso@tx.rr.com
  13. '              Wrote module
  14. ' 29-Jan-2010  Kenneth Ives  kenaso@tx.rr.com
  15. '              Added custom message box routine
  16. ' 29-Jan-2010  Kenneth Ives  kenaso@tx.rr.com
  17. '              Added custom message box routine
  18. ' 23-Jul-2011  Kenneth Ives  kenaso@tx.rr.com
  19. '              - Updated MessageBoxH() routine on the way button captions
  20. '                are determined.
  21. '              - Renamed MsgBoxHookProc() to MsgboxCallBack() for easier
  22. '                maintenance.
  23. ' ***************************************************************************
  24. Option Explicit
  25.  
  26. ' ***************************************************************************
  27. ' Global constants
  28. ' ***************************************************************************
  29.   Public Const IDOK         As Long = 1  ' one button return value
  30.   Public Const IDYES        As Long = 6
  31.   Public Const IDNO         As Long = 7
  32.   Public Const IDCANCEL     As Long = 2
  33.   Public Const DUMMY_NUMBER As Long = vbObjectError + 513
  34.  
  35. ' ***************************************************************************
  36. ' Module Constants
  37. ' ***************************************************************************
  38.   Private Const MB_OK          As Long = &H0&     ' one button
  39.   Private Const MB_YESNO       As Long = &H4&     ' two buttons
  40.   Private Const MB_YESNOCANCEL As Long = &H3&     ' three buttons
  41.   Private Const GWL_HINSTANCE  As Long = &HFFFA   ' (-6)
  42.   Private Const IDPROMPT       As Long = &HFFFF&
  43.   Private Const WH_CBT         As Long = 5
  44.   Private Const HCBT_ACTIVATE  As Long = 5
  45.  
  46. ' ***************************************************************************
  47. ' Type structures
  48. ' ***************************************************************************
  49.   ' UDT for passing data through the hook
  50.   Private Type MSGBOX_HOOK_PARAMS
  51.       hwndOwner As Long
  52.       hHook     As Long
  53.   End Type
  54.  
  55. ' ***************************************************************************
  56. ' Enumerations
  57. ' ***************************************************************************
  58.   Public Enum enumMSGBOX_ICON
  59.       MSG_NOICON = &H0&            ' 0
  60.       MSG_ICONSTOP = &H10&         ' 16
  61.       MSG_ICONQUESTION = &H20&     ' 32
  62.       MSG_ICONEXCLAMATION = &H30&  ' 48
  63.       MSG_ICONINFORMATION = &H40&  ' 64
  64.   End Enum
  65.   
  66. ' ***************************************************************************
  67. ' Global API Declarations
  68. ' ***************************************************************************
  69.   ' The GetDesktopWindow function returns a handle to the desktop window.
  70.   ' The desktop window covers the entire screen. The desktop window is
  71.   ' the area on top of which other windows are painted.
  72.   Public Declare Function GetDesktopWindow Lib "user32" () As Long
  73.  
  74. ' ***************************************************************************
  75. ' Module API Declarations
  76. ' ***************************************************************************
  77.   ' Retrieves the thread identifier of the calling thread.
  78.   Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
  79.  
  80.   ' The GetWindowLong function retrieves information about the specified
  81.   ' window. The function also retrieves the 32-bit   (long) value at the
  82.   ' specified offset into the extra window memory.
  83.   Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  84.           (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  85.  
  86.   ' Displays a modal dialog box that contains a system icon, a set of
  87.   ' buttons, and a brief application-specific message, such as status
  88.   ' or error information. The message box returns an integer value that
  89.   ' indicates which button the user clicked.
  90.   Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
  91.           (ByVal hwnd As Long, ByVal lpText As String, _
  92.           ByVal lpCaption As String, ByVal wType As Long) As Long
  93.    
  94.   ' The SetDlgItemText function sets the title or text of a control
  95.   ' in a dialog box.
  96.   Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" _
  97.           (ByVal hDlg As Long, ByVal nIDDlgItem As Long, _
  98.           ByVal lpString As String) As Long
  99.       
  100.   ' The SetWindowsHookEx function installs an application-defined hook
  101.   ' procedure into a hook chain. You would install a hook procedure to
  102.   ' monitor the system for certain types of events. These events are
  103.   ' associated either with a specific thread or with all threads in the
  104.   ' same desktop as the calling thread.
  105.   Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
  106.           (ByVal idHook As Long, ByVal lpfn As Long, _
  107.           ByVal hmod As Long, ByVal dwThreadId As Long) As Long
  108.    
  109.   ' The SetWindowText function changes the text of the specified window  ' s
  110.   ' title bar   (if it has one). If the specified window is a control, the
  111.   ' text of the control is changed. However, SetWindowText cannot change
  112.   ' the text of a control in another application.
  113.   Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
  114.           (ByVal hwnd As Long, ByVal lpString As String) As Long
  115.  
  116.   ' The UnhookWindowsHookEx function removes a hook procedure installed
  117.   ' in a hook chain by the SetWindowsHookEx function.
  118.   Private Declare Function UnhookWindowsHookEx Lib "user32" _
  119.           (ByVal hHook As Long) As Long
  120.     
  121. ' ***************************************************************************
  122. ' Global Variables
  123. '
  124. ' Variable name:     gblnStopProcessing
  125. ' Naming standard:   g bln StopProcessing
  126. '                    - --- -------------
  127. '                    |  |    |______ Variable subname
  128. '                    |  |___________ Data type (Boolean)
  129. '                    |______________ Global level designator
  130. '
  131. ' ***************************************************************************
  132.   Public gblnStopProcessing As Boolean
  133.   
  134. ' ***************************************************************************
  135. ' Module Variables
  136. '
  137. ' Variable name:     mastrCaptions
  138. ' Naming standard:   m a str Captions
  139. '                    - - --- -----------
  140. '                    | |  |    |______ Variable subname
  141. '                    | |  |___________ Data type (String)
  142. '                    | |______________ Array designator
  143. '                    |________________ Module level designator
  144. '
  145. ' ***************************************************************************
  146.   Private mlngButtonCount As Long
  147.   Private mstrTitle       As String
  148.   Private mstrPrompt      As String
  149.   Private mastrCaptions() As String
  150.   Private mtypMsgHook     As MSGBOX_HOOK_PARAMS
  151.   
  152.   
  153. ' ***************************************************************************
  154. ' ****                      Methods                                      ****
  155. ' ***************************************************************************
  156.  
  157. ' ***************************************************************************
  158. '  Routine:     InfoMsg
  159. '
  160. '  Description: Displays a VB MsgBox with no return values.  It is designed to
  161. '               be used where no response from the user is expected other than
  162. '               "OK".
  163. '
  164. '  Parameters:  strMsg - The message text
  165. '               strCaption - The MsgBox caption (optional)
  166. '
  167. '  Returns:     None
  168. '
  169. ' ===========================================================================
  170. '    DATE      NAME / DESCRIPTION
  171. ' -----------  --------------------------------------------------------------
  172. ' 18-Sep-2002  Kenneth Ives  kenaso@tx.rr.com
  173. '              Wrote routine
  174. ' ***************************************************************************
  175. Public Sub InfoMsg(ByVal strMsg As String, _
  176.           Optional ByVal strCaption As String = "")
  177.                    
  178.     Dim strNewCaption As String  ' Formatted MsgBox caption
  179.                            
  180.     ' Format the MsgBox caption
  181.     strNewCaption = strFormatCaption(strCaption)
  182.     
  183.     ' the MsgBox routine
  184.     MsgBox strMsg, vbInformation Or vbOKOnly, strNewCaption
  185.     
  186. End Sub
  187.  
  188.  
  189. ' ***************************************************************************
  190. '  Routine:     ResponseMsg
  191. '
  192. '  Description: Displays a standard VB MsgBox and returns the MsgBox code. It
  193. '               is designed to be used when the user is prompted for a
  194. '               response.
  195. '
  196. '  Parameters:  strMsg - The message text
  197. '               lngButtons - The standard VB MsgBox buttons (optional)
  198. '               strCaption - The msgbox caption (optional)
  199. '
  200. '  Returns:     The standard VB MsgBox return values
  201. '
  202. ' ===========================================================================
  203. '    DATE      NAME / DESCRIPTION
  204. ' -----------  --------------------------------------------------------------
  205. ' 18-Sep-2002  Kenneth Ives  kenaso@tx.rr.com
  206. '              Wrote routine
  207. ' ***************************************************************************
  208. Public Function ResponseMsg(ByVal strMsg As String, _
  209.                    Optional ByVal lngButtons As Long = vbQuestion + vbYesNo, _
  210.                    Optional ByVal strCaption As String = "") As VbMsgBoxResult
  211.     
  212.     Dim strNewCaption As String  ' Formatted MsgBox caption
  213.     
  214.     ' Format the MsgBox caption
  215.     strNewCaption = strFormatCaption(strCaption)
  216.     
  217.     ' the MsgBox routine and return the user's response
  218.     ResponseMsg = MsgBox(strMsg, lngButtons, strNewCaption)
  219.     
  220. End Function
  221.  
  222. ' ***************************************************************************
  223. '  Routine:     ErrorMsg
  224. '
  225. '  Description: Displays a standard VB MsgBox formatted to display severe
  226. '               (Usually application-type) error messages.
  227. '
  228. '  Parameters:  strModule - The module where the error occurred
  229. '               strRoutine - The routine where the error occurred
  230. '               strMsg - The error message
  231. '               strCaption - The MsgBox caption  (optional)
  232. '
  233. '  Returns:     None
  234. '
  235. ' ===========================================================================
  236. '    DATE      NAME / DESCRIPTION
  237. ' -----------  --------------------------------------------------------------
  238. ' 18-Sep-2002  Kenneth Ives  kenaso@tx.rr.com
  239. '              Wrote routine
  240. ' ***************************************************************************
  241. Public Sub ErrorMsg(ByVal strModule As String, _
  242.                     ByVal strRoutine As String, _
  243.                     ByVal strMsg As String, _
  244.            Optional ByVal strCaption As String = "")
  245.                      
  246.     Dim strNewCaption As String  ' Formatted MsgBox caption
  247.     Dim strFullMsg As String     ' Formatted message
  248.     
  249.     ' Make sure strModule is populated
  250.     If Len(Trim$(strModule)) = 0 Then
  251.        strModule = "Unknown"
  252.     End If
  253.     
  254.     ' Make sure strRoutine is populated
  255.     If Len(Trim$(strRoutine)) = 0 Then
  256.        strRoutine = "Unknown"
  257.     End If
  258.     
  259.     ' Make sure strMsg is populated
  260.     If Len(Trim$(strMsg)) = 0 Then
  261.        strMsg = "Unknown"
  262.     End If
  263.     
  264.     ' Format the MsgBox caption
  265.     strNewCaption = strFormatCaption(strCaption, True)
  266.     
  267.     ' Format the message
  268.     strFullMsg = "Module: " & vbTab & strModule & vbCr & _
  269.                  "Routine:" & vbTab & strRoutine & vbCr & _
  270.                  "Error:  " & vbTab & strMsg
  271.                      
  272.     ' the MsgBox routine
  273.     MsgBox strFullMsg, vbCritical Or vbOKOnly, strNewCaption
  274.     
  275. End Sub
  276.  
  277. ' ***************************************************************************
  278. ' Routine:       MessageBoxH
  279. '
  280. ' Description:   Displays a standard msgbox with customized captions on
  281. '                the buttons.  Wrapper function for the MessageBox API.
  282. '
  283. ' Reference:     VBNet - API calls for Visual Basic 6.0
  284. '                http://vbnet.mvps.org/
  285. '
  286. ' Parameters:    hwndForm - Long integer system ID designating the form
  287. '                hwndWindow - Long integer system ID designating the
  288. '                           desktop window
  289. '                strPrompt - Main body of text for msgbox
  290. '                strTitle - Caption of msgbox
  291. '                astrCaptions() - String array designating button text
  292. '                           for up to three buttons
  293. '                lngIcon - [Optional] - Designates type of icon to use
  294. '                           Default - no icon
  295. '
  296. ' ===========================================================================
  297. '    DATE      NAME / DESCRIPTION
  298. ' -----------  --------------------------------------------------------------
  299. ' 12-Aug-2008  Randy Birch
  300. '              http://vbnet.mvps.org/code/hooks/messageboxhook.htm
  301. ' 29-Jan-2010  Kenneth Ives  kenaso@tx.rr.com
  302. '              Modified and documented
  303. ' 23-Jul-2011  Kenneth Ives  kenaso@tx.rr.com
  304. '              Updated the way button captions are determined
  305. ' ***************************************************************************
  306. Public Function MessageBoxH(ByVal hwndForm As Long, _
  307.                             ByVal hwndWindow As Long, _
  308.                             ByVal strPrompt As String, _
  309.                             ByVal strTitle As String, _
  310.                             ByRef astrCaptions() As String, _
  311.                    Optional ByVal lngIcon As enumMSGBOX_ICON = MSG_NOICON) As Long
  312.  
  313.     Dim lngIndex    As Long
  314.     Dim hInstance   As Long
  315.     Dim hThreadId   As Long
  316.     Dim lngButtonID As Long
  317.     
  318.     Erase mastrCaptions()                      ' Always start with empty arrays
  319.     mstrPrompt = strPrompt                     ' Save msgbox text
  320.     mstrTitle = strTitle                       ' Save msgbox title
  321.     mlngButtonCount = UBound(astrCaptions)     ' Determine number of buttons needed
  322.     
  323.     ' If array size has been exceeded then
  324.     ' reset button count to max allowed
  325.     If mlngButtonCount > 3 then
  326.         mlngButtonCount = 3   
  327.     End If    
  328.     
  329.     ReDim mastrCaptions(mlngButtonCount)       ' Size array to number of captions
  330.     
  331.     ' Transfer captions to module array
  332.     For lngIndex = 0 To mlngButtonCount - 1
  333.         mastrCaptions(lngIndex) = astrCaptions(lngIndex)
  334.     Next lngIndex
  335.                 
  336.     Select Case mlngButtonCount
  337.            Case 1: lngButtonID = MB_OK
  338.            Case 2: lngButtonID = MB_YESNO
  339.            Case 3: lngButtonID = MB_YESNOCANCEL
  340.            Case Else
  341.                 MessageBoxH = IDCANCEL
  342.                 Exit Function
  343.     End Select
  344.     
  345.     ' Set up the hook
  346.     hInstance = GetWindowLong(hwndForm, GWL_HINSTANCE)
  347.     hThreadId = GetCurrentThreadId()
  348.     
  349.     ' set up the MSGBOX_HOOK_PARAMS values
  350.     ' By specifying a Windows hook as one
  351.     ' of the params, we can intercept messages
  352.     ' sent by Windows and thereby manipulate
  353.     ' the dialog
  354.     With mtypMsgHook
  355.         .hwndOwner = hwndWindow
  356.         .hHook = SetWindowsHookEx(WH_CBT, _
  357.                                   AddressOf MsgboxCallBack, _
  358.                                   hInstance, _
  359.                                   hThreadId)
  360.     End With
  361.     
  362.     ' Call MessageBox API and return the
  363.     ' value as the result of the function
  364.     MessageBoxH = MessageBox(hwndWindow, _
  365.                              mstrPrompt, _
  366.                              mstrTitle, _
  367.                              lngButtonID Or lngIcon)
  368.  
  369. End Function
  370.  
  371.  
  372.  
  373. ' ***************************************************************************
  374. ' ****              Internal Functions and Procedures                    ****
  375. ' ***************************************************************************
  376.  
  377. Private Function MsgboxCallBack(ByVal hInstance As Long, _
  378.                                 ByVal hThreadId As Long, _
  379.                                 ByVal lngNotUsed As Long) As Long
  380.  
  381.     ' Called by MessageBoxH()
  382.     
  383.     ' When the message box is about to be shown,
  384.     ' titlebar text, prompt message and button
  385.     ' captions will be updated
  386.     DoEvents
  387.     If hInstance = HCBT_ACTIVATE Then
  388.  
  389.     
  390.         ' In a HCBT_ACTIVATE message, hThreadId 
  391.         ' holds the handle to the messagebox
  392.         SetWindowText hThreadId, mstrTitle
  393.                   
  394.         ' The ID's of the buttons on the message box
  395.         ' correspond exactly to the values they return,
  396.         ' so the same values can be used to identify
  397.         ' specific buttons in a SetDlgItemText call.
  398.         '
  399.         ' Use default captions if array elements are empty
  400.         Select Case mlngButtonCount
  401.                Case 1
  402.                     SetDlgItemText hThreadId, IDOK, IIf(Len(Trim$(mastrCaptions(0))) > 0, mastrCaptions(0), "OK")
  403.                Case 2
  404.                     SetDlgItemText hThreadId, IDYES, IIf(Len(Trim$(mastrCaptions(0))) > 0, mastrCaptions(0), "Yes")
  405.                     SetDlgItemText hThreadId, IDNO, IIf(Len(Trim$(mastrCaptions(1))) > 0, mastrCaptions(1), "No")
  406.                Case 3
  407.                     SetDlgItemText hThreadId, IDYES, IIf(Len(Trim$(mastrCaptions(0))) > 0, mastrCaptions(0), "Yes")
  408.                     SetDlgItemText hThreadId, IDNO, IIf(Len(Trim$(mastrCaptions(1))) > 0, mastrCaptions(1), "No")
  409.                     SetDlgItemText hThreadId, IDCANCEL, IIf(Len(Trim$(mastrCaptions(2))) > 0, mastrCaptions(2), "Cancel")
  410.         End Select
  411.         
  412.         ' Change dialog prompt text
  413.         SetDlgItemText hThreadId, IDPROMPT, mstrPrompt
  414.                                                
  415.         ' Finished with the dialog, release the hook
  416.         UnhookWindowsHookEx mtypMsgHook.hHook
  417.              
  418.     End If
  419.     
  420.     ' Return False to let normal processing continue
  421.     MsgboxCallBack = 0
  422.  
  423. End Function
  424.  
  425. ' ***************************************************************************
  426. '  Routine:     FormatCaption
  427. '
  428. '  Description: Formats the caption text to use the application title as
  429. '               default
  430. '
  431. '  Parameters:  strCaption - The input caption which may be appended to the
  432. '                            application title.
  433. '               bError - Add "Error" to the caption
  434. '
  435. '  Returns:     Formatted string to be used as a msgbox caption
  436. '
  437. ' ===========================================================================
  438. '    DATE      NAME / DESCRIPTION
  439. ' -----------  --------------------------------------------------------------
  440. ' 18-Sep-2002  Kenneth Ives  kenaso@tx.rr.com
  441. '              Wrote routine
  442. ' ***************************************************************************
  443. Private Function strFormatCaption(ByVal strCaption As String, _
  444.                          Optional ByVal bError As Boolean = False) As String
  445.  
  446.     Dim strNewCaption As String  ' The formatted caption
  447.     
  448.     ' Set the caption to either input parm or the application name
  449.     If Len(Trim$(strCaption)) > 0 Then
  450.         strNewCaption = Trim$(strCaption)
  451.     Else
  452.         ' Set the caption default
  453.         strNewCaption = App.EXEName & " v" & App.Major & "." & App.Minor & "." & App.Revision
  454.     End If
  455.     
  456.     ' Optionally, add error text
  457.     If bError Then
  458.         strNewCaption = strNewCaption & " Error"
  459.     End If
  460.  
  461.     ' Return the new caption
  462.     strFormatCaption = strNewCaption
  463.     
  464. End Function
  465.  
  466.  
  467.