当应用程序最小化到托盘时,C# MessageBox 显示在前面

标签 c# .net winforms messagebox system-tray

我有一些弹出消息框的代码:

MessageBox.Show(this,
                 "You have not inputted a username or password. Would you like to configure your settings now?",
                 "Settings Needed",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);

我的问题是当它弹出时我的应用程序通常最小化到托盘。因此,消息框不会出现在最前面,也不会出现在开始栏上。查看它的唯一方法是通过 alt-tabbing。

这是将我的应用程序(父应用程序)最小化到托盘的代码:

if (FormWindowState.Minimized == WindowState)
{                
      Hide();                
}

最佳答案

还有一个额外的标志,您可以将其指定为标准 Windows MessageBox function 的选项。未在 WinForms 包装器中公开。

您正在寻找的是 MB_TOPMOST,它确保消息框显示为桌面上所有其他内容的最顶层窗口。只需修改您的代码,如下所示:

MessageBox.Show(this,
                "You have not inputted a username or password. Would you like to configure your settings now?",
                "Settings Needed",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1,  // specify "Yes" as the default
                (MessageBoxOptions)0x40000);      // specify MB_TOPMOST

关于当应用程序最小化到托盘时,C# MessageBox 显示在前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834819/

相关文章:

c# - 无需使用 Visual Studio 2010 中的设计模式即可找到事件的正确参数

vb.net - 工具提示控件如何通过新属性增强表单上的所有控件?

c# - 如何从 Click 事件调用 Paint 方法?

c# - 在 Windows 窗体中显示 html 文本

c# - 是否有一个库可以使 C#/.NET/ASP.NET 中的 html 更容易

c# - ToList() - ArgumentNullException 处理

.net - 阅读有关导致 WPF 中的内存泄漏的数据绑定(bind)的资源?

c++ - .NET 和 native C++ 应用程序之间通信的最佳方式

C#支持值类型和引用类型,但它们都是对象吗?

c# - 具有可转换参数的通用函数