如何自定义消息框

我正在做C#应用程序,我想改变消息框的样式。 有可能吗?

示例:更改按钮样式,前景色等。

您不能重新设置默认的MessageBox,因为它依赖于当前的Windows操作系统主题,但您可以轻松创建自己的MessageBox。 只需使用以下设置将新表单(即MyNewMessageBox)添加到项目中:

FormBorderStyle FixedToolWindow ShowInTaskBar False StartPosition CenterScreen 

要显示它,请使用myNewMessageBoxInstance.ShowDialog(); 。 并在表单中添加标签和按钮,例如“确定”和“取消”,并相应地设置其DialogResults, MyNewMessageBox添加一个按钮并将其btnOK 。 将属性窗口中的DialogResult属性设置为DialogResult.OK 。 按下该按钮时,它将返回OK结果:

 MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox(); DialogResult result = myNewMessageBoxInstance.ShowDialog(); if (result == DialogResult.OK) { // etc } 

建议您添加自己的Show方法,该方法将获取您需要的文本和其他选项:

 public DialogResult Show(string text, Color foreColour) { lblText.Text = text; lblText.ForeColor = foreColour; return this.ShowDialog(); } 

MessageBox::Show使用来自user32.dll的函数,它的样式依赖于Windows,所以你不能这样改变它,你必须创建自己的表单

winform添加到项目中(我将其命名为frmShowMessage )并将此控件放在其上:

  1. Panel (pnlShowMessage) 2. Label (lblMessageText) 3. PictureBox (pictureBox1) 4. ImageList (imageList1) 

替换您的新表单代码如下:

 using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace CostumeMessageBox { public partial class frmShowMessage : Form { Button btnOk = new Button(); //Create object of Button. Button btnCancel = new Button(); Button btnNo = new Button(); Button btnYes = new Button(); public const int WM_NCLBUTTONDOWN = 0xA1; public const int HT_CAPTION = 0x2; [DllImportAttribute("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll")] public static extern bool ReleaseCapture(); public frmShowMessage() { InitializeComponent(); } ///  /// Here I am overriding Paint method of form object /// and set it's background color as gradient. Here I am /// using LinearGradientBrush class object to make gradient /// color which comes in System.Drawing.Drawing2D namespace. ///  ///  protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.DarkRed, 3, ButtonBorderStyle.Solid, Color.DarkRed, 3, ButtonBorderStyle.Solid, Color.DarkRed, 3, ButtonBorderStyle.Solid, Color.DarkRed, 3, ButtonBorderStyle.Solid); //Rectangle rect = this.ClientRectangle; //LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Snow, Color.SeaShell, 60); //LightCyan Lavender LightGray //e.Graphics.FillRectangle(brush, rect); //base.OnPaint(e); } private void frmShowMessage_Load(object sender, EventArgs e) { btnOk.Text = "تایید"; //Here we set text of Button. btnOk.DialogResult = DialogResult.OK; //Set DialogResult property of button. btnOk.FlatStyle = FlatStyle.Popup; //Set flat appearence of button. btnOk.BackColor = Color.Beige; btnOk.FlatAppearance.BorderSize = 0; btnCancel.Text = "انصراف"; btnCancel.DialogResult = DialogResult.Cancel; btnCancel.FlatStyle = FlatStyle.Popup; btnCancel.BackColor = Color.Beige; btnCancel.FlatAppearance.BorderSize = 0; btnNo.Text = "خیر"; btnNo.DialogResult = DialogResult.No; btnNo.FlatStyle = FlatStyle.Popup; btnNo.BackColor = Color.Beige; btnNo.FlatAppearance.BorderSize = 0; btnYes.Text = "بله"; btnYes.DialogResult = DialogResult.Yes; btnYes.FlatStyle = FlatStyle.Popup; btnYes.BackColor = Color.Beige; btnYes.FlatAppearance.BorderSize = 0; } ///  /// setMessage method is used to display message /// on form and it's height adjust automatically. /// I am displaying message in a Label control. ///  /// Message which needs to be displayed to user. private void setMessage(string messageText) { int number = Math.Abs(messageText.Length / 30); if (number != 0) { lblMessageText.Height = lblRightBorder.Height = number*25; Height = lblMessageText.Height; } lblMessageText.Text = messageText; } ///  /// This method is used to add button on message box. ///  /// MessageButton is type of enumMessageButton /// through which I get type of button which needs to be displayed. private void addButton(enumMessageButton MessageButton) { switch (MessageButton) { case enumMessageButton.OK: { //If type of enumButton is OK then we add OK button only. btnOk.SetBounds(pnlShowMessage.ClientSize.Width - 80, 5, 75, 25); // Set bounds of button. pnlShowMessage.Controls.Add(btnOk); //Finally Add button control on panel. } break; case enumMessageButton.OKCancel: { btnOk.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25); pnlShowMessage.Controls.Add(btnOk); btnCancel.SetBounds((pnlShowMessage.ClientSize.Width - (btnOk.ClientSize.Width + 5 + 80)), 5, 75, 25); pnlShowMessage.Controls.Add(btnCancel); } break; case enumMessageButton.YesNo: { btnNo.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25); pnlShowMessage.Controls.Add(btnNo); btnYes.SetBounds((pnlShowMessage.ClientSize.Width - (btnNo.ClientSize.Width + 5 + 80)), 5, 75, 25); pnlShowMessage.Controls.Add(btnYes); } break; case enumMessageButton.YesNoCancel: { btnCancel.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25); pnlShowMessage.Controls.Add(btnCancel); btnNo.SetBounds((pnlShowMessage.ClientSize.Width - (btnCancel.ClientSize.Width + 5 + 80)), 5, 75, 25); pnlShowMessage.Controls.Add(btnNo); btnYes.SetBounds((pnlShowMessage.ClientSize.Width - (btnCancel.ClientSize.Width + btnNo.ClientSize.Width + 10 + 80)), 5, 75, 25); pnlShowMessage.Controls.Add(btnYes); } break; } } ///  /// We can use this method to add image on message box. /// I had taken all images in ImageList control so that /// I can eaily add images. Image is displayed in /// PictureBox control. ///  /// Type of image to be displayed. private void addIconImage(enumMessageIcon MessageIcon) { switch (MessageIcon) { case enumMessageIcon.Error: pictureBox1.Image = imageList1.Images["Error"]; //Error is key name in imagelist control which uniqly identified images in ImageList control. break; case enumMessageIcon.Information: pictureBox1.Image = imageList1.Images["Information"]; break; case enumMessageIcon.Question: pictureBox1.Image = imageList1.Images["Question"]; break; case enumMessageIcon.Warning: pictureBox1.Image = imageList1.Images["Warning"]; break; } } #region Overloaded Show message to display message box. ///  /// Show method is overloaded which is used to display message /// and this is static method so that we don't need to create /// object of this class to call this method. ///  ///  internal static DialogResult Show(string messageText) { frmShowMessage frmMessage = new frmShowMessage(); frmMessage.setMessage(messageText); frmMessage.addIconImage(enumMessageIcon.Information); frmMessage.addButton(enumMessageButton.OK); frmMessage.ShowDialog(); return frmMessage.DialogResult; } internal static DialogResult Show(string messageText, string messageTitle) { frmShowMessage frmMessage = new frmShowMessage(); frmMessage.Text = messageTitle; frmMessage.setMessage(messageText); frmMessage.addIconImage(enumMessageIcon.Information); frmMessage.addButton(enumMessageButton.OK); frmMessage.ShowDialog(); return frmMessage.DialogResult; } internal static DialogResult Show(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton) { frmShowMessage frmMessage = new frmShowMessage(); frmMessage.setMessage(messageText); frmMessage.Text = messageTitle; frmMessage.addIconImage(messageIcon); frmMessage.addButton(messageButton); frmMessage.ShowDialog(); return frmMessage.DialogResult; } #endregion private void frmShowMessage_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); } } #region constant defiend in form of enumration which is used in showMessage class. internal enum enumMessageIcon { Error, Warning, Information, Question, } internal enum enumMessageButton { OK, YesNo, YesNoCancel, OKCancel } #endregion } 

现在,您可以在项目中调用Customized MessageBox ,如下所示:

  private void btnQuestion_Click(object sender, EventArgs e) { frmShowMessage.Show(rch.Text, "Message Text", enumMessageIcon.Question, enumMessageButton.YesNoCancel); } private void btnInformation_Click(object sender, EventArgs e) { frmShowMessage.Show(rch.Text, "Message Text", enumMessageIcon.Information, enumMessageButton.OKCancel); } private void btnError_Click(object sender, EventArgs e) { frmShowMessage.Show(rch.Text, "Message Text", enumMessageIcon.Error, enumMessageButton.OK); } private void btnWarning_Click(object sender, EventArgs e) { frmShowMessage.Show(rch.Text, "Message Text", enumMessageIcon.Warning, enumMessageButton.YesNo); } 

我已经忘记了我从中学到的这个链接。

以下是创建自己的消息框所需的代码:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyStuff { public class MyLabel : Label { public static Label Set(string Text = "", Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color()) { Label l = new Label(); l.Text = Text; l.Font = (Font == null) ? new Font("Calibri", 12) : Font; l.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor; l.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor; l.AutoSize = true; return l; } } public class MyButton : Button { public static Button Set(string Text = "", int Width = 102, int Height = 30, Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color()) { Button b = new Button(); b.Text = Text; b.Width = Width; b.Height = Height; b.Font = (Font == null) ? new Font("Calibri", 12) : Font; b.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor; b.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor; b.UseVisualStyleBackColor = (b.BackColor == SystemColors.Control); return b; } } public class MyImage : PictureBox { public static PictureBox Set(string ImagePath = null, int Width = 60, int Height = 60) { PictureBox i = new PictureBox(); if (ImagePath != null) { i.BackgroundImageLayout = ImageLayout.Zoom; i.Location = new Point(9, 9); i.Margin = new Padding(3, 3, 2, 3); i.Size = new Size(Width, Height); i.TabStop = false; i.Visible = true; i.BackgroundImage = Image.FromFile(ImagePath); } else { i.Visible = true; i.Size = new Size(0, 0); } return i; } } public partial class MyMessageBox : Form { private MyMessageBox() { this.panText = new FlowLayoutPanel(); this.panButtons = new FlowLayoutPanel(); this.SuspendLayout(); // // panText // this.panText.Parent = this; this.panText.AutoScroll = true; this.panText.AutoSize = true; this.panText.AutoSizeMode = AutoSizeMode.GrowAndShrink; //this.panText.Location = new Point(90, 90); this.panText.Margin = new Padding(0); this.panText.MaximumSize = new Size(500, 300); this.panText.MinimumSize = new Size(108, 50); this.panText.Size = new Size(108, 50); // // panButtons // this.panButtons.AutoSize = true; this.panButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink; this.panButtons.FlowDirection = FlowDirection.RightToLeft; this.panButtons.Location = new Point(89, 89); this.panButtons.Margin = new Padding(0); this.panButtons.MaximumSize = new Size(580, 150); this.panButtons.MinimumSize = new Size(108, 0); this.panButtons.Size = new Size(108, 35); // // MyMessageBox // this.AutoScaleDimensions = new SizeF(8F, 19F); this.AutoScaleMode = AutoScaleMode.Font; this.ClientSize = new Size(206, 133); this.Controls.Add(this.panButtons); this.Controls.Add(this.panText); this.Font = new Font("Calibri", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = FormBorderStyle.FixedSingle; this.Margin = new Padding(4); this.MaximizeBox = false; this.MinimizeBox = false; this.MinimumSize = new Size(168, 132); this.Name = "MyMessageBox"; this.ShowIcon = false; this.ShowInTaskbar = false; this.StartPosition = FormStartPosition.CenterScreen; this.ResumeLayout(false); this.PerformLayout(); } public static string Show(Label Label, string Title = "", List