Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何自定义消息框_C#_.net_Winforms_Messagebox - Fatal编程技术网

C# 如何自定义消息框

C# 如何自定义消息框,c#,.net,winforms,messagebox,C#,.net,Winforms,Messagebox,我正在做C#应用程序,我想更改消息框的样式。有没有可能 示例:更改按钮样式、前景色等。您无法重新设置默认MessageBox的样式,因为这取决于当前Windows OS主题,但是您可以轻松创建自己的MessageBox。只需使用以下设置向项目中添加新表单(即MyNewMessageBox): FormBorderStyle FixedToolWindow ShowInTaskBar False StartPosition CenterScreen 要显示它,请使用my

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


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

您无法重新设置默认MessageBox的样式,因为这取决于当前Windows OS主题,但是您可以轻松创建自己的MessageBox。只需使用以下设置向项目中添加新表单(即MyNewMessageBox):

FormBorderStyle    FixedToolWindow
ShowInTaskBar      False
StartPosition      CenterScreen
要显示它,请使用
myNewMessageBoxInstance.ShowDialog()。并将标签和按钮添加到表单中,例如“确定”和“取消”,并适当设置其对话框结果,即将按钮添加到
MyNewMessageBox
并称之为
btnOK
。将属性窗口中的
DialogResult
属性设置为
DialogResult.OK
。按下该按钮时,将返回OK结果:

MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox();
DialogResult result = myNewMessageBoxInstance.ShowDialog();
if (result == DialogResult.OK)
{
    // etc
}
建议添加您自己的显示方法,该方法采用您需要的文本和其他选项:

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

MessageBox::Show
使用user32.dll中的函数,其样式取决于Windows,因此您不能这样更改,您必须创建自己的表单

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

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 = "";
    }
}   
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间MyStuff
{
公共类MyLabel:标签
{
公共静态标签集(string Text=”“,Font-Font=null,Color-ForeColor=new-Color(),Color-BackColor=new-Color())
{
标签l=新标签();
l、 文本=文本;
l、 字体=(字体==空)?新字体(“Calibri”,12):字体;
l、 前景色=(前景色==新颜色())?颜色。黑色:前景色;
l、 BackColor=(BackColor==newcolor())?系统颜色。控件:BackColor;
l、 AutoSize=true;
返回l;
}
}
公共类MyButton:按钮
{
公共静态按钮集(string Text=“”,int-Width=102,int-Height=30,Font-Font=null,Color-ForeColor=new-Color(),Color-BackColor=new-Color())
{
按钮b=新按钮();
b、 文本=文本;
b、 宽度=宽度;
b、 高度=高度;
b、 字体=(字体==空)?新字体(“Calibri”,12):字体;
b、 前景色=(前景色==新颜色())?颜色。黑色:前景色;
b、 BackColor=(BackColor==newcolor())?系统颜色。控件:BackColor;
b、 UseVisualStyleBackColor=(b.BackColor==SystemColor.Control);
返回b;
}
}
公共类MyImage:PictureBox
{
公共静态PictureBox集(字符串ImagePath=null,int-Width=60,int-Height=60)
{
PictureBox i=新PictureBox();
if(ImagePath!=null)
{
i、 BackgroundImageLayout=ImageLayout.Zoom;
i、 位置=新点(9,9);
i、 边距=新填充(3,3,2,3);
i、 尺寸=新尺寸(宽度、高度);
i、 TabStop=false;
i、 可见=真实;
i、 BackgroundImage=Image.FromFile(ImagePath);
}
其他的
{
i、 可见=真实;
i、 尺寸=新尺寸(0,0);
}
返回i;
}
}
公共部分类MyMessageBox:表单
{
私有MyMessageBox()
{
this.panText=新的FlowLayoutPanel();
this.panButtons=新的FlowLayoutPanel();
这个.SuspendLayout();
// 
//泛文本
// 
this.panText.Parent=此;
this.panText.AutoScroll=true;
this.panText.AutoSize=true;
this.panText.AutoSizeMode=AutoSizeMode.growthandshrink;
//this.panText.Location=新点(90,90);
this.panText.Margin=新填充(0);
this.panText.MaximumSize=新尺寸(500300);
this.panText.MinimumSize=新尺寸(108,50);
this.panText.Size=新尺寸(108,50);
// 
//按钮
// 
this.panButtons.AutoSize=true;
this.panButtons.AutoSizeMode=AutoSizeMode.growtandshrink;
this.panButtons.FlowDirection=FlowDirection.RightToLeft;
this.panButtons.Location=新点(89,89);
this.panButtons.Margin=新填充(0);
this.panButtons.MaximumSize=新尺寸(580150);
this.panButtons.MinimumSize=新尺寸(108,0);
this.panButtons.Size=新尺寸(108,35);
// 
//MyMessageBox
// 
此.AutoScaleDimensions=新尺寸(8F,19F);
this.AutoScaleMode=AutoScaleMode.Font;
this.ClientSize=新尺寸(206133);
this.Controls.Add(this.panbutions);
this.Controls.Add(this.panText);
this.Font=新字体(“Calibri”,12F,FontStyle.Regular,GraphicsUnit.Point,((字节)(0));
this.FormBorderStyle=FormBorderStyle.FixedSingle;
此边距=新填充(4);
this.ebox=false;
this.ebox=false;
this.MinimumSize=新尺寸(168132);
this.Name=“MyMessageBox”;
this.ShowIcon=false;
this.ShowInTaskbar=false;
this.StartPosition=FormStartPosition.CenterScreen;
此选项为.resume布局(false);
这个。执行布局();
}
公共静态字符串显示(标签标签,字符串标题=”,列表按钮=null,PictureBox图像=null)
{
列表标签=新列表();
标签。添加(标签);
返回显示(标签、标题、按钮、图像);
}
公共静态字符串显示(字符串标签,字符串标题=”,列表按钮=null,PictureBox图像=null)
{
列表标签=n