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.
Printable View
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.
This is what tou want.
Here is the exe version.
You Can Use Frame For That, Design Frame like massage box, at desigen time set frame ‘Bring to Front’ and it run time
If WantotShow=True Then
MasageFrmae.Visible = True
Else
MasageFrmae.Visible= False
EndIf
Hey VB IT, I have no clue what the you're talking about.
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
* Replace Formname with your forms name...
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.
hmm i too am interested in seeing how you can center it with code.
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. :confused:
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.
I too suggest that U go for a Custom Designed MessageBox with Form, though the code does something like what U'r after using the MessageBox API Call.
:)VB Code:
'1 Form '1 Timer Control name tmrMBox '1 Command Button to popup the MsgBox Const MB_OK = &H0& Private Declare Function MessageBox _ Lib "user32" Alias "MessageBoxA" _ (ByVal hwnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) As Long Private Declare Function FindWindow _ Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Private Declare Function MoveWindow Lib "user32" _ (ByVal hwnd As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal bRepaint As Long) As Long Dim lngMBoxHwnd As Long Private Sub Command1_Click() MessageBox Me.hwnd, "MBox Prompt", "MBox Title", MB_OK End Sub Private Sub Form_Load() Me.ScaleMode = vbPixels tmrMBox.Interval = 1 tmrMBox.Enabled = True End Sub Private Sub tmrMBox_Timer() Dim oLeft As Long Dim oTop As Long Debug.Print Now lngMBoxHwnd = FindWindow(ByVal vbNullString, "MBox Title") If lngMBoxHwnd > 0 Then oTop = Me.ScaleTop + (Me.ScaleHeight / 2) oLeft = Me.ScaleLeft + (Me.ScaleWidth / 2) 'U could use the GetWindowPlacement API Call to 'Find out the length of the MessageBox to correctly 'set the width of the msgbox MoveWindow lngMBoxHwnd, oLeft, oTop, 100, 100, 1 End If End Sub
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:Example Usage: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 ' Attempt to center this dialog on that Window. If mlCenterhWnd Then ' Get the Dialog's Coords Call GetWindowRect(hwnd, tRECT) ' Get the Owner Window's Coords Call GetWindowRect(mlCenterhWnd, tOWNER) ' Calculate the Center Owner Position 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) ' Reposition the MsgBox Dialog SetWindowPos hwnd, 0, tRECT.Left, tRECT.Top, 0, 0, SWP_NOSIZE Or SWP_NOZORDER End If Case WM_DESTROY ' Remove the MsgBox Subclassing Call SetWindowLong(hwnd, GWL_WNDPROC, mlPrevWnd) End Select SubMsgBox = CallWindowProc(mlPrevWnd, 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) If tCWP.message = WM_CREATE Then sClass = Space(255) sClass = Left(sClass, GetClassName(tCWP.hwnd, ByVal sClass, 255)) If sClass = "#32770" Then ' Subclass the Messagebox as it's created mlPrevWnd = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubMsgBox) End If End If HookWindow = CallNextHookEx(mlHook, nCode, wParam, ByVal lParam) End Function Public Function MsgBoxEx(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context, Optional CenterhWnd As Long) As VbMsgBoxResult ' Store the Parent Window Handle (if specified) mlCenterhWnd = CenterhWnd 'Set the Defaults If IsEmpty(Title) Then Title = App.Title 'Hook the Thread mlHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID) 'Store the Return Value MsgBoxEx = MsgBox(Prompt, Buttons, Title, HelpFile, Context) 'Remove the Hook Call UnhookWindowsHookEx(mlHook) End Function Private Function LOWORD(dwValue As Long) As Integer CopyMemory LOWORD, dwValue, 2 End Function Private Function HIWORD(dwValue As Long) As Integer CopyMemory HIWORD, ByVal VarPtr(dwValue) + 2, 2 End Function Private Function MAKELONG(wLow As Long, wHigh As Long) As Long MAKELONG = LOWORD(wLow) Or (&H10000 * LOWORD(wHigh)) End FunctionVB Code:
Private Sub Command1_Click() MsgBoxEx "Example of a Messagebox" & vbCrLf & "Which has been Centered on it's owner.", vbInformation + vbOKOnly, CenterhWnd:=Me.hwnd End Sub
Aaron - you are the man! That is incredible!
That is also the most code I have ever seen to display a msgbox! :p
Just tried out your example Aaron.
Works - Cool job :cool:
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.