在所有表单上显示MessageBox,设置位置和/或颜色
我有两种forms,我将其中一种forms’ TopMost
属性设置为true。 在某个地方,当程序运行时,我显示一个MessageBox,但由于TopMost设置为true,当MessageBox弹出时,它显示在最顶层的表单下,所以我看不到它。
-
有没有什么方法可以使我的表单中的一个始终位于顶部,但是当弹出MessageBox时,将消息框显示在该特定表单的顶部?
-
是否可以为MessageBox提供一个位置,使其不显示在中间但例如屏幕上的低位?
-
假设我有一个橙色的表单,我可以为这个特定的应用程序设置一个粉红色的消息框。 我的意思是我不是指播放Windows颜色属性。 (我不希望所有的消息框都是粉红色的。)
1)MessageBox.Show方法有一个重载 ,它接受Window类型的第一个参数。 如果你使用那个重载而不仅仅是Show(string),即:
class MyForm : Form { void method(){ MessageBox.Show(this, "blablablablabla"); } }
然后MessageBox将以“模态”模式显示,它将完全位于该表单的顶部。 现在只需确保您通过最顶层的表格即可完成。 副作用是’模态’模式将导致Messagebox阻塞原始窗口,直到消息被解除。
2)不,这是不可能直接的。 但是,您可以使用.Net轻松玩游戏并获取消息框的“句柄”,然后通过P / Invoke将窗口移动到某些WinApi函数,但我建议您不要。
3)不,这对MessageBoxes来说是不可能的
您希望在(2)和(3)中实现的目标是不可能的,因为MsgBox意味着简单。 为了得到这些东西,你将不得不编写自己的小forms作为消息框,并呈现该forms而不是消息框。 该表单将具有任何样式,任何位置和您喜欢的任何行为。
我用这个。
MessageBox.Show( "message", "title", MessageBoxButtons.OK, messageBoxIcon, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); // this set TopMost
对于最顶级的MessageBox
,一个简单的方法是这样的:
using (var dummy = new Form() { TopMost = true }) { MessageBox.Show(dummy, text, title); }
您不必实际显示虚拟表单。
要在应用程序的所有其他forms(包括具有TopMost
集的那些forms)之上显示MessageBox
,您可以使用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);
@Saber 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; } }
MSG在第二种forms上正确显示,即TopMost。 唯一的“问题”是知道哪种forms是最顶层的。
我认为.Net中没有内置function,但我建议您保留TopMost表单的引用,并在显示每条消息之前更改它,如下所示:
public static void ShowMessage(string message) { Component.InstanceOfTopMost.TopMost = false; MessageBox.Show(message); Component.InstanceOfTopMost.TopMost = true; }
Component
是一个静态类,它包含表单的引用,该引用应该是TopMost。 这个静态类的原因是您可能希望在多个地方使用该表单,这样您就可以轻松访问您的表单。 这是一种简单的方法,您可以根据自己的要求进行更改。
更新:
public class Component { public static Form2 InstanceOfTopMost { get; set; } }
Component只是一个名称,给出了另一个名称,因为还有另一个名为Component的.Net类。
var frm = new Form2(); Component.InstanceOfTopMost = frm; frm.Show();
希望这有帮助。