Click here to Skip to main content
Click here to Skip to main content

Centering MessageBox, Common DialogBox or Form on applications

By , 30 Mar 2005
 

Example

Introduction

The .NET standard MessageBox is centered on the screen area, not on the application. This code contains a simple component which uses a hook to center the MessageBox and also other CommonDialog or Forms on the application area.

Background

This article will not explain in detail how to use a hook in a window. The component will add a WH_CALLWNDPROC hook looking for a WM_INITDIALOG message before opening the dialog. When this message occurs, the window is centered on the owner window, or the active window if the owner window is null.

Using the code

Using the code is very easy. It is the same as using the standard MessageBox.

The following static methods are available:

  • DlgBox.ShowDialog(...)

    For CommonDialog and Forms.

  • MsgBox.Show(...)

    For standard MessageBox, you can define the caption.

  • AppBox.Show(...)

    For standard MessageBox with Application.ProductName as caption.

  • ErrBox.Show(...)

    For standard error MessageBox with message or exception.

Example: the following code displays a centered OpenFileDialog:

DlgBox.ShowDialog(new OpenFileDialog());

Example: the following code display a centered exception error message:

try
{
    ...
}
catch (Exception ex)
{
    ErrBox.Show(ex);
}

It is possible to add standard buttons or icons. All the MessageBox methods are wrapped.

Example: the following example displays a centered rich message box:

AppBox.Show("Rich Application Box",
    MessageBoxButtons.YesNoCancel,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2);

Points of Interest

There is a base class WindowsHook to create other hooks. The WndProcRetHook is based on this class. You can use this base class to create your own hook.

There is also a TrueScreenRect property to retrieve the true screen size on multiscreen display.

History

This version is working well since 1.1.2003.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jean-Claude Lanz
Web Developer
Switzerland Switzerland
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: Does not work for all desktop configurations, fixmemberMember 767985416 Feb '11 - 15:05 
would consider posting the vb net code?
Ken
QuestionCenterWindow Class Bug?memberThomas Wells29 Nov '06 - 8:47 
In the WndActivate routine did you really mean this?
 
// if no parent window, center on the primary screen
if (rectForm.right == rectForm.left)
rectForm.right = rectForm.left = Screen.PrimaryScreen.WorkingArea.Width / 2;
if (rectForm.bottom == rectForm.top)
rectForm.bottom = rectForm.top = Screen.PrimaryScreen.WorkingArea.Height / 2;
 
I would think there should be a minus sign in there. It doesn't cause a problem because there is always a parent to center on. Is that right?
 
Thanks
 
Tom
QuestionNot working for VS2005 [modified]membervrodrigo15 Jun '06 - 5:30 
I had implemented this code in VS 2003 and everything worked great. But after we upgraded to VS2005, the WindowActivate event does not get fired and hence the messagebox is always centering on the screen. Any thoughts on why this might be happening?
 
Vinod Rodrigo
 
-- modified at 12:59 Thursday 15th June, 2006
 
-update
The difference between the codebases is in
public void Install()
{
hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
}
MS deprecated AppDomain.GetCurrentThreadId substituting it with Thread.CurrentThread.ManagedThreadId, so now hHook = 0 with the new call
 

AnswerRe: Not working for VS2005memberWillem Fourie2 Sep '08 - 1:55 
Been playing with thsi class under Vista SP1 and VS2008. You're right - the hook handle is always 0. Reverted to the prior code ie. (int)AppDomain.GetCurrentThreadId(), and lo-&-behold, it works as it should...my suggestion - just ignore the compile warning for now....
 
Willem

GeneralNice...memberbrunzefb10 Jun '06 - 3:00 
Hi,
 
just looked at the code, its pretty nice...; adding a private contructor to hide from the documentation is a good trick! I recompiled for .NET 2.0 without problems. The *very* first time I ran the 'Show Open File Dialog', I had a bit of an unwanted animation: the box flashed in the middle of the screen, and then re-centered above the test application. The flash disappeared running it the second time (not that I mind, I'm just curious as to why this might happen).
 
You have some amazing skiing picts on your site!
 
Friedrich
GeneralRe: Nice...memberlinuxyf19 Oct '06 - 23:51 
I also encounter this question, The every first time I ran the 'Show Open File Dialog', I had a bit of an unwanted animation: the box flashed in the middle of the screen, and then be moved to center of the test application.
 
The flash disappeared running it the second time
 
why??? who can answer me???
GeneralProcess.Start...memberPaul Selormey23 Aug '05 - 18:31 
Thanks for the codes.
 
What is the best way to use your codes to hook on an application (like the IE) started by Process.Start()?
 
Best regards,
Paul.

 
Jesus Christ is LOVE! Please tell somebody.
GeneralExcellentmemberNicholas Butler8 Apr '05 - 2:46 
Excellent article - and it even works! Thank you Smile | :)
 
One question - what license applies to this work?
 
I would like to include this functionality in a commercial app - do you mind?
 
----------------------------
Be excellent to each other Smile | :)
GeneralRe: ExcellentmemberJean-Claude Lanz8 Apr '05 - 9:26 
No problem, you can include this component in your application or copy it into your components. This is only an assembly of knowing technology.
GeneralRe: ExcellentmemberNicholas Butler8 Apr '05 - 9:38 
That's great - thanks Smile | :)
 
----------------------------
Be excellent to each other Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 30 Mar 2005
Article Copyright 2005 by Jean-Claude Lanz
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid