Click to See Complete Forum and Search --> : MsgBox Location


St. Simon
June 15th, 2000, 04:29 PM
I'm wondering how I can define the exact location a msgbox will pop up based upon the coordinates of a form.

For example, user clicks a button, msgbox pops up at forms coordinates 0,0.

Thanks in advance.

Iouri
June 15th, 2000, 05:46 PM
Here is a module code and the code for the command button which positions a msgbox



Module
Public Const MAX_PATH As Long = 260&

Public Const API_FALSE As Long = 0&

' made up constants for setting our timer
Public Const NV_CLOSEMSGBOX As Long = &H5000&
Public Const NV_MOVEMSGBOX As Long = &H5001&

' MessageBox() Flags
Public Const MB_ICONQUESTION As Long = &H20&
Public Const MB_TASKMODAL As Long = &H2000&

' SetWindowPos Flags
Public Const SWP_NOSIZE As Long = &H1&
Public Const SWP_NOZORDER As Long = &H4&
Public Const HWND_TOP As Long = 0&

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

' API declares
Public Declare Function LockWindowUpdate& Lib "user32" (ByVal hwndLock&)

Public Declare Function GetActiveWindow& Lib "user32" ()

Public Declare Function GetDesktopWindow& Lib "user32" ()

Public Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, _
ByVal lpWindowName$)

Public Declare Function SetForegroundWindow& Lib "user32" (ByVal hWnd&)

Public Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal hWnd&, _
ByVal lpClassName$, ByVal nMaxCount&)

Public Declare Function GetWindowRect& Lib "user32" (ByVal hWnd&, lpRect As RECT)

Public Declare Function SetWindowPos& Lib "user32" (ByVal hWnd&, ByVal hWndInsertAfter&, _
ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal wFlags&)

Public Declare Function MessageBox& Lib "user32" Alias "MessageBoxA" (ByVal hWnd&, _
ByVal lpText$, ByVal lpCaption$, ByVal wType&)

Public Declare Function SetTimer& Lib "user32" (ByVal hWnd&, ByVal nIDEvent&, ByVal uElapse&, _
ByVal lpTimerFunc&)

Public Declare Function KillTimer& Lib "user32" (ByVal hWnd&, ByVal nIDEvent&)

Public Sub TimerProc(ByVal hWnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
' this is a callback function. This means that windows "calls back" to this function
' when it's time for the timer event to fire

' first thing we do is kill the timer so that no other timer events will fire
KillTimer hWnd, idEvent

' select the type of manipulation that we want to perform
Select Case idEvent
Case NV_CLOSEMSGBOX '<-- we want to close this messagebox after 4 seconds
Dim hMessageBox&

' find the messagebox window
hMessageBox = FindWindow("#32770", "Self Closing Message Box")

' if we found it make sure it has the keyboard focus and then send it an enter to dismiss it
If hMessageBox Then
Call SetForegroundWindow(hMessageBox)
SendKeys "{enter}"
End If

Case NV_MOVEMSGBOX '<-- we want to move this messagebox
Dim hMsgBox&, xPoint&, yPoint&
Dim stMsgBoxRect As RECT, stParentRect As RECT

' find the messagebox window
hMsgBox = FindWindow("#32770", "Position A Message Box")

' if we found it then move it
If hMsgBox Then
' get the rect for the parent window and the messagebox
Call GetWindowRect(hMsgBox, stMsgBoxRect)
Call GetWindowRect(hWnd, stParentRect)

' calculate the position for putting the messagebox in the middle of the form
xPoint = stParentRect.Left + (((stParentRect.Right - stParentRect.Left) \ 2) - _
((stMsgBoxRect.Right - stMsgBoxRect.Left) \ 2))
yPoint = stParentRect.Top + (((stParentRect.Bottom - stParentRect.Top) \ 2) - _
((stMsgBoxRect.Bottom - stMsgBoxRect.Top) \ 2))

' move the messagebox
Call SetWindowPos(hMsgBox, HWND_TOP, xPoint, yPoint, _
API_FALSE, API_FALSE, SWP_NOZORDER Or SWP_NOSIZE)
End If

' unlock the desktop
Call LockWindowUpdate(API_FALSE)

End Select

End Sub


'Form
Private Sub Command1_Click()
' this positions the messagebox in the desired location on the screen.
' the location is defined in the callback timer function

' lock the desktop so that the initial position is not shown
Call LockWindowUpdate(GetDesktopWindow())

' set the callback timer with our application defined ID (NV_MOVEMSGBOX)
' set the time for 10 microseconds to allow the messagebox time to become active
SetTimer hWnd, NV_MOVEMSGBOX, 10&, AddressOf TimerProc

' call the messagebox API function
Call MessageBox(hWnd, "Have you ever seen a message box that wasn't in the middle of the screen?", _
"Position A Message Box", MB_ICONQUESTION Or MB_TASKMODAL)

End Sub


Iouri Boutchkine
iboutchkine@hotmail.com

Aaron Young
June 15th, 2000, 09:00 PM
Try out the sample code I supplied this site a while ago, http://codeguru.earthweb.com/vb/articles/2064.shtml

Aaron Young
Analyst Programmer
ajyoung@charter.net
Certified AllExperts Expert: http://www.allexperts.com/displayExpert.asp?Expert=11884