winform重写的messagebox(简易版,小白可能有用)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weepingisgood/article/details/80425810

因看不惯Windows自带的messagebox,决定自己做个,搜索了一下csdn、博客园、codeproject,在网友的一个版本做了一点点改良,效果图如下

提示文本



选择文本




附上主要代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public enum MessageBoxStyle
    {
        info = 0,
        question = 1,
        error = 2
    };

    public partial class frmMessageBox : Form
    {
        public frmMessageBox(MessageBoxStyle messageBoxStyle, string msg)
        {
            InitializeComponent();

            if (messageBoxStyle == MessageBoxStyle.info)
            {
                picICO.Image = global::WindowsFormsApplication2.Properties.Resources.info;
                this.Text = "提示";
                panel1.Visible = true;
                panel2.Visible = false;
            }
            else if (messageBoxStyle == MessageBoxStyle.question)
            {
                picICO.Image = global::WindowsFormsApplication2.Properties.Resources.question;
                this.Text = "询问";
                panel1.Visible = false;
                panel2.Visible = true;
            }
            else if (messageBoxStyle == MessageBoxStyle.error)
            {
                picICO.Image = global::WindowsFormsApplication2.Properties.Resources.error;
                this.Text = "错误";
                panel1.Visible = true;
                panel2.Visible = false;
            }

            int tempWidth = this.Width - labInfo.Width;
            if (msg.Length < 90)
            {
                this.labInfo.Text = msg;
            }
            else
            {
                this.labInfo.Text = msg.Substring(0, 90) + "......";
            }
            SizeF size = TextRenderer.MeasureText(msg, new Font("宋体", 12, FontStyle.Regular));

            int TempWidth = (int)size.Width;

            if ((size.Width + tempWidth + 4) <= this.Size.Width)
            {
                return;
            }
            else
            {
                this.Width = (int)(tempWidth + size.Width + 4);
                labInfo.Width = (int)size.Width + 4;
                panel1.Width = (int)size.Width + 4;
                panel2.Width = (int)size.Width + 4;

                int x = labInfo.Width / 2 - btnNo.Width - 10;
                int y = btnYes.Location.Y;
                btnYes.Location = new Point(x,y);

                x = labInfo.Width / 2  + 10;
                y = btnNo.Location.Y;
                btnNo.Location = new Point(x, y);
            }
        }
    }
}

源码下载地址:https://download.csdn.net/download/weepingisgood/10433409

如果本文侵犯了你的权益,请留言告知,我将予以删除。


展开阅读全文

没有更多推荐了,返回首页