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   
QuestionThe code should be updatedmemberYouzelin11 Mar '12 - 5:03 
Hi, Lanz,
 
I read your article and run the source code. It seems the code does not work in Windows 7. Maybe you should update your code.
Thank you very much.
AnswerRe: The code should be updatedmemberYouzelin11 Mar '12 - 5:04 
I mean all the boxes will not center to the owner when you show it. Smile | :)
AnswerRe: The code should be updated - I got it working on 64-bit Windows 7memberhatman707 Apr '13 - 6:41 
I am not a WinAPI guru by any means, but this code was just too cool to not update it. (Plus I needed it badly to center the ColorDialog for a project I'm working on. LOL).
 
So I got it working on 64-bit Windows 7. Essentially, the fix (as I understand it - again, I don't usually spelunk in the WinAPI caves) involves the size allocated in the structure to receive lparam, lresult, wparam, etc. These were noted as "int" in the original code, but with 64-bit Windows (from my research on the InterWebs) they need to be IntPtr (8-byte, not 4-byte - or something like that).
 
I am more than willing to ZIP up the code and re-post it. Does anyone know how I do that from the forum Q&A? I have posted some original stuff to codeproject, but I have never posted a code attachment from the forum Q&A.
 
So long story short: I got this bad boy working with 64-bit Windows 7, and it works as advertised. MUCH karma to the OP. I did essentially nothing but update a few struct record sizes. In fact, the code was so insanely complex that I decided I had to try and update it. There was no way I wanted to start from scratch!
 
Cheers,
Michael

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

Permalink | Advertise | Privacy | Mobile
Web03 | 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