C#伪模态对话框

效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

功能

  1. 可以再父窗口中添加用户点击确定和取消的按钮事件
  2. 模态对话框的作用
  3. 调用函数就可以显示该模态对话框
  4. 显示内容随时变化

用法

在这里插入图片描述

实现代码

命名空间

using System;
using System.Windows.Forms;
using System.Drawing;

代码


    public class MessageTip : Panel
    {
        private void button_init(Button button)
        {
            button.BackColor = Color.White;
            button.FlatStyle = FlatStyle.Flat;
            button.Font = new Font("微软雅黑", 10);
            button.FlatAppearance.BorderColor = Color.Black;
            button.FlatAppearance.MouseOverBackColor = Color.Black;
            button.FlatAppearance.MouseDownBackColor = Color.Black;
            button.MouseEnter += button_WinSignIn_SignIn_MouseEnter;
            button.MouseLeave += button_Lev;
        }
        private void button_WinSignIn_SignIn_MouseEnter(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.ForeColor = Color.White;
        }
        private void button_Lev(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.ForeColor = Color.Black;
        }


        public event EventHandler sure;
        private void sure_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            if (sure != null)
            {
                sure(sender, e);
            }
        }
        public event EventHandler nosure;
        private void nosure_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            if (nosure != null)
            {
                nosure(sender, e);
            }
        }
        public void ShowMessage(string tips)
        {
            labels[0].Text = tips;
            labels[0].Location = new Point(this.Width / 2 - labels[0].Width / 2, this.Height / 2 - labels[0].Height / 2 - 50);
            buttons[0].Location = new Point(this.Width/2-buttons[0].Width-40, labels[0].Location.Y + labels[0].Height + 50);
            buttons[1].Location = new Point(buttons[0].Location.X + buttons[0].Width + 80, buttons[0].Location.Y);
            this.Visible = true;
            this.BringToFront();
        }

        Label[] labels = new Label[1];
        Button[] buttons = new Button[2];

        public MessageTip(int messageW,int messageH,int messageX,int messageY)
        {
            this.Width = messageW;
            this.Height = messageH;
            this.BackColor = Color.White;
            this.Visible = false;
            this.Location = new Point(messageX, messageY);

            labels[0] = new Label();
            labels[0].AutoSize = true;
            labels[0].Text = "NULL";
            labels[0].Font = new Font("微软雅黑", 14);
            labels[0].Location = new Point(this.Width / 2 - labels[0].Width / 2, this.Height/2 - labels[0].Height/2);
            this.Controls.Add(labels[0]);

            buttons[0] = new Button();
            buttons[1] = new Button();
            buttons[0].Text = "确定";
            buttons[1].Text = "取消";
            button_init(buttons[1]);
            button_init(buttons[0]);
            buttons[1].Font = new Font("微软雅黑", 10);
            buttons[0].Font = new Font("微软雅黑", 10);
            buttons[1].Width = 100;
            buttons[0].Width = 100;
            buttons[1].Height = 30;
            buttons[0].Height = 30;
            buttons[0].Location = new Point(labels[0].Location.X - 50, labels[0].Location.Y + labels[0].Height + 30);
            buttons[1].Location = new Point(buttons[0].Location.X + buttons[0].Width + 80, buttons[0].Location.Y);
            buttons[0].Click += sure_Click;
            buttons[1].Click += nosure_Click;
            this.Controls.Add(buttons[0]);
            this.Controls.Add(buttons[1]);

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值