Winform圆角窗体,timer倒计时关闭窗体

public partial class MyMessageBox : Form
{
        // 自动关闭的时间限制,如3为3秒后自动关闭
        private int _second;

        public int Second
        {
            get { return _second; }
            set { _second = value; }
        }
        // 计数器,用以判断当前窗口弹出后持续的时间
        private int counter;
        public MyMessageBox(string message)
        {
            InitializeComponent();
            this._second = Second;
            // 初始化计数器
            this.counter = 0;
            this.label1.Text = message;
            this.buttonEx1.Text = string.Format("确定({0})", this._second - this.counter);
            // 激活并启动timer,设置timer的触发间隔为1000毫秒(1秒)
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void buttonEx1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.counter < this._second)
            {
                // 刷新按钮的文本
                this.buttonEx1.Text = string.Format("确定({0})", this._second - this.counter - 1);
                this.Refresh();
                // 计数器自增
                this.counter++;
            }
            // 如果到达时间限制
            else
            {
                // 关闭timer
                this.timer1.Enabled = false;
                this.timer1.Stop();
                // 关闭对话框
                this.Close();
            }
        }

        // "画圆角"
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 10);
            this.Region = new Region(FormPath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect">窗体大小</param>
        /// <param name="radius">圆角大小</param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            path.AddArc(arcRect, 180, 90);//左上角

            arcRect.X = rect.Right - diameter;//右上角
            path.AddArc(arcRect, 270, 90);

            arcRect.Y = rect.Bottom - diameter;// 右下角
            path.AddArc(arcRect, 0, 90);

            arcRect.X = rect.Left;// 左下角
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }
        private void MyMessageBox_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                SetWindowRegion();
            }
            else
            {
                this.Region = null;
            }
        }
}

猜你喜欢

转载自blog.csdn.net/PLF_1994/article/details/81562261