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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1220882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:49:08  来源:igfitidea点击:

Keep Messagebox.show() on top of other application using c#

c#

提问by Anuya

How to keep a Messagebox.show() on top of other application using c# ??

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

回答by donutboy

There's a better solution, without creating a new form.

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

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

The 0x40000 is the "MB_TOPMOST"-Flag.

0x40000 是“MB_TOPMOST”标志。

回答by MikeDub

I tried the solution provided by donutboy and it doesn't seem to accept 0x40000 (or 40000) as a valid option as a MessageBoxOptions Enum value.

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

However I have found that using MessageBoxOptions.DefaultDesktopOnly has the same effect and keeps the MessageBox on top until it is confirmed by the user. ie.

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

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

This is likely the simplest native solution on offer.

这可能是最简单的本地解决方案。

回答by Dave

Another easy way to handle this:

处理此问题的另一种简单方法:

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