TopMost WPF style MessageBox

In my VSTO Excel Addin application I wish to show a Systems.Window.MessageBox as TopMost window when the user attempt to close any opened workbook – just like Excel does if you try to close a workbook while the “Move or Copy…” small window is displayed. It would be simpler to use Systems.Window.Forms.MessageBox however I wish to keep the “WPF style” of the Systems.Window.MessageBox.

I found this question on stackoverflow /questions/16105097/why-isnt-messagebox-topmost so I further extended this code – as per https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox. I have not included the modality of the dialog box and the additional options. It also has an issue: if working on a multi-screen setup, the MessageBox will be displayed as top most on the main display, even if Excel is shown on the other display. Here’s the helper class.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
namespace MyAddIn
{
public class MessageBoxHelperClass
{
[DllImport("user32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)]
public static extern int MessageBoxUser32(IntPtr hWnd, string text, string caption, uint type);
private const uint MB_TOPMOST = 0x00040000;
private const uint MB_OK = 0x00000000;
private const uint MB_OKCANCEL = 0x00000001;
private const uint MB_YESNOCANCEL = 0x00000003;
private const uint MB_YESNO = 0x00000004;
private const uint MB_ICONERROR = 0x00000010;
private const uint MB_ICONQUESTION = 0x00000020;
private const uint MB_ICONEXCLAMATION = 0x00000030;
private const uint MB_ICONINFORMATION = 0x00000040;
private const uint MB_DEFBUTTON1 = 0x00000000;
private const uint MB_DEFBUTTON2 = 0x00000100;
private const uint MB_DEFBUTTON3 = 0x00000200;
public static MessageBoxResult ShowMessageBox(string message, bool topMost = true, string title = null, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None)
{
uint type = topMost ? MB_TOPMOST : 0;
// Add button flags
switch (buttons)
{
case MessageBoxButton.OK:
type |= MB_OK;
break;
case MessageBoxButton.OKCancel:
type |= MB_OKCANCEL;
break;
case MessageBoxButton.YesNoCancel:
type |= MB_YESNOCANCEL;
break;
case MessageBoxButton.YesNo:
type |= MB_YESNO;
break;
}
// Add icon flags
switch (icon)
{
case MessageBoxImage.Error:
type |= MB_ICONERROR;
break;
case MessageBoxImage.Question:
type |= MB_ICONQUESTION;
break;
case MessageBoxImage.Exclamation:
type |= MB_ICONEXCLAMATION;
break;
case MessageBoxImage.Information:
type |= MB_ICONINFORMATION;
break;
}
// Add default button flags
switch (defaultResult)
{
case MessageBoxResult.OK:
type |= MB_DEFBUTTON1;
break;
case MessageBoxResult.Cancel:
if (buttons == MessageBoxButton.YesNoCancel)
{
type |= MB_DEFBUTTON2;
}
else
{
type |= MB_DEFBUTTON1;
}
break;
case MessageBoxResult.Yes:
type |= MB_DEFBUTTON1;
break;
case MessageBoxResult.No:
type |= MB_DEFBUTTON2;
break;
}
IntPtr hwnd = IntPtr.Zero;
if (System.Windows.Application.Current != null && System.Windows.Application.Current.MainWindow != null)
{
WindowInteropHelper wih = new WindowInteropHelper(System.Windows.Application.Current.MainWindow);
hwnd = wih.Handle;
}
int result = 0;
if (topMost)
{
result = MessageBoxUser32(hwnd, message, title ?? string.Empty, type);
}
else
{
System.Windows.MessageBoxResult wpfResult = System.Windows.MessageBox.Show(message, title ?? string.Empty, buttons, icon);
return ConvertWpfToCustomMessageBoxResult(wpfResult);
}
return ConvertWin32ToCustomMessageBoxResult(result, buttons);
}
private static MessageBoxResult ConvertWin32ToCustomMessageBoxResult(int win32Result, MessageBoxButton buttons)
{
switch (win32Result)
{
case 1: // IDOK
return MessageBoxResult.OK;
case 2: // IDCANCEL
return MessageBoxResult.Cancel;
case 6: // IDYES
return MessageBoxResult.Yes;
case 7: // IDNO
return MessageBoxResult.No;
default:
return MessageBoxResult.None;
}
}
private static MessageBoxResult ConvertWpfToCustomMessageBoxResult(MessageBoxResult wpfResult)
{
switch (wpfResult)
{
case System.Windows.MessageBoxResult.OK:
return MessageBoxResult.OK;
case System.Windows.MessageBoxResult.Cancel:
return MessageBoxResult.Cancel;
case System.Windows.MessageBoxResult.Yes:
return MessageBoxResult.Yes;
case System.Windows.MessageBoxResult.No:
return MessageBoxResult.No;
default:
return MessageBoxResult.None;
}
}
}
}
</code>
<code>using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Forms; using System.Windows.Interop; namespace MyAddIn { public class MessageBoxHelperClass { [DllImport("user32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)] public static extern int MessageBoxUser32(IntPtr hWnd, string text, string caption, uint type); private const uint MB_TOPMOST = 0x00040000; private const uint MB_OK = 0x00000000; private const uint MB_OKCANCEL = 0x00000001; private const uint MB_YESNOCANCEL = 0x00000003; private const uint MB_YESNO = 0x00000004; private const uint MB_ICONERROR = 0x00000010; private const uint MB_ICONQUESTION = 0x00000020; private const uint MB_ICONEXCLAMATION = 0x00000030; private const uint MB_ICONINFORMATION = 0x00000040; private const uint MB_DEFBUTTON1 = 0x00000000; private const uint MB_DEFBUTTON2 = 0x00000100; private const uint MB_DEFBUTTON3 = 0x00000200; public static MessageBoxResult ShowMessageBox(string message, bool topMost = true, string title = null, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None) { uint type = topMost ? MB_TOPMOST : 0; // Add button flags switch (buttons) { case MessageBoxButton.OK: type |= MB_OK; break; case MessageBoxButton.OKCancel: type |= MB_OKCANCEL; break; case MessageBoxButton.YesNoCancel: type |= MB_YESNOCANCEL; break; case MessageBoxButton.YesNo: type |= MB_YESNO; break; } // Add icon flags switch (icon) { case MessageBoxImage.Error: type |= MB_ICONERROR; break; case MessageBoxImage.Question: type |= MB_ICONQUESTION; break; case MessageBoxImage.Exclamation: type |= MB_ICONEXCLAMATION; break; case MessageBoxImage.Information: type |= MB_ICONINFORMATION; break; } // Add default button flags switch (defaultResult) { case MessageBoxResult.OK: type |= MB_DEFBUTTON1; break; case MessageBoxResult.Cancel: if (buttons == MessageBoxButton.YesNoCancel) { type |= MB_DEFBUTTON2; } else { type |= MB_DEFBUTTON1; } break; case MessageBoxResult.Yes: type |= MB_DEFBUTTON1; break; case MessageBoxResult.No: type |= MB_DEFBUTTON2; break; } IntPtr hwnd = IntPtr.Zero; if (System.Windows.Application.Current != null && System.Windows.Application.Current.MainWindow != null) { WindowInteropHelper wih = new WindowInteropHelper(System.Windows.Application.Current.MainWindow); hwnd = wih.Handle; } int result = 0; if (topMost) { result = MessageBoxUser32(hwnd, message, title ?? string.Empty, type); } else { System.Windows.MessageBoxResult wpfResult = System.Windows.MessageBox.Show(message, title ?? string.Empty, buttons, icon); return ConvertWpfToCustomMessageBoxResult(wpfResult); } return ConvertWin32ToCustomMessageBoxResult(result, buttons); } private static MessageBoxResult ConvertWin32ToCustomMessageBoxResult(int win32Result, MessageBoxButton buttons) { switch (win32Result) { case 1: // IDOK return MessageBoxResult.OK; case 2: // IDCANCEL return MessageBoxResult.Cancel; case 6: // IDYES return MessageBoxResult.Yes; case 7: // IDNO return MessageBoxResult.No; default: return MessageBoxResult.None; } } private static MessageBoxResult ConvertWpfToCustomMessageBoxResult(MessageBoxResult wpfResult) { switch (wpfResult) { case System.Windows.MessageBoxResult.OK: return MessageBoxResult.OK; case System.Windows.MessageBoxResult.Cancel: return MessageBoxResult.Cancel; case System.Windows.MessageBoxResult.Yes: return MessageBoxResult.Yes; case System.Windows.MessageBoxResult.No: return MessageBoxResult.No; default: return MessageBoxResult.None; } } } } </code>
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;


namespace MyAddIn
{
    public class MessageBoxHelperClass
    {
        [DllImport("user32.dll", EntryPoint = "MessageBox", CharSet = CharSet.Auto)]
        public static extern int MessageBoxUser32(IntPtr hWnd, string text, string caption, uint type);

        private const uint MB_TOPMOST = 0x00040000;

        private const uint MB_OK = 0x00000000;
        private const uint MB_OKCANCEL = 0x00000001;
        private const uint MB_YESNOCANCEL = 0x00000003;
        private const uint MB_YESNO = 0x00000004;

        private const uint MB_ICONERROR = 0x00000010;
        private const uint MB_ICONQUESTION = 0x00000020;
        private const uint MB_ICONEXCLAMATION = 0x00000030;
        private const uint MB_ICONINFORMATION = 0x00000040;

        private const uint MB_DEFBUTTON1 = 0x00000000;
        private const uint MB_DEFBUTTON2 = 0x00000100;
        private const uint MB_DEFBUTTON3 = 0x00000200;

        public static MessageBoxResult ShowMessageBox(string message, bool topMost = true, string title = null, MessageBoxButton buttons = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None)
        {
            uint type = topMost ? MB_TOPMOST : 0;

            // Add button flags
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    type |= MB_OK;
                    break;
                case MessageBoxButton.OKCancel:
                    type |= MB_OKCANCEL;
                    break;
                case MessageBoxButton.YesNoCancel:
                    type |= MB_YESNOCANCEL;
                    break;
                case MessageBoxButton.YesNo:
                    type |= MB_YESNO;
                    break;
            }

            // Add icon flags
            switch (icon)
            {
                case MessageBoxImage.Error:
                    type |= MB_ICONERROR;
                    break;
                case MessageBoxImage.Question:
                    type |= MB_ICONQUESTION;
                    break;
                case MessageBoxImage.Exclamation:
                    type |= MB_ICONEXCLAMATION;
                    break;
                case MessageBoxImage.Information:
                    type |= MB_ICONINFORMATION;
                    break;
            }

            // Add default button flags
            switch (defaultResult)
            {
                case MessageBoxResult.OK:
                    type |= MB_DEFBUTTON1;
                    break;
                case MessageBoxResult.Cancel:
                    if (buttons == MessageBoxButton.YesNoCancel)
                    {
                        type |= MB_DEFBUTTON2;
                    }
                    else
                    {
                        type |= MB_DEFBUTTON1;
                    }
                    break;
                case MessageBoxResult.Yes:
                    type |= MB_DEFBUTTON1;
                    break;
                case MessageBoxResult.No:
                    type |= MB_DEFBUTTON2;
                    break;
            }

            IntPtr hwnd = IntPtr.Zero;

            if (System.Windows.Application.Current != null && System.Windows.Application.Current.MainWindow != null)
            {
                WindowInteropHelper wih = new WindowInteropHelper(System.Windows.Application.Current.MainWindow);
                hwnd = wih.Handle;
            }

            int result = 0;

            if (topMost)
            {
                result = MessageBoxUser32(hwnd, message, title ?? string.Empty, type);
            }
            else
            {
                System.Windows.MessageBoxResult wpfResult = System.Windows.MessageBox.Show(message, title ?? string.Empty, buttons, icon);
                return ConvertWpfToCustomMessageBoxResult(wpfResult);
            }

            return ConvertWin32ToCustomMessageBoxResult(result, buttons);
        }

        private static MessageBoxResult ConvertWin32ToCustomMessageBoxResult(int win32Result, MessageBoxButton buttons)
        {
            switch (win32Result)
            {
                case 1: // IDOK
                    return MessageBoxResult.OK;
                case 2: // IDCANCEL
                    return MessageBoxResult.Cancel;
                case 6: // IDYES
                    return MessageBoxResult.Yes;
                case 7: // IDNO
                    return MessageBoxResult.No;
                default:
                    return MessageBoxResult.None;
            }
        }

        private static MessageBoxResult ConvertWpfToCustomMessageBoxResult(MessageBoxResult wpfResult)
        {
            switch (wpfResult)
            {
                case System.Windows.MessageBoxResult.OK:
                    return MessageBoxResult.OK;
                case System.Windows.MessageBoxResult.Cancel:
                    return MessageBoxResult.Cancel;
                case System.Windows.MessageBoxResult.Yes:
                    return MessageBoxResult.Yes;
                case System.Windows.MessageBoxResult.No:
                    return MessageBoxResult.No;
                default:
                    return MessageBoxResult.None;
            }
        }
    }
}

I have this solution that is mostly working – just need to figure out how to display the MessageBox on the correct display. However, I was wondering whether there was a simpler way to achieve that.

Many thanks for your support/ideas.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật