创建无模式消息框

发布于 2024-09-05 15:55:59 字数 94 浏览 9 评论 0原文

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

How might one go about creating a Modeless MessageBox? Do I have to just create my own Windows Form class and use that? If so, is there an easy way of adding a warning icon (rather than inserting my own image of one) and resizing based on text volume?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

阿楠 2024-09-12 15:56:06

//没有commnet

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

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

//no commnet

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

needs more threads and more code(I don't have enough time to write) for concurrent messageboxes.

错爱 2024-09-12 15:56:05

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

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

如果您确实需要,您可以监听按下“确定”按钮时触发的事件。

Short of writing the code, you could create a small form that in the constructor does the following

  • Takes a parameter string as the message to display
  • Fills up a label on the form with this string
  • Loads an icon with one of the following (pass in an Enum to the constructor)
    • SystemIcons.Application
    • SystemIcons.Asterix
    • SystemIcons.Error
    • SystemIcons.Exclamation
    • SystemIcons.Hand
    • SystemIcons.Information
    • SystemIcons.Question
    • SystemIcons.Shield
    • SystemIcons.Warning
    • SystemIcons.WinLogo
  • Calls Show() which will cause it to be a modal dialog

If you really wanted, you could listen to an event that is fired when the OK button is pushed.

戈亓 2024-09-12 15:56:05

您可以使用标准系统警告图标 SystemIcons< /代码>

You can use the standard system warning icon using SystemIcons

维持三分热 2024-09-12 15:56:05

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

对于图标,请使用

MessageBoxIcon.Warning

You have to either use form and call showDialog()

And for icons use

MessageBoxIcon.Warning

疯狂的代价 2024-09-12 15:56:05

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

这里是示例代码

if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
    // Do some stuff if yes pressed
}
else
{
    // no pressed
}

Note: this will create a Modal dialog box, which is not what the question is asking

here is a sample code

if (MessageBox.Show("Description of the message", "Caption text", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
{
    // Do some stuff if yes pressed
}
else
{
    // no pressed
}
毁梦 2024-09-12 15:56:04

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

    // 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.

If you need a message box that just displays itself while your code continues running in the background (the box is still modal and will prevent the user from using other windows until OK is clicked), you can always start the message box in its own thread and continue doing what ever you do in the original thread:

    // 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.
画中仙 2024-09-12 15:56:04

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

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

You'll have to create a Form and use Show() to display it Modeless. MessageBox.Show(...) behaved Modal as seen in the example by ghiboz; "Description of the message" is displayed until the user presses a button.

With MessageBox.Show(...) you get the result as soon as the messagebox is closed; with a modeless message box, your code will have to have a mechanism such as an event to react when the user eventually selects something on your message box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文