Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to center a message box in a form

Status
Not open for further replies.

rickpro

Programmer
Sep 11, 2002
7
CA
How can I center a message box in my visual basic app? I want the message box to be the center of the form, I know how to do this with input boxes but that's about it. Thanks all!

Rick
 
Well if you are using the generic MSGbox function, then it is already centered. But if you are using another form as a message box, then use one of these two. First one if you have an MDI Form in your project, and second one if you dont have an MDIForm...
'===========================================================
Public Function setPosition(frm as form)

frm.Move (MDIForm1.ScaleWidth - frm.ScaleWidth) / 2, (MDIForm1.ScaleHeight - frm.ScaleHeight) / 2

End Function
'===========================================================
Public Function setPosition(frm as form)

frm.Move (screen.Width - frm.ScaleWidth) / 2, (screen.height- frm.ScaleHeight) / 2

End Function
'===========================================================

All the best All the Best
Praveen Menon
pcmin@rediffmail.com
 
The generic MsgBox function can't be centred within a form (as far as I know) only on screen. You will need to create your own.

Copy the following code into a module:
'-----------------------------------------------------
Global glForm As Form

Public Function Message_Box()
frmMessageBox.Move (glForm.Left + (glForm.Width - frmMessageBox.Width) / 2), _
(glForm.Top + (glForm.Height - frmMessageBox.Height) / 2)
End Function
'-----------------------------------------------------

Add a form to your project and name it frmMessageBox.
In the frmMessageBox's load event add the following to use the message box:
'-----------------------------------------------------
Message_Box
'-----------------------------------------------------

To test the message box draw a command button in another form, add the following to the button's click event.
'-----------------------------------------------------
Set glForm = Me
Load frmMessageBox
frmMessageBox.Show
'-----------------------------------------------------

This will centre the message box within any form it is called from regardless of the form's size.

If you need any help on creating your own messagebox, please let me know.

Bill Power




 
VB's MsgBox can be centered on a form rather than the screen, but it involves a bit of a hack. You are probably better off following the advice given above to create you own messagebox form.

However, if you want to try the hack, here's a version of it. You'll need a project with a Form and a module. Drop the following code into the module:
[tt]
Option Explicit
' Necessary constants for hooking
Private Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5

' Working variables that require global scope in hooking module
Private hHook As Long

' The API declarations we need
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) 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 Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10

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

Private CallingForm As Form


' Our wrapper for the normal MsgBox function takes an additional optional
' parameter that allows us to specify the form we want the msgbox centred
' over
Public Function vbMsgBox(frmCallingForm As Form, Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String, Optional HelpFile As String, Optional Context As Long) As Long
Set CallingForm = frmCallingForm
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, App.hInstance, 0)
vbMsgBox = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
End Function

Private Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim rectForm As RECT
Dim rectMsg As RECT
Dim x As Long, y As Long

If lMsg = HCBT_ACTIVATE Then
' Get the coordinates of the form and the message box so that
' you can determine where the center of the form is located
GetWindowRect CallingForm.hwnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2) - ((rectMsg.Right - rectMsg.Left) / 2)
y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2) - ((rectMsg.Bottom - rectMsg.Top) / 2)
' Position the msgbox
SetWindowPos wParam, 0, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
' Release the CBT hook
UnhookWindowsHookEx hHook
End If

WinProc = False
End Function
[/tt]
Now add a command button to your form, and drop in the following code:
[tt]
Option Explicit

Private Sub Command1_Click()
vbMsgBox Me, "This message box is centered over Form1."
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top