【问题标题】:Keep Messagebox.show() on top of other application using c#使用 c# 将 Messagebox.show() 保持在其他应用程序之上
【发布时间】:2009-08-03 06:26:24
【问题描述】:

如何使用 c# 将 Messagebox.show() 保持在其他应用程序之上??

【问题讨论】:

标签: c#


【解决方案1】:

我尝试了 donutboy 提供的解决方案,但它似乎不接受 0x40000(或 40000)作为 MessageBoxOptions Enum 值的有效选项。

但是我发现使用 MessageBoxOptions.DefaultDesktopOnly 具有相同的效果,并且在用户确认之前将 MessageBox 保持在顶部。 IE。

MessageBox.Show("Hello there", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);

这可能是提供的最简单的原生解决方案。

【讨论】:

  • 是的,我发现这个也可以!有几个建议,但我同意这是最容易实施的建议。
  • 正是我需要的!
  • 像魅力一样工作!这应该被接受为一种解决方案。
  • 虽然这行得通,但我最终还是使用了 Xceed MessageBox,因为它也在应用程序顶部居中。
【解决方案2】:

有一个更好的解决方案,无需创建新表单。

MessageBox.Show("Message Text", "Header", MessageBoxButtons.OK, MessageBoxIcon.None, 
     MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000);  // MB_TOPMOST

0x40000 是“MB_TOPMOST”-标志。

【讨论】:

    【解决方案3】:

    另一种简单的处理方法:

    MessageBox.Show(new Form { TopMost = true }, "This is TopMost", "TopMost", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    

    【讨论】:

    • 这是一个更好的解决方案,因为它可以在多屏幕系统上正常工作。
    • 完美运行
    【解决方案4】:

    使用“new Form { TopMost = true }”作为第一个参数的问题是它在完成后无法正确处理新表单。

    发现这个问题花了很多时间(几个星期)。唯一的症状是程序将在半小时后“无法响应”。完全锁定,必须使用附加的调试器或任务管理器将其杀死,没有可用的调试信息。

    要解决这个问题,你需要这样的东西:

            using (Form form = new Form {TopMost = true})
            { 
                var retval = MessageBox.Show(form, text, caption, ok, error);
                form.Dispose();
                return retval;
            }
         
    

    更好的是,编写自己的“MyMessageBox”类并使用它:

    公共静态类 MyMessageBox {

        public static DialogResult Show(string text, string caption, MessageBoxButtons ok, MessageBoxIcon error)
        {
            using (Form form = new Form {TopMost = true})
            { 
                var retval = MessageBox.Show(form, text, caption, ok, error);
                form.Dispose();
                return retval;
            }
            // return UseForm ? MessageBox.Show(form, text, caption, ok, error) : MessageBox.Show(text, caption, ok, error);
        }
        public static DialogResult Show(string text, string caption, MessageBoxButtons ok)
        {
            using (Form form = new Form { TopMost = true })
            {
                var retval = MessageBox.Show(form, text, caption, ok);
                form.Dispose();
                return retval;
            }
        }
        public static DialogResult Show( string text, string caption)
        {
            using (Form form = new Form { TopMost = true })
            {
                var retval = MessageBox.Show(form, text, caption);
                form.Dispose();
                return retval;
            }
        }
        public static DialogResult Show(string text)
        {
            using (Form form = new Form { TopMost = true })
            {
                var retval = MessageBox.Show(form, text);
                form.Dispose();
                return retval;
            }
    
        }
    

    }

    【讨论】:

      【解决方案5】:

      使用选项

      MessageBoxOptions.DefaultDesktopOnly

      【讨论】:

        【解决方案6】:

        根据戴夫的回答:

        WPF:

        MessageBox.Show(new Window { Topmost = true }, "Message", "Title");
        

        Windows 窗体:

        MessageBox.Show(new Form { TopMost = true }, "Message", "Title");
        

        【讨论】:

          猜你喜欢
          • 2011-01-27
          • 1970-01-01
          • 2011-03-20
          • 1970-01-01
          • 2012-05-03
          • 1970-01-01
          • 2022-11-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多