Is there a way to center a message box on the form? In other words I want the message box to be center owner. It always seems that the message box is center screen.
1st: create your own form, design it and put text and buttons.
2nd: name and set options for the form, and then set StartupPosition to 2 - CenterOwner
3rd: to open it, just put in *Formname.visble = True, and to close it; *Formname.visble = False
I don't want to create a new form for the message box. All I want to know is, is there an easy way to center my message box in my form instead of the screen. I only want to use the MsgBox function, I don't want to create a new form.
I don't believe it can be done.
I tried using the API message box and get the handel,
but it does not return the handel only a '1' for success.
My thought was to get the message box handel and use
GetParent and SetParent API's to set it to your form.
Without the handel it can not be moved.
Since the vb message box function and API message box functions
utilize the same user32.dll, unless you had the code to the dll,
I don't believe it can be done.
Looks like you will have to create another form to simulate
the message box.
If someone else has any other ideas, or can prove me wrong,
I'd like to see them too.
I also do not beleive it is possible. You'll have to make your own message box which is actually better if you ask me becasue you can totaly customize it and do whatever you want with it.
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.
KayJay: wont the messagebox stop the processing of the code since its modal?
Therefore the timer will not fire until after the messagebox has been dismissed.
Almost anything is possible in VB with a little help from the API's:
In a Standard Module:
VB Code:
Option Explicit
Private Type CWPSTRUCT
lParam As Long
wParam As Long
message As Long
hwnd As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom 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 Const WH_CALLWNDPROC = 4
Private Const GWL_WNDPROC = (-4)
Private Const WM_DESTROY = &H2
Private Const WM_CREATE = &H1
Private Const WM_SHOWWINDOW = &H18
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private mlHook As Long
Private mlPrevWnd As Long
Private mlCenterhWnd 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
Select Case Msg
Case WM_SHOWWINDOW
' MsgBox Displayed, If a window Handle was passed to the MsgBoxEx Function
Public Function MsgBoxEx(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context, Optional CenterhWnd As Long) As VbMsgBoxResult
That is also the most code I have ever seen to display a msgbox!
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
Here is another way of doing it using APIs that I picked up a while ago. Not that it really matters but it seems to be a little shorter than Aaron's code.