I would like to Show my Messagebox in the center of its parent form. if i move the form and show the messagebox, it always show in the center of the desktop. i want it to appear along with the form. Can you give me some tricks and advice?
-
Possible duplicate of [Winforms-How can I make MessageBox appear centered on MainForm?](https://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform) – StayOnTarget Apr 30 '19 at 15:23
5 Answers
The best way to do this is to use Window Hooks and center the message box yourself. There is a perfect article which shows this usage.
You can find it here: http://www.codeproject.com/KB/dialog/CenterDialog.aspx
You can also use the class in your application without diving in too deep to find out how it actually works.

- 14,498
- 6
- 44
- 69
-
Article is great. Just need to understand it before applying it to my app. tnx. is there much easier way? heh.. – Jepe d Hepe Oct 27 '09 at 08:55
-
1The easiest way would be make a new MessageBox form yourself. That will be according to your taste and will look like the way you want it to be. Also, you can add any functionality you like. – Yogesh Oct 27 '09 at 09:40
I made this class based on a class for Windows Forms which I found somwehere else.
Just add the class to your WPF project and provide "this" as a parameter to the helper method like this:
MessageBoxHelper.PrepToCenterMessageBoxOnForm(this)"
Then show the message box:
MessageBox.Show("Hello there!");
/// <summary>
/// This class makes it possible to center a MessageBox over the parent dialog.
/// Usage example:
/// MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
/// MessageBox.Show("Hello there!);
/// </summary>
public static class MessageBoxHelper
{
public static void PrepToCenterMessageBoxOnForm(Window window)
{
MessageBoxCenterHelper helper = new MessageBoxCenterHelper();
helper.Prep(window);
}
private class MessageBoxCenterHelper
{
private int messageHook;
private IntPtr parentFormHandle;
public void Prep(Window window)
{
NativeMethods.CenterMessageCallBackDelegate callBackDelegate = new NativeMethods.CenterMessageCallBackDelegate(CenterMessageCallBack);
GCHandle.Alloc(callBackDelegate);
parentFormHandle = new WindowInteropHelper(window).Handle;
messageHook = NativeMethods.SetWindowsHookEx(5, callBackDelegate, new IntPtr(NativeMethods.GetWindowLong(parentFormHandle, -6)), NativeMethods.GetCurrentThreadId()).ToInt32();
}
private int CenterMessageCallBack(int message, int wParam, int lParam)
{
NativeMethods.RECT formRect;
NativeMethods.RECT messageBoxRect;
int xPos;
int yPos;
if (message == 5)
{
NativeMethods.GetWindowRect(parentFormHandle, out formRect);
NativeMethods.GetWindowRect(new IntPtr(wParam), out messageBoxRect);
xPos = (int)((formRect.Left + (formRect.Right - formRect.Left) / 2) - ((messageBoxRect.Right - messageBoxRect.Left) / 2));
yPos = (int)((formRect.Top + (formRect.Bottom - formRect.Top) / 2) - ((messageBoxRect.Bottom - messageBoxRect.Top) / 2));
NativeMethods.SetWindowPos(wParam, 0, xPos, yPos, 0, 0, 0x1 | 0x4 | 0x10);
NativeMethods.UnhookWindowsHookEx(messageHook);
}
return 0;
}
}
private static class NativeMethods
{
internal struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
internal delegate int CenterMessageCallBackDelegate(int message, int wParam, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnhookWindowsHookEx(int hhk);
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("kernel32.dll")]
internal static extern int GetCurrentThreadId();
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int hook, CenterMessageCallBackDelegate callback, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
}
}

- 239,200
- 50
- 490
- 574

- 2,913
- 1
- 28
- 26
-
You forgot to say *where* you stole this code from. Attribution is important here. A Google search turns up a couple of possible sources: http://www.jasoncarr.com/technology/centering-a-message-box-on-the-active-window-in-csharp, http://code.google.com/p/lioneditor/source/browse/branches/imageEditorv2/FFTPatcher/PatcherLib/MyMessageBox.cs?spec=svn468&r=468. – Cody Gray - on strike Apr 11 '11 at 08:22
-
Good idea. But please mind your words : "stole" that is a harsh expression. I explictly said "found somwhere else", but you are absolutely right about attribution. – AH. Apr 13 '11 at 12:58
-
1And no, the original code did not come frome the link you added. It came from Jason Carr: http://www.jasoncarr.com/technology/centering-a-message-box-on-the-active-window-in-csharp. I just modified it to work in WPF. Thanks Jason. – AH. Apr 13 '11 at 17:06
-
-
You should create a variable to store the result of GCHandle.Alloc and call GCHandle.Free() inside of the Callback Hook after UnhookWindowsHookEx – Alan Jun 25 '13 at 21:39
Set the owner of the message box window to your window (using the first parameter of .Show()
), instead of not setting an owner.
See here for a reference.

- 951,095
- 183
- 1,149
- 1,285
-
1can you give me some example? i've tried this: IWin32Window win = this; MessageBox.Show(win,"TESTING"); Although, same result. Messagebox still pop up in the middle of the desktop – Jepe d Hepe Oct 27 '09 at 07:36
-
I suppose that depends on what `this` is. I'm afraid that my experience is limited to the native Win32 `MessageBox()` function, which is a bit different, and I got halfway through answering before I realised you were using WinForms. So I modified my answer to fit what I found in the reference, but I may still have some details missing. :) – Greg Hewgill Oct 27 '09 at 07:44
-
1Passing a window handle to MessageBox does not display the message box in the center of the parent. It is just to enable minimize/maximize with the parent and there is no specific task icon for the message box window. – Yogesh Oct 27 '09 at 08:24
-
can you give me an example to display messagebox in the center of the parent? – Jepe d Hepe Oct 27 '09 at 08:35
-
@Yogesh: interesting, perhaps the WinForms MessageBox isn't implemented using the Win32 MessageBox function. – Greg Hewgill Oct 27 '09 at 09:07
I have done this before in C#. Here's what I remember.
Define a class:
public class IWindowWrapper : System.Windows.Forms.IWin32Window
{
public IWindowWrapper(IntPtr handle)
{
this.Handle= handle;
}
public IntPtr Handle
{
get;
set;
}
}
Define a class based on MessageBox. Create a class based on MessageBox and create a new Show method :
public string Show(IWin32Window owner)
{
if(owner == null)
{
this.ShowDialog();
}
else
{
//parentWindow.
this.StartPosition = FormStartPosition.CenterParent;
this.ShowDialog(owner);
}
}
In your calling code (here it is assumed to be a winform and msgBox is based on the new message box class) call the new Show method and pass an IWindowWrapper instance on Show e.g.
msgBox.Show(new IWindowWrapper(this.Handle))
-
-
-
You don't have to use C# to do this, but that's what I used. Is that the language that you're programming in? – ChrisBD Oct 27 '09 at 08:27
-
The Second Code. Am i going to Inherit a Messagebox? or just create a form with the same style as Messagebox? this.ShowDialog() is not availabe if i inherit a messagebox – Jepe d Hepe Oct 27 '09 at 08:28
-
-
2@ChrisBD: How do you plan to inherit a MessageBox? Can you show an example? – Yogesh Oct 27 '09 at 08:34
-
This absolutely DOES NOT WORK. You cannot inherit from MessageBox. – Sod Almighty Jul 23 '21 at 15:57
Here is a very easy to use solution, and it works perfectly:
Steps:
- Copy and paste that class into your project. I used it without any editing.
- To use the modified MessageBox, use this line of code in your project:
(inside a UserControl
or a Form
)
MessageBoxEx.Show(this, "Please fix the validation errors before saving.", "Validation Errors");