【问题标题】:How to customize message box如何自定义消息框
【发布时间】:2010-09-17 11:14:05
【问题描述】:

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

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

【问题讨论】:

  • 坏主意,具有一致外观的消息框很重要。缓和用户的“这到底是怎么回事”的响应,让她很可能会真正阅读该消息并单击正确的按钮。
  • @Hans Passant :这个问题是为了清除我对消息框的怀疑,这就是为什么被问到“这可能吗?”
  • 原则上我同意Hans Passant的观点。我认为消息框应该具有一致的外观。但这在当今意味着什么?毕竟有很多程序带有更复杂的消息框,尤其是关于框。如果按钮样式和前景色等内容在不同程序的消息框中略有不同,我认为大多数用户不会感到惊讶。

标签: c# .net winforms messagebox


【解决方案1】:

您无法重新设置默认 MessageBox 的样式,因为它依赖于当前的 Windows 操作系统主题,但是您可以轻松创建自己的 MessageBox。只需使用以下设置向您的项目添加一个新表单(即 MyNewMessageBox):

FormBorderStyle    FixedToolWindow
ShowInTaskBar      False
StartPosition      CenterScreen

要显示它,请使用myNewMessageBoxInstance.ShowDialog();。并在表单中添加标签和按钮,例如 OK 和 Cancel 并适当设置它们的 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();
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这是创建您自己的消息框所需的代码:

      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<Button> Buttons = null, PictureBox Image = null)
              {
                  List<Label> Labels = new List<Label>();
                  Labels.Add(Label);
                  return Show(Labels, Title, Buttons, Image);
              }
              public static string Show(string Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
              {
                  List<Label> Labels = new List<Label>();
                  Labels.Add(MyLabel.Set(Label));
                  return Show(Labels, Title, Buttons, Image);
              }
              public static string Show(List<Label> Labels = null, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
              {
                  if (Labels == null) Labels = new List<Label>();
                  if (Labels.Count == 0) Labels.Add(MyLabel.Set(""));
                  if (Buttons == null) Buttons = new List<Button>();
                  if (Buttons.Count == 0) Buttons.Add(MyButton.Set("OK"));
                  List<Button> buttons = new List<Button>(Buttons);
                  buttons.Reverse();
      
                  int ImageWidth = 0;
                  int ImageHeight = 0;
                  int LabelWidth = 0;
                  int LabelHeight = 0;
                  int ButtonWidth = 0;
                  int ButtonHeight = 0;
                  int TotalWidth = 0;
                  int TotalHeight = 0;
      
                  MyMessageBox mb = new MyMessageBox();
      
                  mb.Text = Title;
      
                  //Image
                  if (Image != null)
                  {
                      mb.Controls.Add(Image);
                      Image.MaximumSize = new Size(150, 300);
                      ImageWidth = Image.Width + Image.Margin.Horizontal;
                      ImageHeight = Image.Height + Image.Margin.Vertical;
                  }
      
                  //Labels
                  List<int> il = new List<int>();
                  mb.panText.Location = new Point(9 + ImageWidth, 9);
                  foreach (Label l in Labels)
                  {
                      mb.panText.Controls.Add(l);
                      l.Location = new Point(200, 50);
                      l.MaximumSize = new Size(480, 2000);
                      il.Add(l.Width);
                  }
                  int mw = Labels.Max(x => x.Width);
                  il.ToString();
                  Labels.ForEach(l => l.MinimumSize = new Size(Labels.Max(x => x.Width), 1));
                  mb.panText.Height = Labels.Sum(l => l.Height);
                  mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
                  mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
                  LabelWidth = mb.panText.Width;
                  LabelHeight = mb.panText.Height;
      
                  //Buttons
                  foreach (Button b in buttons)
                  {
                      mb.panButtons.Controls.Add(b);
                      b.Location = new Point(3, 3);
                      b.TabIndex = Buttons.FindIndex(i => i.Text == b.Text);
                      b.Click += new EventHandler(mb.Button_Click);
                  }
                  ButtonWidth = mb.panButtons.Width;
                  ButtonHeight = mb.panButtons.Height;
      
                  //Set Widths
                  if (ButtonWidth > ImageWidth + LabelWidth)
                  {
                      Labels.ForEach(l => l.MinimumSize = new Size(ButtonWidth - ImageWidth - mb.ScrollBarWidth(Labels), 1));
                      mb.panText.Height = Labels.Sum(l => l.Height);
                      mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
                      mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
                      LabelWidth = mb.panText.Width;
                      LabelHeight = mb.panText.Height;
                  }
                  TotalWidth = ImageWidth + LabelWidth;
      
                  //Set Height
                  TotalHeight = LabelHeight + ButtonHeight;
      
                  mb.panButtons.Location = new Point(TotalWidth - ButtonWidth + 9, mb.panText.Location.Y + mb.panText.Height);
      
                  mb.Size = new Size(TotalWidth + 25, TotalHeight + 47);
                  mb.ShowDialog();
                  return mb.Result;
              }
      
              private FlowLayoutPanel panText;
              private FlowLayoutPanel panButtons;
              private int ScrollBarWidth(List<Label> Labels)
              {
                  return (Labels.Sum(l => l.Height) > 300) ? 23 : 6;
              }
      
              private void Button_Click(object sender, EventArgs e)
              {
                  Result = ((Button)sender).Text;
                  Close();
              }
      
              private string Result = "";
          }
      }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多