c# 制作闹钟示例

摘要:
1.Form1表单属性this。FormBorderStyle=FormBorderStyle。没有一个2.添加事件privatepointMyDrawClock(inth,intm,int){Graphicsg=this.CreateGraphics();Rectanglerect=this.ClientRectangle;this.FormBorderStyle=FormBorderStyle

1. Form1窗体的属性 this.FormBorderStyle = FormBorderStyle.None;

2.添加事件

private void MyDrawClock(int h, int m, int s)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = this.ClientRectangle;
            this.FormBorderStyle = FormBorderStyle.None;
            g.Clear(Color.White);

            Pen myPen = new Pen(Color.Black, 1);
            //g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 50, this.ClientRectangle.Height / 2 - 50, 100, 100);//画表盘

            Point centerPoints = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点

            for (int i = 0; i < 12; i++)
            {
                Point sp12 = new Point((int)(centerPoints.X + (Math.Sin(i*5 * Math.PI / 30) * 44)), (int)(centerPoints.Y - (Math.Cos(i*5 * Math.PI / 30) * 44)));
                Point sp121 = new Point((int)(sp12.X + (Math.Sin(i*5 * Math.PI / 30) * 5)), (int)(sp12.Y - (Math.Cos(i*5 * Math.PI / 30) * 5)));
                myPen = new Pen(Color.Gray, 1);
                if (i % 3 == 0)
                {
                    myPen = new Pen(Color.Gray, 2);
                }
                g.DrawLine(myPen, sp12, sp121);
            }

            int hh = ((int)(m / 10)) + h * 5;
            Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点
            //计算出秒针,时针,分针的另外一个商点
            //Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50)));
            Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 40)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 40)));
            //Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360));
            Point newhourPoint = new Point((int)(centerPoint.X + (Math.Sin(hh * Math.PI / 30) * 30)),
                (int)(centerPoint.Y - (Math.Cos(hh * Math.PI / 30) * 30)));
            //以不同颜色和宽度绘制表针
            //myPen = new Pen(Color.Red, 1);
            //g.DrawLine(myPen, centerPoint, secPoint);
            myPen = new Pen(Color.Orange, 1);
            g.DrawLine(myPen, centerPoint, minPoint);
            myPen = new Pen(Color.Goldenrod, 2);
            g.DrawLine(myPen, centerPoint, newhourPoint);
            g.FillEllipse(new SolidBrush(Color.OrangeRed), (this.ClientRectangle.Width / 2) - 3, (this.ClientRectangle.Height / 2) - 3, 6, 6);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            MyDrawClock(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            this.TransparencyKey = Color.White;
            base.OnPaint(e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            MyDrawClock(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
        }

3.添加拖动窗体事件

bool ismove = false;
        int _x;
        int _y;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            _x = e.X;
            _y = e.Y;
            ismove = true;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (ismove)
            {
                int dx = e.X - _x;
                int dy = e.Y - _y;
                this.Location = new Point(this.Left + dx, this.Top + dy);
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            ismove = false;
        }

4.效果图:

c# 制作闹钟示例第1张

还可以改装一下

替换方法代码:

        private void MyDrawClock(int h, int m, int s)
        {
            Graphics g = this.CreateGraphics();
            Rectangle rect = this.ClientRectangle;

            g.Clear(Color.Green);

            g.FillRectangle(new SolidBrush(Color.White), 60, 40, 34, 40);
            g.DrawRectangle(new Pen(Color.Black), 60, 40, 34, 40);
            g.DrawString("闹钟.exe", new Font("宋体", 10, FontStyle.Regular), new SolidBrush(Color.White), 50, 86);

            Pen myPen = new Pen(Color.Black, 1);
            //g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 50, this.ClientRectangle.Height / 2 - 50, 100, 100);//画表盘

            Point centerPoints = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点

            for (int i = 0; i < 12; i++)
            {
                Point sp12 = new Point((int)(centerPoints.X + (Math.Sin(i*5 * Math.PI / 30) * 12)), (int)(centerPoints.Y - (Math.Cos(i*5 * Math.PI / 30) * 12)));
                Point sp121 = new Point((int)(sp12.X + (Math.Sin(i*5 * Math.PI / 30) * 2)), (int)(sp12.Y - (Math.Cos(i*5 * Math.PI / 30) * 2)));
                myPen = new Pen(Color.Gray, 1);
                if (i % 3 == 0)
                {
                    myPen = new Pen(Color.Black, 2);
                }
                g.DrawLine(myPen, sp12, sp121);
            }

            int hh = ((int)(m / 10)) + h * 5;
            Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点
            //计算出秒针,时针,分针的另外一个商点
            //Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50)));
            Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 10)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 10)));
            //Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360));
            Point newhourPoint = new Point((int)(centerPoint.X + (Math.Sin(hh * Math.PI / 30) * 8)),
                (int)(centerPoint.Y - (Math.Cos(hh * Math.PI / 30) * 8)));
            //以不同颜色和宽度绘制表针
            //myPen = new Pen(Color.Red, 1);
            //g.DrawLine(myPen, centerPoint, secPoint);
            myPen = new Pen(Color.Orange, 1);
            g.DrawLine(myPen, centerPoint, minPoint);
            myPen = new Pen(Color.Goldenrod, 2);
            g.DrawLine(myPen, centerPoint, newhourPoint);
            g.FillEllipse(new SolidBrush(Color.OrangeRed), (this.ClientRectangle.Width / 2) - 1, (this.ClientRectangle.Height / 2) - 1, 3, 3);
            
        }

效果图(模仿文件形式):

c# 制作闹钟示例第2张

免责声明:文章转载自《c# 制作闹钟示例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇借助Proxifier实现内网访问写一个播放视频文件的ActiveX控件——MFC版(原创)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

uniApp之 顶部选项卡

为了在uniapp插件中创建类似于信息应用程序模板的功能,使用了官方的组件刷。起初,它无法滚动。后来,我看了一下官方网站,说有必要添加“滚动视图”标签,以记录第一次使用uniapp的应用程序。首先,在顶部制作一个选项卡,因为我只有两个项目,所以我将它们直接写入视图标记中{item.label}}然后编写以下内容。单击和滑动可以切换选项卡,所选样式:curre...

"SQLserver 事务日志已满"解决方法

如果不够,备份后换个地方存[注:tempdb你数据库名称。...

excel下划线转驼峰公式

最近,为了避免麻烦,我决定生成jquery的json对象结构。数据表。下划线应变成驼峰,如下所示:=LEFT&MID...

汇编指令MOV

格式:MOVDST,SRC例如:MOVEAX,#050aH;将十六进制050a传送到通用寄存器eax中MOVDI,BXMOVES,AXMOVAX,DSMOVAL,23HMOV[2000H],02HMOV[2061H],BX...

一分钟制作U盘版BT3

一分钟生产BT3U磁盘版本方便、快捷、简单、无效且不可退款。BT3磁盘版本,大约694MB,可以直接烧录,然后用CD引导进入BT3。连接如下:http://ftp.heanet.ie/mirrors/backtrack/bt3-final.isoU磁盘版本Bt3,约783MB,连接为:http://cesium.di.uminho.pt/pub/backtr...

十四、ES开启密码认证

所以我们需要为es head和kibana添加密码认证。4、 为kibana设置密码。1.为kibana配置证书。因为kibana和es之间的连接也需要证书加密通信。mkdir-p/etc/kibana/certscp/etc/selastic search/certs-*/etc/kibana/certs/2.授予kibana主要权限。权限必须为kiban...