31

I have a 700w x 300h WPF application and can drag it anywhere on my large screen.

When my application executes:

MessageBox.Show("Sorry, this function is not yet implemented.");

the mesage box appears in the middle of my screen, which may or may not even be near the application itself.

How can I get my MessageBox to appear in the middle of my application instead?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 2
    Edward, were you able to resolve this problem? I tried using Application.Current.MainWindow but no effect, the Message Box still centers in the screen and not my WPF application main window. – byte Apr 16 '10 at 14:24
  • Here is a reference to a stackoverflow Q&A that actually works (for me) http://stackoverflow.com/questions/1629213/messagebox-show – invalidusername Aug 17 '11 at 16:02

6 Answers6

26

Here's a version of the MessageBoxEx helper class posted in the other thread that uses WPF style message boxes (Note that you still need to reference System.Drawing):

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Drawing;

public class MessageBoxEx
{
    private static IntPtr _owner;
    private static HookProc _hookProc;
    private static IntPtr _hHook;

    public static MessageBoxResult Show(string text)
    {
        Initialize();
        return MessageBox.Show(text);
    }

    public static MessageBoxResult Show(string text, string caption)
    {
        Initialize();
        return MessageBox.Show(text, caption);
    }

    public static MessageBoxResult Show(string text, string caption, MessageBoxButton buttons)
    {
        Initialize();
        return MessageBox.Show(text, caption, buttons);
    }

    public static MessageBoxResult Show(string text, string caption, MessageBoxButton buttons, MessageBoxImage icon)
    {
        Initialize();
        return MessageBox.Show(text, caption, buttons, icon);
    }

    public static MessageBoxResult Show(string text, string caption, MessageBoxButton buttons, MessageBoxImage icon, MessageBoxResult defResult)
    {
        Initialize();
        return MessageBox.Show(text, caption, buttons, icon, defResult);
    }

    public static MessageBoxResult Show(string text, string caption, MessageBoxButton buttons, MessageBoxImage icon, MessageBoxResult defResult, MessageBoxOptions options)
    {
        Initialize();            
        return MessageBox.Show(text, caption, buttons, icon, defResult, options);
    }

    public static MessageBoxResult Show(Window owner, string text)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text);
    }

    public static MessageBoxResult Show(Window owner, string text, string caption)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text, caption);
    }

    public static MessageBoxResult Show(Window owner, string text, string caption, MessageBoxButton buttons)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text, caption, buttons);
    }

    public static MessageBoxResult Show(Window owner, string text, string caption, MessageBoxButton buttons, MessageBoxImage icon)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text, caption, buttons, icon);
    }

    public static MessageBoxResult Show(Window owner, string text, string caption, MessageBoxButton buttons, MessageBoxImage icon, MessageBoxResult defResult)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text, caption, buttons, icon, defResult);
    }

    public static MessageBoxResult Show(Window owner, string text, string caption, MessageBoxButton buttons, MessageBoxImage icon, MessageBoxResult defResult, MessageBoxOptions options)
    {
        _owner = new WindowInteropHelper(owner).Handle;
        Initialize();
        return MessageBox.Show(owner, text, caption, buttons, icon,
                                defResult, options);
    }

    public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

    public delegate void TimerProc(IntPtr hWnd, uint uMsg, UIntPtr nIDEvent, uint dwTime);

    public const int WH_CALLWNDPROCRET = 12;

    public enum CbtHookAction : int
    {
        HCBT_MOVESIZE = 0,
        HCBT_MINMAX = 1,
        HCBT_QS = 2,
        HCBT_CREATEWND = 3,
        HCBT_DESTROYWND = 4,
        HCBT_ACTIVATE = 5,
        HCBT_CLICKSKIPPED = 6,
        HCBT_KEYSKIPPED = 7,
        HCBT_SYSCOMMAND = 8,
        HCBT_SETFOCUS = 9
    }

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);

    [DllImport("user32.dll")]
    private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("User32.dll")]
    public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc);

    [DllImport("User32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

    [DllImport("user32.dll")]
    public static extern int UnhookWindowsHookEx(IntPtr idHook);

    [DllImport("user32.dll")]
    public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    public static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);

    [DllImport("user32.dll")]
    public static extern int EndDialog(IntPtr hDlg, IntPtr nResult);

    [StructLayout(LayoutKind.Sequential)]
    public struct CWPRETSTRUCT
    {
        public IntPtr lResult;
        public IntPtr lParam;
        public IntPtr wParam;
        public uint message;
        public IntPtr hwnd;
    } ;

    static MessageBoxEx()
    {
        _hookProc = new HookProc(MessageBoxHookProc);
        _hHook = IntPtr.Zero;
    }

    private static void Initialize()
    {
        if (_hHook != IntPtr.Zero)
        {
            throw new NotSupportedException("multiple calls are not supported");
        }

        if (_owner != null)
        {
            _hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, _hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
        }
    }

    private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode < 0)
        {
            return CallNextHookEx(_hHook, nCode, wParam, lParam);
        }

        CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
        IntPtr hook = _hHook;

        if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE)
        {
            try
            {
                CenterWindow(msg.hwnd);
            }
            finally
            {
                UnhookWindowsHookEx(_hHook);
                _hHook = IntPtr.Zero;
            }
        }

        return CallNextHookEx(hook, nCode, wParam, lParam);
    }

    private static void CenterWindow(IntPtr hChildWnd)
    {
        Rectangle recChild = new Rectangle(0, 0, 0, 0);
        bool success = GetWindowRect(hChildWnd, ref recChild);

        int width = recChild.Width - recChild.X;
        int height = recChild.Height - recChild.Y;

        Rectangle recParent = new Rectangle(0, 0, 0, 0);
        success = GetWindowRect(_owner, ref recParent);

        System.Drawing.Point ptCenter = new System.Drawing.Point(0, 0);
        ptCenter.X = recParent.X + ((recParent.Width - recParent.X) / 2);
        ptCenter.Y = recParent.Y + ((recParent.Height - recParent.Y) / 2);


        System.Drawing.Point ptStart = new System.Drawing.Point(0, 0);
        ptStart.X = (ptCenter.X - (width / 2));
        ptStart.Y = (ptCenter.Y - (height / 2));

        ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X;
        ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y;

        int result = MoveWindow(hChildWnd, ptStart.X, ptStart.Y, width,
                                height, false);
    }
}
Framnk
  • 261
  • 3
  • 3
  • 5
    This works great although I am not sure why `ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X;` `ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y;` is in `CenterWindow(IntPtr hHhildWnd)` as if the user has multiple screens and has his primary screen as something other than the left most the application could be in the negatives and you would want the message box to show up in the negative x position otherwise it will not come up in the center of the application if its on a screen the the left of the primary or below it for that matter in relation to needing a negative y. – Birdbuster Jun 10 '15 at 20:15
  • @Birdbuster did it really works for you? I tried this and the messagebox always appear in my top-left of my screen (not window) – Mirza Feb 24 '16 at 06:26
  • @Mirza Yes totally worked for me. You are using the versions where you pass in the owner window right? Might seem like a silly question but maybe just an over sight on your end? I also modified private static void CenterWindow(IntPtr hChildWnd) {} as stated in my comment so that if my window was in the negative x or y axis in relation to windows "main" screen it still showed up in the middle correctly. – Birdbuster Feb 25 '16 at 02:59
  • @Birdbuster my problem solved also, made some mistakes before – Mirza Feb 25 '16 at 04:36
  • 4
    That didn't work for me because the hook wasn't triggered, I had to replace `AppDomain.GetCurrentThreadId()` with `(int) GetCurrentThreadId()` (`[DllImport("kernel32.dll")] static extern IntPtr GetCurrentThreadId();`), that works for me. – Snicker Mar 07 '16 at 13:25
  • 2
    This is a horror approach. It would be easier to create your own WPF window that looks like a message box. This takes less time than understanding and debugging the above code. – Alexandru Dicu Nov 29 '19 at 00:14
9

This was answered here for Windows.Forms but with the following added to your class, you can get it to work in WPF.

You have to add a reference to System.Windows.Forms & System.Drawing for the class above to work and to do the following.

public partial class YourWPFWindow : Window, System.Windows.Forms.IWin32Window
{
    public IntPtr Handle
    {
        get { return new WindowInteropHelper(this).Handle; }
    }
}

Then you can call MessageBoxEx with:

MessageBoxEx.Show(this, "Message");

It should show up in the middle of your window.

Community
  • 1
  • 1
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
4

Another approach would be to create your own message box window and set window's startup location to center owner. The advantage is you can easily have your styles and customization, without hooking.

Rajeesh
  • 391
  • 3
  • 7
  • your should provide minimal code to support your answer. what ever you posted here, that can go as a comment but not an answer. – Raju Mar 10 '16 at 14:27
2

If you're open to using external libraries, one option is the Xceed WPF Toolkit which has a MessageBox that will center properly. It is also very consistent with WPF in general, e.g. it can be styled from XAML.

https://github.com/xceedsoftware/wpftoolkit/wiki/MessageBox

Usage

The MessageBox mimics the behavior of the System.Windows.MessageBox closely. You use similar syntax to create and show a message box.

MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK, MessageBoxImage.Question);

MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK);

MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox");

MessageBoxResult result =  Xceed.Wpf.Toolkit.MessageBox.Show("Hello world!");

The centering on parent window appears to be automatic.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
1

In addition to Framnk's answer

This part doesn't work on multiple screens

ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X;
ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y;

here's a quickfix:

var allScreens = System.Windows.Forms.Screen.AllScreens;
ptStart.X = allScreens.All(a => a.WorkingArea.Left > ptStart.X) ? allScreens.Min(a => a.WorkingArea.Left) : ptStart.X;
ptStart.X = allScreens.All(a => a.WorkingArea.Right - width < ptStart.X) ? allScreens.Max(a => a.WorkingArea.Right) - width : ptStart.X;
ptStart.Y = allScreens.All(a => a.WorkingArea.Top > ptStart.Y) ? allScreens.Min(a => a.WorkingArea.Top) : ptStart.Y;
ptStart.Y = allScreens.All(a => a.WorkingArea.Bottom - height < ptStart.Y) ? allScreens.Max(a => a.WorkingArea.Bottom) - height : ptStart.Y;
Community
  • 1
  • 1
chainerlt
  • 215
  • 3
  • 8
-8

If your app has several windows, you might want to use Application.Current.MainWindow:

MessageBox.Show(Application.Current.MainWindow, "Can't login with given names.", "Login Failure", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.Cancel);