Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Centering MessageBox, Common DialogBox or Form on applications

Rate me:
Please Sign up or sign in to vote.
4.82/5 (15 votes)
30 Mar 20051 min read 173.9K   5.1K   45   24
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:

C#
DlgBox.ShowDialog(new OpenFileDialog());

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

C#
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:

C#
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


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sathish Krishnan.R22-Jan-18 20:07
Sathish Krishnan.R22-Jan-18 20:07 
QuestionPerfect Solution Pin
Member 1082316216-May-14 1:10
Member 1082316216-May-14 1:10 
QuestionThe code should be updated Pin
Youzelin11-Mar-12 5:03
Youzelin11-Mar-12 5:03 
AnswerRe: The code should be updated Pin
Youzelin11-Mar-12 5:04
Youzelin11-Mar-12 5:04 
AnswerRe: The code should be updated - I got it working on 64-bit Windows 7 Pin
hatman707-Apr-13 6:41
hatman707-Apr-13 6:41 
GeneralRe: The code should be updated - I got it working on 64-bit Windows 7 Pin
Kyudos15-Sep-14 17:08
Kyudos15-Sep-14 17:08 
GeneralRe: The code should be updated - I got it working on 64-bit Windows 7 Pin
havelian7-Feb-19 1:29
havelian7-Feb-19 1:29 
GeneralFolderBrowserDialg Pin
deciacco22-Jan-11 7:20
deciacco22-Jan-11 7:20 
GeneralObsolete problem Pin
Bjerner16-Jun-10 0:02
Bjerner16-Jun-10 0:02 
GeneralThanks! Pin
hairy_hats8-Mar-10 4:43
hairy_hats8-Mar-10 4:43 
QuestionLicense? Pin
ionymous10-Nov-09 5:11
ionymous10-Nov-09 5:11 
AnswerRe: License? Pin
ibuza19-Feb-10 2:36
ibuza19-Feb-10 2:36 
AnswerRe: License? Pin
Jean-Claude Lanz21-Feb-10 9:36
Jean-Claude Lanz21-Feb-10 9:36 
GeneralDoes not work for all desktop configurations, fix Pin
Thomas Wells29-Nov-06 8:51
Thomas Wells29-Nov-06 8:51 
GeneralRe: Does not work for all desktop configurations, fix Pin
Member 767985416-Feb-11 15:05
Member 767985416-Feb-11 15:05 
QuestionCenterWindow Class Bug? Pin
Thomas Wells29-Nov-06 8:47
Thomas Wells29-Nov-06 8:47 
QuestionNot working for VS2005 [modified] Pin
vrodrigo15-Jun-06 5:30
vrodrigo15-Jun-06 5:30 
AnswerRe: Not working for VS2005 Pin
Willem Fourie2-Sep-08 1:55
Willem Fourie2-Sep-08 1:55 
GeneralNice... Pin
Friedrich Brunzema10-Jun-06 3:00
Friedrich Brunzema10-Jun-06 3:00 
GeneralRe: Nice... Pin
linuxyf19-Oct-06 23:51
linuxyf19-Oct-06 23:51 
GeneralProcess.Start... Pin
Paul Selormey23-Aug-05 18:31
Paul 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.
GeneralExcellent Pin
Nicholas Butler8-Apr-05 2:46
sitebuilderNicholas Butler8-Apr-05 2:46 
GeneralRe: Excellent Pin
Jean-Claude Lanz8-Apr-05 9:26
Jean-Claude Lanz8-Apr-05 9:26 
GeneralRe: Excellent Pin
Nicholas Butler8-Apr-05 9:38
sitebuilderNicholas Butler8-Apr-05 9:38 

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

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