home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1999 April / CD_Shareware_Magazine_31.iso / Free / Prg / MessageBoxDemo.exe / MManipulateMsgBox.bas < prev    next >
Encoding:
BASIC Source File  |  1998-10-23  |  4.6 KB  |  115 lines

  1. Attribute VB_Name = "MManipulateMsgBox"
  2.   Option Explicit
  3.   ' demo project showing how to use the API to manipulate a messagebox
  4.   ' by Bryan Stafford of New Vision Software« - newvision@imt.net
  5.   ' this demo is released into the public domain "as is" without
  6.   ' warranty or guaranty of any kind.  In other words, use at
  7.   ' your own risk.
  8.   
  9.   
  10.   ' the max length of a path for the system (usually 260 or there abouts)
  11.   ' this is used to size the buffer string for retrieving the class name of the active window below
  12.   Public Const MAX_PATH As Long = 260&
  13.  
  14.   Public Const API_FALSE As Long = 0&
  15.   
  16.   ' made up constants for setting our timer
  17.   Public Const NV_CLOSEMSGBOX As Long = &H5000&
  18.   Public Const NV_MOVEMSGBOX As Long = &H5001&
  19.  
  20.   ' MessageBox() Flags
  21.   Public Const MB_ICONQUESTION As Long = &H20&
  22.   Public Const MB_TASKMODAL As Long = &H2000&
  23.  
  24.   ' SetWindowPos Flags
  25.   Public Const SWP_NOSIZE As Long = &H1&
  26.   Public Const SWP_NOZORDER As Long = &H4&
  27.   Public Const HWND_TOP As Long = 0&
  28.  
  29.   Type RECT
  30.     Left As Long
  31.     Top As Long
  32.     Right As Long
  33.     Bottom As Long
  34.   End Type
  35.  
  36.   ' API declares
  37.   Public Declare Function LockWindowUpdate& Lib "user32" (ByVal hwndLock&)
  38.   
  39.   Public Declare Function GetActiveWindow& Lib "user32" ()
  40.   
  41.   Public Declare Function GetDesktopWindow& Lib "user32" ()
  42.   
  43.   Public Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, _
  44.                                                                         ByVal lpWindowName$)
  45.  
  46.   Public Declare Function SetForegroundWindow& Lib "user32" (ByVal hWnd&)
  47.   
  48.   Public Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal hWnd&, _
  49.                                                         ByVal lpClassName$, ByVal nMaxCount&)
  50.  
  51.   Public Declare Function GetWindowRect& Lib "user32" (ByVal hWnd&, lpRect As RECT)
  52.   
  53.   Public Declare Function SetWindowPos& Lib "user32" (ByVal hWnd&, ByVal hWndInsertAfter&, _
  54.                                       ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal wFlags&)
  55.                                       
  56.   Public Declare Function MessageBox& Lib "user32" Alias "MessageBoxA" (ByVal hWnd&, _
  57.                                                 ByVal lpText$, ByVal lpCaption$, ByVal wType&)
  58.  
  59.   Public Declare Function SetTimer& Lib "user32" (ByVal hWnd&, ByVal nIDEvent&, ByVal uElapse&, _
  60.                                                                             ByVal lpTimerFunc&)
  61.   
  62.   Public Declare Function KillTimer& Lib "user32" (ByVal hWnd&, ByVal nIDEvent&)
  63.  
  64. Public Sub TimerProc(ByVal hWnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
  65.   ' this is a callback function.  This means that windows "calls back" to this function
  66.   ' when it's time for the timer event to fire
  67.   
  68.   ' first thing we do is kill the timer so that no other timer events will fire
  69.   KillTimer hWnd, idEvent
  70.   
  71.   ' select the type of manipulation that we want to perform
  72.   Select Case idEvent
  73.     Case NV_CLOSEMSGBOX '<-- we want to close this messagebox after 4 seconds
  74.       Dim hMessageBox&
  75.       
  76.       ' find the messagebox window
  77.       hMessageBox = FindWindow("#32770", "Self Closing Message Box")
  78.       
  79.       ' if we found it make sure it has the keyboard focus and then send it an enter to dismiss it
  80.       If hMessageBox Then
  81.         Call SetForegroundWindow(hMessageBox)
  82.         SendKeys "{enter}"
  83.       End If
  84.       
  85.     Case NV_MOVEMSGBOX '<-- we want to move this messagebox
  86.       Dim hMsgBox&, xPoint&, yPoint&
  87.       Dim stMsgBoxRect As RECT, stParentRect As RECT
  88.       
  89.       ' find the messagebox window
  90.       hMsgBox = FindWindow("#32770", "Position A Message Box")
  91.     
  92.       ' if we found it then move it
  93.       If hMsgBox Then
  94.         ' get the rect for the parent window and the messagebox
  95.         Call GetWindowRect(hMsgBox, stMsgBoxRect)
  96.         Call GetWindowRect(hWnd, stParentRect)
  97.         
  98.         ' calculate the position for putting the messagebox in the middle of the form
  99.         xPoint = stParentRect.Left + (((stParentRect.Right - stParentRect.Left) \ 2) - _
  100.                                               ((stMsgBoxRect.Right - stMsgBoxRect.Left) \ 2))
  101.         yPoint = stParentRect.Top + (((stParentRect.Bottom - stParentRect.Top) \ 2) - _
  102.                                               ((stMsgBoxRect.Bottom - stMsgBoxRect.Top) \ 2))
  103.         
  104.         ' move the messagebox
  105.         Call SetWindowPos(hMsgBox, HWND_TOP, xPoint, yPoint, _
  106.                                         API_FALSE, API_FALSE, SWP_NOZORDER Or SWP_NOSIZE)
  107.       End If
  108.       
  109.       ' unlock the desktop
  110.       Call LockWindowUpdate(API_FALSE)
  111.   
  112.   End Select
  113.   
  114. End Sub
  115.