开发者

C# MessageBox To Front When App is Minimized To Tray

I have some code that popups a message box:

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);

My problem is when this pops up my app is usually minimized to the tray. As a result the messagebox doesn't come to the front and it also doesnt appear along the start bar. The only way of seeing it is by alt-tabbing.

Here is the code that minimizes my app (the parent) to the tray:

if (FormWindowState.Minimized == WindowState)
{                
      Hide();                
}
开发者_JAVA百科


There's an additional flag you can specify as an option to the standard Windows MessageBox function that isn't exposed in the WinForms wrapper.

What you're looking for is called MB_TOPMOST, which ensures that the message box is displayed as the top-most window over everything else on your desktop. Simply amend your code as shown below:

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


you can try like this

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);


I only needed this for testing, so if you don't mind being extra cheesy, MessageBoxOptions.ServiceNotification will do the trick...

        MessageBox.Show(message,
            "Error",
            MessageBoxButton.YesNo,
            MessageBoxImage.Exclamation,
            MessageBoxResult.OK,
            MessageBoxOptions.ServiceNotification);


MessageBox on top of all windows (no tray icon):

MessageBox.Show(new Form() { TopMost = true }, boxText, "Box Title",
                MessageBoxButtons.OK, boxIcon);

MessageBox and your app on top of all windows (no tray icon):

TopMost = true;
MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon);
TopMost = false;

MessageBox on top of all windows, plus tray icon (app loses focus):

MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon, 0,
                MessageBoxOptions.DefaultDesktopOnly);
// (The "0" can also be "MessageBoxDefaultButton.Button1".)

MessageBoxButtons.OK and boxIcon are optional arguments in the first two.

Setting TopLevel doesn't do anthing; it is already true.

There is no direct way to center a MessageBox on its parent form. (Except maybe centering the parent form.)


The more correct way to do this is to set the owner of the MessageBox

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜