Click to See Complete Forum and Search --> : Msgbox Positioning


gknierim
February 29th, 2000, 10:53 AM
Is there a way to specify the position of a MsgBox? You know like centerowner, etc... without making a separate form for the MsgBox? I am using the MsgBox statement.

JimmyT
February 29th, 2000, 12:19 PM
I don't think that you can specify the position of MsgBox, but, you can create your own, using a new form. Set this form to be a dialog box (can't resize it) and place the appropriate text and your desired buttons onto it. You can specify the location of a form.

Good Luck...

Aaron Young
February 29th, 2000, 02:16 PM
Here's a Wrapper I wrote for the MsgBox Dialog which adds 3 extra Parameters;
X, Y and StartupPostion which allow you to specify either Custom X/Y Coords or predefined ones like CenterScreen and CenterOwner:

'In a Module...
public Enum StartupPos
CenterScreen
CenterOwner
Custom
End Enum

private Type RECT
Left as Long
Top as Long
Right as Long
Bottom as Long
End Type

private Type CWPSTRUCT
lParam as Long
wParam as Long
message as Long
hwnd as Long
End Type

private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as Any, Source as Any, byval Length 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 CallNextHookEx Lib "user32" (byval hHook as Long, byval nCode as Long, byval wParam as Long, lParam as Any) as Long
private Declare Function UnhookWindowsHookEx Lib "user32" (byval hHook as Long) as Long
private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long
private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (byval lpPrevWndFunc as Long, byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (byval hwnd as Long, byval lpClassName as string, byval nMaxCount 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 Declare Function GetParent Lib "user32" (byval hwnd as Long) as Long

private Const SWP_FRAMECHANGED = &H20
private Const SWP_NOSIZE = &H1
private Const SWP_NOZORDER = &H4
private Const GWL_WNDPROC = (-4)
private Const WH_CALLWNDPROC = 4
private Const WM_CREATE = &H1
private Const WM_INITDIALOG = &H110

private MSGBOXEX_X as Integer
private MSGBOXEX_Y as Integer
private MSGBOXEX_STARTUP as StartupPos
private lPrevWnd as Long
private lHook as Long

private Function SubMsgBox(byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
Dim tRECT as RECT
Dim tOWNER as RECT
If Msg = WM_INITDIALOG then
'Reposition the MsgBox is Neccessary..
If MSGBOXEX_STARTUP = CenterOwner then
Call GetWindowRect(GetParent(hwnd), tOWNER)
else
Call GetWindowRect(0, tOWNER)
End If
Call GetWindowRect(hwnd, tRECT)
Select Case MSGBOXEX_STARTUP
Case Custom
If MSGBOXEX_X = -1 then
'Center Horz
tRECT.Left = ((Screen.Width / Screen.TwipsPerPixelX) - (tRECT.Right - tRECT.Left)) / 2
else
'Position Horz
tRECT.Left = MSGBOXEX_X
End If
If MSGBOXEX_Y = -1 then
'Center Vert
tRECT.Top = ((Screen.Height / Screen.TwipsPerPixelY) - (tRECT.Bottom - tRECT.Top)) / 2
else
'Position Vert
tRECT.Top = MSGBOXEX_Y
End If
Call SetWindowPos(hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)

Case CenterOwner
tRECT.Left = tOWNER.Left + (((tOWNER.Right - tOWNER.Left) - (tRECT.Right - tRECT.Left)) / 2)
tRECT.Top = tOWNER.Top + (((tOWNER.Bottom - tOWNER.Top) - (tRECT.Bottom - tRECT.Top)) / 2)
Call SetWindowPos(hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)

End Select
'Remove the Messagebox Subclassing
Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
End If
SubMsgBox = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, byval lParam)
End Function

private Function HookWindow(byval nCode as Long, byval wParam as Long, byval lParam as Long) as Long
Dim tCWP as CWPSTRUCT
Dim sClass as string
'This is where you need to Hook the Messagebox
CopyMemory tCWP, byval lParam, len(tCWP)
Select Case tCWP.message
Case WM_CREATE
sClass = Space(255)
sClass = Left(sClass, GetClassName(tCWP.hwnd, byval sClass, 255))
If sClass = "#32770" then
'Subclass the Messagebox as it's created
lPrevWnd = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubMsgBox)
End If
End Select
HookWindow = CallNextHookEx(lHook, nCode, wParam, byval lParam)
End Function

public Function MsgBoxEx(Prompt, optional X = -1, optional Y = -1, optional StartupPosition as StartupPos = CenterScreen, optional Buttons as VbMsgBoxStyle = vbOKOnly, optional Title, optional HelpFile, optional Context) as VbMsgBoxResult
MSGBOXEX_X = X
MSGBOXEX_Y = Y
MSGBOXEX_STARTUP = StartupPosition
'set a Thread Message Hook..
lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID)
MsgBoxEx = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
'Remove the Hook
Call UnhookWindowsHookEx(lHook)
End Function

Usage:
MsgBoxEx "CodeGuru is one of the Best VB Forums!!", , , CenterOwner, vbInformation + vbOKOnly

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

gknierim
March 1st, 2000, 08:50 AM
Thanks. I found something similar in the Knowledge Base Article Q180936.

Sigal Laniado
March 1st, 2000, 08:56 AM
Thanks for your full answer.
What is "#32770"?

Aaron Young
March 1st, 2000, 11:42 AM
#32770 is the Windows Dialog Classname.

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

Sigal Laniado
March 2nd, 2000, 12:27 AM
How do I know what is the windows dialog classname?

Ravi Kiran
March 2nd, 2000, 01:25 AM
This sample is simply superb!. Really Great

RK

Chris Eastwood
March 2nd, 2000, 04:45 AM
Another piece of excellent code by Aaron - I've posted it in a downloadable form on the CodeGuru site at :

[Cimperiali: link updated]

http://codeguru.earthweb.com/vb/articles/2064.shtml
- If anyone wants to try it out.


Chris Eastwood

CodeGuru - the website for developers
http://www.codeguru.com/updates/whats_new.shtml?id=613

Cimperiali
September 23rd, 2003, 04:18 PM
Indeed.

Cesare Imperiali
:o