c# dialogresult 选择文件_C#控件美化之路(12):自定义消息弹出框

3c8d91f479781d45c4807de00e580ff7.png

在软件开发中,很多地方需要用到与用户消息提醒,用form绘制一个自己消息框。

首先在界面上绘制两个按钮,在之前文章有教程绘制一个button方法,也可以直接使用系统。

私有属性

        #region 私有属性        private string message;        private MesBoxIcon mesIcon;        #endregion

公有属性

        #region 公有属性        public MesBoxIcon MesIcon        {            get => mesIcon;            set            {                mesIcon = value;                this.Invalidate();                if (value == MesBoxIcon.Info)                {                    wenImageButton1.Visible = false;                }            }        }        public string Message        {            get => message;            set            {                message = value;                Graphics g = this.CreateGraphics();                SizeF sizef = g.MeasureString(value, this.Font);                int width =(int) sizef.Width + this.FrameWidth * 2 + 80;                int height = this.TitleHeight + this.FrameWidth + panel1.Height;                this.Size = new Size(this.Width < width ? width : this.Width, this.Height < height ? height : this.Height);                this.Invalidate();            }        }        #endregion

MesBoxIcon 消息类型选择

2321bcbbdb74d2e3c72b8826b21fc0cd.png

找到与自己需要绘制的图档

        public enum MesBoxIcon        {            Asterisk,            Error,            Info,            Warning,        }

采用GDI绘制消息框

protected override void OnPaint(PaintEventArgs e)        {            base.OnPaint(e);            Rectangle rec = new Rectangle(this.FrameWidth, this.TitleHeight, this.Width - this.FrameWidth * 2, this.Height - this.TitleHeight - this.FrameWidth - panel1.Height);            Rectangle recStr = new Rectangle(rec.X + 70, rec.Y, rec.Width - 70, rec.Height);            Rectangle recIco = new Rectangle(rec.X + 5, rec.Y + (rec.Height - 60) / 2, 60, 60);            Graphics g = e.Graphics;            g.DrawString(Message, Font, new SolidBrush(this.ForeColor), recStr, WenSkin.Controls.ControlHelper.StringConters);            switch (MesIcon)            {                case MesBoxIcon.Asterisk:                    g.DrawImage(Properties.Resources.Asterisk, recIco);                    break;                case MesBoxIcon.Error:                    g.DrawImage(Properties.Resources.error, recIco);                    break;                case MesBoxIcon.Info:                    g.DrawImage(Properties.Resources.Info, recIco);                    break;                case MesBoxIcon.Warning:                    g.DrawImage(Properties.Resources.Warning, recIco);                    break;                default:                    break;            }        }

两个按钮点击事件

        private void wenImageButton2_Click(object sender, EventArgs e)        {            this.DialogResult = DialogResult.OK;        }        private void wenImageButton1_Click(object sender, EventArgs e)        {            this.DialogResult = DialogResult.Cancel;        }

可以根据自己需求更改。

多个构造函数示例

        public MesBox()        {            InitializeComponent();            this.SizeChanged += (s, e) =>            {                if (wenImageButton1 != null)                    wenImageButton1.Width = (this.Width - this.FrameWidth * 2) / 2;            };            Text = "消息";            mesIcon = MesBoxIcon.Info;            this.StartPosition = FormStartPosition.CenterScreen;        }        public MesBox(string text) : this()        {            Message = text;        }        public MesBox(string text,MesBoxIcon mesBoxIcon) : this(text)        {            MesIcon = mesBoxIcon;            switch (MesIcon)            {                case MesBoxIcon.Asterisk:                    Text = "提醒";                    break;                case MesBoxIcon.Error:                    Text = "错误";                    break;                case MesBoxIcon.Info:                    Text = "消息";                    break;                case MesBoxIcon.Warning:                    Text = "警告";                    break;                default:                    break;            }        }        public MesBox(string text,string caption, MesBoxIcon mesBoxIcon) : this(text)        {            Text = caption;        }

至此,一个自主弹窗绘制完成

接下来调用

            var m = new MesBox(text, MesBox.MesBoxIcon.Asterisk).ShowDialog();            if (m == DialogResult.OK)                 true;            else                 false;

一行代码即可,是不是很方便。

关注文林软控,带你一起C# 美化.NET 控件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值