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

Luk*_*ina 8 .net c# system-tray messagebox winforms

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

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);
Run Code Online (Sandbox Code Playgroud)

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

以下代码可以最小化我的应用程序(父级)到托盘:

if (FormWindowState.Minimized == WindowState)
{                
      Hide();                
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*ray 29

您可以指定一个额外的标志作为标准Windows MessageBox函数的一个选项,该函数未在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
Run Code Online (Sandbox Code Playgroud)


ani*_*key 18

你可以这样试试

MessageBox.Show(new Form() { TopMost = true }, "You have not inputted a username or password. Would you like to configure your settings now?",
                 "Settings Needed",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);
Run Code Online (Sandbox Code Playgroud)

  • @nextgenneo:没有理由你只需要创建一个新表单来显示一个消息框.请参阅我的答案以获得更好的解决方 (不,它不一定会杀死表格.它仍然在后台运行.) (3认同)

Ste*_*eve 5

我只需要这个来测试,所以如果你不介意额外的俗气,MessageBoxOptions.ServiceNotification 会做的伎俩......

        MessageBox.Show(message,
            "Error",
            MessageBoxButton.YesNo,
            MessageBoxImage.Exclamation,
            MessageBoxResult.OK,
            MessageBoxOptions.ServiceNotification);
Run Code Online (Sandbox Code Playgroud)