c中的MessageBox#
问题描述:
我想向用户显示消息框,以便用户不能拒绝确认消息框。在确认消息框之前,不应允许用户在屏幕上执行其他任何操作。c中的MessageBox#
这是一个基于Windows的c#应用程序。
最主要的是,即使我使用Windows消息框。有时它隐藏在屏幕后面。但对于我的情况,我希望消息框在出现时最上面。
我正在使用一些其他第三方应用程序,它们在我的消息框上。我想克服这一点。
如何做到这一点...
答
您必须创建自己的形式,使用了模式,改变Z顺序,使其始终在最前面,并捕获所有的键盘和鼠标操作。
总在最前面:http://www.codeguru.com/cpp/w-d/dislog/article.php/c1857
答
在这篇文章看看
MessageBox.Show Examples in Windows Forms C#
编辑:
您还可以使用一种形式的最高性能,使其在所有窗口之上,在给定的应用。
How to: Keep a Windows Form on Top
要显示形式作为模态对话框呼叫ShowDialog的方法。
Form frmAbout = new Form();
frmAbout.ShowDialog();
答
这听起来像消息框被显示在另一个线程。您需要确保在主UI线程上调用MessageBox.Show。下面是说明的方式来实现这一目标的代码片段:
public class FooForm: Form
{
//This is just a button click handler that calls ShowMessage from another thread.
private void ButtonShowMessage_Click(object sender,EventArgs e)
{
//Use this to see that you can't interact with FooForm before closing the messagebox.
ThreadPool.QueueUserWorkItem(delegate{ ShowMessage("Hello World!");});
//Use this (uncomment) to see that you can interact with FooForm even though there is a messagebox.
//ThreadPool.QueueUserWorkItem(delegate{ MessageBox.Show("Hello World!");});
}
private void ShowMessage(string message)
{
if(InvokeRequire)
{
BeginInvoke(new MethodInvoker(() => MessageBox.Show(message)));
}
else
{
MessageBox.Show(message);
}
}
}
我假设你没有遇到的情况,你有多个UI线程,当其中一个会弹出一个消息框,你想要的消息框成为整个用户界面的模式。这是一个更复杂的情况。
该类型的消息框称为“系统模式”,对此的支持非常少,因为没有人喜欢禁用系统其余部分的应用程序。为什么你的应用程序会被允许执行此操作? – rein 2009-09-08 05:57:50