我有一些像这样编码的 MessageBox:
MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Message","Title", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
举个更好的例子,我为 FormClosing 事件这样做:
private void Example_FormClosing(object sender, FormClosingEventArgs e){
MessageBox.Show(new Form(){TopMost=true, TopLevel=True}, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
但是,几乎每次我都必须在看到消息框之前更改计算机上的 Window(例如返回 Visual Studio)时,这对用户来说并不友好,而且真的很烦人。
我确认我的主要形式不在 TopMost=true 中,我只尝试了 TopMost 或 TopLevel,StartPosition=FormStartPosition.CenterScreen 但没有任何效果。
[更新]
我试过:
private void Example_FormClosing(object sender, FormClosingEventArgs e){
MessageBox.Show(this.Owner, "Really close?"," Program", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
我希望将我的 messageBox 放在窗口的前面,而不必切换窗口即可看到它,因为它就像在当前窗口的后面。
你有解决这个问题的想法吗?
最佳答案
这样做:
MessageBox.Show(
"Message",
"Title",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
它将把它放在所有其他窗口的前面,包括来自其他进程的窗口(我认为这是您所要求的)。
关键参数是MessageBoxOptions.DefaultDesktopOnly
.请注意,这将使消息框成为默认桌面的父级,导致调用 MessageBox.Show()
的应用程序失去焦点。
(您真的应该为关键消息保留此行为。)
或者,如果您的应用程序有一个窗口,请在显示消息框之前调用 this.BringToFront()
,方法是调用 MessageBox.Show() 并将第一个参数设置为 this
。 (您可以从窗口窗体类中调用它)。
关于C#、Windows 窗体、顶部的消息框无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901047/