C#如何实现图形界面的时钟
导读:本文共3797字符,通常情况下阅读需要13分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 秒针有跳跃两个格子问题,主要是算法耗时没考虑在TimeTicker的触发事件内,导致程序运行有延迟。时间运行正确(获取的系统时间)。usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usi... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。秒针有跳跃两个格子问题,主要是算法耗时没考虑在TimeTicker的触发事件内,导致程序运行有延迟。
时间运行正确(获取的系统时间)。
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceCool_Graphics{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}Pointcenter;privatevoidForm1_Load(objectsender,EventArgse){lastFormSize=this.Size;initPicBox();timer1.Interval=welcomeTime/welcomeSplits;timer1.Enabled=true;}privatevoidinitPicBox(){//清空面板,找中心点center=newPoint(pictureBox1.Width/2,pictureBox1.Height/2);pictureBox1.Image=newBitmap(pictureBox1.Width,pictureBox1.Height);Graphicsg=Graphics.FromImage(pictureBox1.Image);g.FillRectangle(Brushes.White,newRectangle(0,0,pictureBox1.Width,pictureBox1.Height));//按窗口大小,自动设置表盘适宜半径r=pictureBox1.Width<pictureBox1.Height?(pictureBox1.Width/2-margin):(pictureBox1.Height/2-margin);//默认相对r比例设置其他值circle_width=GetRelativeValue(r,100);pFlag1_length=GetRelativeValue(r,25);pFlag1_width=GetRelativeValue(r,80);pFlag2_length=GetRelativeValue(r,50);pFlag2_width=GetRelativeValue(r,250);second_length=GetRelativeValue(r,1.15);second_tailLen=GetRelativeValue(r,5);second_width=GetRelativeValue(r,250);minute_length=GetRelativeValue(r,250.0/190.0);//分针长度minute_width=GetRelativeValue(r,250.0/2.0);//分针宽度hour_length=GetRelativeValue(r,250.0/160.0);//时针长度hour_width=GetRelativeValue(r,250.0/3.0);//时针宽度center_r=GetRelativeValue(r,250.0/4.0);//表盘中心点半径}intshanxinNum=0;privatevoidtimer1_Tick(objectsender,EventArgse){Graphicsg=Graphics.FromImage(pictureBox1.Image);g.FillPie(newSolidBrush(Color.FromArgb(shanxinNum+90,shanxinNum*2+10,shanxinNum*3+50)),center.X-r,center.Y-r,r*2,r*2,shanxinNum*360/welcomeSplits,360/welcomeSplits);if(shanxinNum++>welcomeSplits/2){timer2.Interval=1000;timer2.Enabled=true;}if(shanxinNum>welcomeSplits-1){shanxinNum=0;timer1.Enabled=false;}pictureBox1.Refresh();}intwelcomeTime=1000;//欢迎界面时间msintwelcomeSplits=60;//欢迎界面的切割个数intmargin=10;//表盘外边距intr=250;//表盘半径Colorbg_color=Color.White;//背景色Colorcircle_bg_color=Color.LightBlue;//圆盘背景色floatcircle_width=2;//外表盘圆宽度Colorcircle_color=Color.Black;//外表盘圆颜色floatpFlag1_length=10;//圆盘外部格标志1长度floatpFlag1_width=3;//圆盘外部格标志1宽度ColorpFlag1_color=Color.Black;//圆盘外部格标志1颜色floatpFlag2_length=5;//圆盘外部格标志2长度floatpFlag2_width=1;//圆盘外部格标志2宽度ColorpFlag2_color=Color.Black;//圆盘外部格标志2颜色floatpSLine_length=20;//下吊坠线长度floatpSLine_width=1;//下吊坠线长度ColorpSLine_color=Color.Red;//下吊坠线长度floatsecond_length=200;//秒针长度floatsecond_tailLen=50;//秒针尾巴长度floatsecond_width=1;//秒针宽度Colorsecond_color=Color.Red;//秒针颜色floatminute_length=190;//分针长度floatminute_width=2;//分针宽度Colorminute_color=Color.DarkGreen;//分针颜色floathour_length=160;//时针长度floathour_width=3;//时针宽度Colorhour_color=Color.DarkBlue;//时针颜色floatcenter_r=4;//表盘中心点半径Colorcenter_color=Color.Black;//圆心点颜色privatevoidtimer2_Tick(objectsender,EventArgse){//Console.WriteLine(DateTime.Now.Millisecond);//timer2.Enabled=false;Graphicsg=Graphics.FromImage(pictureBox1.Image);//面板清空g.FillRectangle(newSolidBrush(bg_color),newRectangle(0,0,pictureBox1.Width,pictureBox1.Height));//画圆盘背景g.FillEllipse(newSolidBrush(circle_bg_color),center.X-r,center.Y-r,r*2,r*2);//画表盘外框圆g.DrawEllipse(newPen(circle_color,circle_width),center.X-r,center.Y-r,r*2,r*2);//话表盘格doublespan=Math.PI/30;//每个格子间隔弧度值for(floati=0;i<60;i++){PointFp1=newPointF(center.X+r*(float)Math.Sin(i*span),center.Y-r*(float)Math.Cos(i*span));PointFp2;PointFps2;if(i%5==0){p2=newPointF(center.X+(r-pFlag1_length)*(float)Math.Sin(i*span),center.Y-(r-pFlag1_length)*(float)Math.Cos(i*span));g.DrawLine(newPen(pFlag1_color,pFlag1_width),p1,p2);/*ps2=newPointF(p1.X,p1.Y+pSLine_length);g.DrawLine(newPen(pSLine_color,pSLine_width),p1,ps2);*/}else{p2=newPointF(center.X+(r-pFlag2_length)*(float)Math.Sin(i*span),center.Y-(r-pFlag2_length)*(float)Math.Cos(i*span));g.DrawLine(newPen(pFlag2_color,pFlag2_width),p1,p2);}}//获取当前时间DateTimetime=DateTime.Now;floatmillisecond=(float)time.Millisecond;floatsecond=time.Second+millisecond/1000;floatminute=time.Minute+second/60;floathour=time.Hour+minute/60;PointFtempPF;//画时针tempPF=newPointF(center.X+hour_length*(float)Math.Sin(hour*5*span),center.Y-hour_length*(float)Math.Cos(hour*5*span));g.DrawLine(newPen(hour_color,hour_width),center,tempPF);//画分针tempPF=newPointF(center.X+minute_length*(float)Math.Sin(minute*span),center.Y-minute_length*(float)Math.Cos(minute*span));g.DrawLine(newPen(minute_color,minute_width),center,tempPF);//画秒针if(timer2.Interval==1000){second=time.Second;}tempPF=newPointF(center.X+second_length*(float)Math.Sin(second*span),center.Y-second_length*(float)Math.Cos(second*span));PointFtailP=newPointF(center.X-second_tailLen*(float)Math.Sin(second*span),center.Y+second_tailLen*(float)Math.Cos(second*span));g.DrawLine(newPen(second_color,second_width),tailP,tempPF);画秒针附加效果//for(inti=1;i<256;i++)//{//tempPF=newPointF(center.X+second_length*(float)Math.Sin((second-i*0.02)*span),center.Y-second_length*(float)Math.Cos((second-i*0.02)*span));//tailP=newPointF(center.X-second_tailLen*(float)Math.Sin((second-i*0.02)*span),center.Y+second_tailLen*(float)Math.Cos((second-i*0.02)*span));//g.DrawLine(newPen(Color.FromArgb(255,i,i),second_width),tailP,tempPF);//}画毫秒针//tempPF=newPointF(center.X+second_length*(float)Math.Sin(millisecond*span*60/1000),center.Y-second_length*(float)Math.Cos(millisecond*span*60/1000));//tailP=newPointF(center.X-second_tailLen*(float)Math.Sin(millisecond*span*60/1000),center.Y+second_tailLen*(float)Math.Cos(millisecond*span*60/1000));//g.DrawLine(newPen(second_color,second_width),tailP,tempPF);//画中心点g.FillEllipse(newSolidBrush(center_color),center.X-center_r,center.Y-center_r,center_r*2,center_r*2);pictureBox1.Refresh();//timer2.Enabled=true;}privatefloatGetRelativeValue(floatsrc,doubleratio){returnsrc/(float)ratio>1?src/(float)ratio:1;}SizelastFormSize;privatevoidForm1_ResizeEnd(objectsender,EventArgse){if(this.Size==lastFormSize)return;timer2.Enabled=false;shanxinNum=0;initPicBox();timer1.Interval=17;timer1.Enabled=true;}FormWindowStatelastState=FormWindowState.Normal;privatevoidForm1_SizeChanged(objectsender,EventArgse){if(this.WindowState!=lastState){lastState=this.WindowState;timer2.Enabled=false;shanxinNum=0;initPicBox();timer1.Interval=17;timer1.Enabled=true;}}}}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
C#如何实现图形界面的时钟的详细内容,希望对您有所帮助,信息来源于网络。