winform 关于Messagebox自动定时关闭和自定义提示框总按钮上文本的问题的整理

如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实现,因为C#没有对Messagebox提供相关的关闭方法。第一种方法(自定义winform窗口实现):  [DllImport("user32.dll", CharSet =
摘要由CSDN通过智能技术生成

如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实现,因为C#没有对Messagebox提供相关的关闭方法。

第一种方法(自定义winform窗口实现):

 

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private extern static bool MessageBeep(uint type);

        [DllImport("Shell32.dll")]
        public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons);

        private IntPtr[] largeIcon;
        private IntPtr[] smallIcon;

        private MyMsgBox newMessageBox;
        private Label frmTitle;
        private Label frmMessage;
        private PictureBox pIcon;
        private FlowLayoutPanel flpButtons;
        private Icon frmIcon;

        private Button btnOK;
        private Button btnAbort;
        private Button btnRetry;
        private Button btnIgnore;
        private Button btnCancel;
        private Button btnYes;
        private Button btnNo;

        private DialogResult CYReturnButton;
        /// <summary>
        /// 对话框图标
        /// </summary>
        public enum MyIcon
        {
            Error,
            Explorer,
            Find,
            Information,
            Mail,
            Media,
            Print,
            Question,
            RecycleBinEmpty,
            RecycleBinFull,
            Stop,
            User,
            Warning
        }
        /// <summary>
        /// 对话框上的按钮
        /// </summary>
        public enum MyButtons
        {
            AbortRetryIgnore,
            OK,
            OKCancel,
            RetryCancel,
            YesNo,
            YesNoCancel
        }
        /// <summary>
        /// 绘制对话框
        /// </summary>
        /// <param name="title">对话框标题</param>
        private void BuildMessageBox(string title)
        {
            newMessageBox = new MyMsgBox();
            newMessageBox.Text = title;
            newMessageBox.Size = new System.Drawing.Size(400, 200);
            newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            newMessageBox.Paint += new PaintEventHandler(newMessageBox_Paint);
            newMessageBox.BackColor = System.Drawing.Color.White;

            TableLayoutPanel tlp = new TableLayoutPanel();
            tlp.RowCount = 3;
            tlp.ColumnCount = 0;
            tlp.Dock = System.Windows.Forms.DockStyle.Fill;
            tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22));
            tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50));
            tlp.BackColor = System.Drawing.Color.Transparent;
            tlp.Padding = new Padding(2, 5, 2, 2);

            frmTitle = new Label();
            frmTitle.Dock = System.Windows.Forms.DockStyle.Fill;
            frmTitle.BackColor = System.Drawing.Color.Transparent;
            frmTitle.ForeColor = System.Drawing.Color.White;
            frmTitle.Font = new Font("Tahoma", 9, FontStyle.Bold);

            frmMessage = new Label();
            frmMessage.Dock = System.Windows.Forms.DockStyle.Fill;
            frmMessage.BackColor = System.Drawing.Color.White;
            frmMessage.Font = new Font("Tahoma", 9, FontStyle.Regular);
            frmMessage.Text = "hiii";

            largeIcon = new IntPtr[250];
            smallIcon = new IntPtr[250];
            pIcon = new PictureBox();
            ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250);

            flpButtons = new FlowLayoutPanel();
            flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            flpButtons.Padding = new Padding(0, 5, 5, 0);
            flpButtons.Dock = System.Windows.Forms.DockStyle.Fill;
            flpButtons.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);

            TableLayoutPanel tlpMessagePanel = new TableLayoutPanel();
            tlpMessagePanel.BackColor = System.Drawing.Color.White;
            tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill;
            tlpMessagePanel.ColumnCount = 2;
            tlpMessagePanel.RowCount = 0;
            tlpMessagePanel.Padding = new Padding(4, 5, 4, 4);
            tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50));
            tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            tlpMessagePanel.Controls.Add(pIcon);
            tlpMessagePanel.Controls.Add(frmMessage);

            tlp.Controls.Add(frmTitle);
            tlp.Controls.Add(tlpMessagePanel);
            tlp.Controls.Add(flpButtons);
            newMessageBox.Controls.Add(tlp);
        }
        /// <summary>
        /// 显现对话框
        /// </summary>
        /// <param name="timeout">自动关闭时间,以毫秒为单位,如果设置为0则不自动关闭</param>
        /// <param name="Message">对话框中显示的提示内容</param>
        /// <returns></returns>
        public DialogResult ShowMsg(int timeout, string Message)
        {
            if (timeout > 0)
            {
                StartTimer(timeout);
            }
            BuildMessageBox("");
            frmMessage.Text = Message;
            ShowOKButton();
            newMessageBox.ShowDialog();
            return CYReturnButton;
        }
        /// <summary>
        /// 显示对话框
   
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值