C# 在所有表单顶部显示 MessageBox,设置位置和/或颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11910448/
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
Displaying a MessageBox on top of all forms, setting location and/or color
提问by modest and cute girl
I have two forms and I set one of the forms' TopMostproperty to true. Somewhere, while the program runs, I show a MessageBox, but since TopMost is set to true, when the MessageBox pops up it shows under the topmost form so I cannot see it.
我有两个表单,我将其中一个表单的TopMost属性设置为 true。在某个地方,当程序运行时,我显示了一个 MessageBox,但由于 TopMost 设置为 true,当 MessageBox 弹出时,它显示在最顶层的表单下,所以我看不到它。
Is there any way that I make one of my forms always be on top, but when a MessageBox pops up, make the message box show on top of that specific form?
Is it possible to give a location to the MessageBox so that it shows not in the middle but for example low down on the screen?
Let's say that I have an orange colored form can I have a pink colored message box only for that specific application. I mean I am NOT referring to playing the windows color properties. (I don't want allmessage boxes to be pink.)
有什么方法可以让我的一个表单始终位于顶部,但是当弹出 MessageBox 时,让消息框显示在该特定表单的顶部?
是否可以为 MessageBox 指定一个位置,使其不显示在中间,而是显示在屏幕的下方?
假设我有一个橙色的表单,我可以有一个仅用于该特定应用程序的粉红色消息框。我的意思是我不是指播放 Windows 颜色属性。(我不希望所有消息框都是粉红色。)
采纳答案by quetzalcoatl
1) The MessageBox.Show method has an overloadthat takes a first parameter of Window type. If you use that overload instead of just Show(string), ie.:
1) MessageBox.Show 方法有一个重载,它采用 Window 类型的第一个参数。如果您使用该重载而不仅仅是 Show(string),即:
class MyForm : Form {
void method(){
MessageBox.Show(this, "blablablablabla");
}
}
then the MessageBox will show up in a 'modal' mode and it will be exactly on top on that form. Now just ensure that you pass that topmost form and you're done. Side effect is that the 'modal' mode will cause the Messagebox to BLOCK the original window until the message is dismissed.
然后 MessageBox 将显示在“模态”模式中,并且它恰好位于该表单的顶部。现在只需确保您通过了最顶层的表单,您就完成了。副作用是“模态”模式将导致消息框阻塞原始窗口,直到消息被解除。
2) No, that is not possible directly. However, you can play hard with .Net and get a "handle" to the messagebox and then move the window via P/Invoke to some WinApi functions, but I recommend you not.
2)不,这是不可能直接的。但是,您可以努力使用 .Net 并获得消息框的“句柄”,然后通过 P/Invoke 将窗口移动到某些 WinApi 函数,但我不建议您这样做。
3) No, that's just not possible with MessageBoxes
3) 不,这在 MessageBoxes 中是不可能的
What you want to achieve in (2) and (3) is not possible, because the MsgBox is meant to be simple. To get that things you will have to write your own tiny form that will act as a message box, and present that form instead of the message box. That form will be able to have any styling, any position and any behaviour you like.
您想要在 (2) 和 (3) 中实现的目标是不可能的,因为 MsgBox 是简单的。要获得这些东西,您必须编写自己的小表单作为消息框,并显示该表单而不是消息框。该表单将能够具有您喜欢的任何样式、任何位置和任何行为。
回答by saber
I think there is no built-in feature to do that in .Net, but I suggest you to keep a reference of your TopMost form, and change it before showing each message, something like following :
我认为 .Net 中没有内置功能可以做到这一点,但我建议您保留 TopMost 表单的引用,并在显示每条消息之前对其进行更改,如下所示:
public static void ShowMessage(string message)
{
Component.InstanceOfTopMost.TopMost = false;
MessageBox.Show(message);
Component.InstanceOfTopMost.TopMost = true;
}
Componentis a static class which is holds a reference of your form which should be TopMost. The reason of this static class is you may want to use that form in several places, this way you can easily access to your Form.
This is a simple method, you can change it based on your requirements.
Component是一个静态类,它包含您的表单的引用,它应该是 TopMost。这个静态类的原因是您可能希望在多个地方使用该表单,这样您就可以轻松访问您的表单。这是一种简单的方法,您可以根据自己的要求进行更改。
Update :
更新 :
public class Component
{
public static Form2 InstanceOfTopMost { get; set; }
}
Component is just a name give another name to that, because there is another .Net class named Component.
Component 只是一个名字,给它另一个名字,因为还有另一个名为 Component 的 .Net 类。
var frm = new Form2();
Component.InstanceOfTopMost = frm;
frm.Show();
Hope this help.
希望这有帮助。
回答by quetzalcoatl
@Saber Amani: why so? look, it just works:
@Sabre Amani:为什么会这样?看,它只是有效:
using System.Windows.Forms;
namespace ReusingUserControlsSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
Form1 second = new Form1();
second.TopMost = true;
second.Show();
MessageBox.Show(second, "BLARGH");
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(178, 201);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button button1;
}
}
The MSG is properly shown over the second form, which is TopMost. The only "problem" is to knowwhich form is the topmost.
MSG 正确显示在第二种形式上,即 TopMost。唯一的“问题”是要知道哪种形式是最顶层的。
回答by Joel
A simple approach for a top most MessageBoxwould be something like this:
一个最简单的方法MessageBox是这样的:
using (var dummy = new Form() { TopMost = true })
{
MessageBox.Show(dummy, text, title);
}
You don't have to actually display the dummy form.
您不必实际显示虚拟表单。
回答by Chris
To show a MessageBoxon top of all the other forms of your application (including those with TopMostset) you can use the Show()method overload that takes a parameter of type MessageBoxOptionsand pass MessageBoxOptions.ServiceNotification as that parameter.
要MessageBox在应用程序的所有其他形式(包括带有TopMostset 的形式)之上显示 a ,您可以使用Show()采用类型参数的方法重载MessageBoxOptions并将 MessageBoxOptions.ServiceNotification 作为该参数传递。
DialogResult result = MessageBox.Show("Configuration file was corrupted.\n\nDo you want to reset it to default and lose all configurations?", "Config File Corrupted", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification);
回答by Kim Ki Won
I use this.
我用这个。
MessageBox.Show(
"message",
"title",
MessageBoxButtons.OK,
messageBoxIcon,
MessageBoxDefaultButton.Button1,
(MessageBoxOptions)0x40000); // this set TopMost

