欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-04 09:27:49
public partial class mymessagebox : form { // 自动关闭的时间限制,如3为3秒后自动关闭 private int...
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);
        }

        /// 
        /// 
        /// 
        ///窗体大小
        ///圆角大小
        /// 
        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;
            }
        }
}