Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBox.Show()

Tags:

.net

winforms

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?

like image 529
Jepe d Hepe Avatar asked Oct 27 '09 07:10

Jepe d Hepe


3 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.

like image 116
Yogesh Avatar answered Sep 23 '22 10:09

Yogesh


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);
    }
}
like image 31
AH. Avatar answered Sep 23 '22 10:09

AH.


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.

like image 28
Greg Hewgill Avatar answered Sep 21 '22 10:09

Greg Hewgill