是否可以调用静态 MessageBox
类 Show()
方法,使其没有任务栏图标或具有自定义图像?我正在尝试寻找构建自定义 MessageBox
类的替代方法。
谢谢。
我尝试通过以下方式使用 DefaultDesktopOnly
选项:
if (MessageBox.Show("Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
//Do stuff
}
但是任务栏中仍然有一个图标,主窗体也开始崩溃。如果重要的话,我在Win7中。 DefaultDesktopOnly
是否存在稳定性问题?
最佳答案
您需要为 MessageBox
提供一个所有者窗口,该窗口具有(或不具有)自身的图标,以使对话框不单独显示。如果您从打开的表单中调用 MessageBox
,您可以将表单作为第一个参数传递,使其成为所有者:
// Assume "this" is a form, not valid from any other class
if (MessageBox.Show(this, "Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
//Do stuff
}
但是如果你的程序此时没有其他可见的 GUI,你可以简单地创建一个虚拟表单来为它提供一个所有者,就像这样:
// A new, invisible form is created as the MessageBox owner, this prevents it from appearing in the taskbar
if (MessageBox.Show(new Form(), "Are you sure you would like to do something?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
//Do stuff
}
关于c# - 隐藏 .NET MessageBox 的任务栏图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20196410/