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

Centering MessageBox, Common DialogBox or Form on applications

, 30 Mar 2005
Rate this:
Please Sign up or sign in to vote.
Simple component to center any MessageBox, Form or CommonDialog on applications.

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

Share

About the Author

Jean-Claude Lanz
Web Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
QuestionPerfect Solution PinmemberMember 1082316216-May-14 2:10 
QuestionThe code should be updated PinmemberYouzelin11-Mar-12 6:03 
AnswerRe: The code should be updated PinmemberYouzelin11-Mar-12 6:04 
AnswerRe: The code should be updated - I got it working on 64-bit Windows 7 Pinmemberhatman707-Apr-13 7:41 
GeneralRe: The code should be updated - I got it working on 64-bit Windows 7 PinmemberKyudos15-Sep-14 18:08 
GeneralFolderBrowserDialg Pinmemberdeciacco22-Jan-11 8:20 
GeneralObsolete problem PinmemberBjerner16-Jun-10 1:02 
GeneralThanks! Pinmemberviaducting8-Mar-10 5:43 
QuestionLicense? Pinmemberionymous10-Nov-09 6:11 
Is this licensed under the The Code Project Open License (CPOL)?
Your article does not specify, and your code says "COPYRIGHT: Prosoft-Lanz."
 
I can't find a way to contact you directly.
AnswerRe: License? Pinmemberibuza19-Feb-10 3:36 
AnswerRe: License? PinmemberJean-Claude Lanz21-Feb-10 10:36 
GeneralDoes not work for all desktop configurations, fix PinmemberThomas Wells29-Nov-06 9:51 
GeneralRe: Does not work for all desktop configurations, fix PinmemberMember 767985416-Feb-11 16:05 
QuestionCenterWindow Class Bug? PinmemberThomas Wells29-Nov-06 9:47 
QuestionNot working for VS2005 [modified] Pinmembervrodrigo15-Jun-06 6:30 
AnswerRe: Not working for VS2005 PinmemberWillem Fourie2-Sep-08 2:55 
GeneralNice... Pinmemberbrunzefb10-Jun-06 4:00 
GeneralRe: Nice... Pinmemberlinuxyf20-Oct-06 0:51 
GeneralProcess.Start... PinmemberPaul Selormey23-Aug-05 19:31 
GeneralExcellent PinmemberNicholas Butler8-Apr-05 3:46 
GeneralRe: Excellent PinmemberJean-Claude Lanz8-Apr-05 10:26 
GeneralRe: Excellent PinmemberNicholas Butler8-Apr-05 10:38 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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