25

如何创建一个无模式消息框?我是否必须创建自己的 Windows 窗体类并使用它?如果是这样,是否有一种简单的方法可以添加警告图标(而不是插入我自己的图像)并根据文本量调整大小?

4

7 回答 7

54

如果您需要一个在代码继续在后台运行时仅显示自身的消息框(该框仍然是模态的,并且会阻止用户在单击“确定”之前使用其他窗口),您始终可以在其自己的线程中启动该消息框并继续做你在原始线程中所做的事情:

    // Do stuff before.
    // Start the message box -thread:
    new Thread(new ThreadStart(delegate
    {
      MessageBox.Show
      (
        "Hey user, stuff runs in the background!", 
        "Message",
        MessageBoxButtons.OK,
        MessageBoxIcon.Warning
      );
    })).Start();
    // Continue doing stuff while the message box is visible to the user.
    // The message box thread will end itself when the user clicks OK.
于 2011-05-17T12:21:07.153 回答
6

您必须创建一个表单并使用Show()无模式显示它。MessageBox.Show(...)在 ghiboz 的示例中表现出模态;“消息的描述”一直显示到用户按下按钮。

MessageBox.Show(...)一旦消息框关闭,您就会得到结果;使用无模式的消息框,您的代码将必须具有一种机制,例如当用户最终在您的消息框上选择某些内容时做出反应的事件。

于 2010-06-18T16:04:05.407 回答
1

如果不编写代码,您可以创建一个小表单,在构造函数中执行以下操作

  • 将参数字符串作为要显示的消息
  • 使用此字符串填充表单上的标签
  • 使用以下之一加载图标(将枚举传递给构造函数)
    • SystemIcons.Application
    • SystemIcons.Asterix
    • SystemIcons.Error
    • SystemIcons.Exclamation
    • SystemIcons.Hand
    • SystemIcons.Information
    • SystemIcons.Question
    • SystemIcons.Shield
    • SystemIcons.Warning
    • SystemIcons.WinLogo
  • 调用 Show() 这将使它成为一个模态对话框

如果你真的想要,你可以监听按下 OK 按钮时触发的事件。

于 2010-06-18T17:01:04.253 回答
0

您可以使用标准系统警告图标SystemIcons

于 2010-06-17T06:56:53.623 回答
0

您必须使用表单并调用 showDialog()

对于图标使用

MessageBoxIcon.Warning

于 2010-06-17T07:00:20.827 回答
-1

注意:这将创建一个模态对话框,这不是问题所要问的

这是一个示例代码

if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
    // Do some stuff if yes pressed
}
else
{
    // no pressed
}
于 2010-06-17T07:14:30.107 回答
-1

//没有通讯

object sync = new object();
ManualResetEvent Wait = new ManualResetEvent();
//you should create a place holder named MessageData for Message Data.
List<MessageData> Messages = new List<MessageData>();
internal void ShowMessage(string Test, string Title, ....)
{
    MessageData MSG = new MessageData(Test, Title);
    Wait.Set();
    lock(sync) Messages.Add(MSG);
}
// another thread should run here.
void Private_Show()
{
    while(true)
{
        while(Messsages.Count != 0)
        {
            MessageData md;
            lock(sync)
            {
                md = List[0];
                List.RemoveAt(0);
            }
            MessageBox.Show(md.Text, md.Title, md....);
        }
        Wait.WaitOne();
    }
}

并发消息框需要更多线程和更多代码(我没有足够的时间编写)。

于 2010-06-19T04:36:53.823 回答