C#实现简单的loading提示控件实例代码
自己画一个转圈圈的控件
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Drawing.Drawing2D; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; namespaceExerciseUIPrj.controls { publicpartialclassLoadControl:Control { ColorbeginColor=Color.Blue; ColorendColor=Color.Red; intwid=10; intcurindex=0; Timertimer; intinstervel=200; stringloadStr="loading...."; publicLoadControl() { InitializeComponent(); SetStyle(ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer,true); this.MinimumSize=newSize(40,80); if(!DesignMode) { Start(); } } publicvoidStart() { if(timer==null) { timer=newTimer(); timer.Interval=instervel; timer.Tick+=Timer_Tick; } timer.Enabled=true; } publicvoidStop() { if(timer!=null) { timer.Enabled=false; } } voidTimer_Tick(objectsender,EventArgse) { curindex++; curindex=curindex>=wid?0:curindex; Refresh(); } //计算各种圈圈相关 PointgetPoint(doubled,doubler,Pointcenter) { intx=(int)(r*Math.Cos(d*Math.PI/180.0)); inty=(int)(r*Math.Sin(d*Math.PI/180.0)); returnnewPoint(center.X+x,center.Y-y); } GraphicsPathgetPath(Pointa,Pointb) { Pointc,d,e,f; inth=2; Vertical(a,b,h,outc,outd); Vertical(b,a,h,oute,outf); GraphicsPathpath=newGraphicsPath(); path.AddPolygon(newPoint[]{c,d,e,f}); path.CloseAllFigures(); returnpath; } boolVertical(Pointpointa,Pointpointb,doubleR,outPointpointc,outPointpointd) { pointc=newPoint(); pointd=newPoint(); try { //(X-xa)^2+(Y-ya)^2=R*R距离公式 //(X-xa)*(xb-xa)+(Y-ya)*(yb-ya)=0垂直 //解方程得两点即为所求点 varcx=pointa.X-(pointb.Y-pointa.Y)*R/Distance(pointa,pointb); varcy=pointa.Y+(pointb.X-pointa.X)*R/Distance(pointa,pointb); vardx=pointa.X+(pointb.Y-pointa.Y)*R/Distance(pointa,pointb); vardy=pointa.Y-(pointb.X-pointa.X)*R/Distance(pointa,pointb); pointc=newPoint((int)cx,(int)cy); pointd=newPoint((int)dx,(int)dy); returntrue; } catch { //如果A,B两点重合会报错,那样就返回false returnfalse; } } doubleDistance(doublexa,doubleya,doublexb,doubleyb) { doubleL; L=Math.Sqrt(Math.Pow(xa-xb,2)+Math.Pow(ya-yb,2)); returnL; } doubleDistance(Pointpa,Pointpb) { returnDistance(pa.X,pa.Y,pb.X,pb.Y); } GraphicsPathgetPath(doubled,doubler,Pointc) { varp1=getPoint(d,r/2.0,c); varp2=getPoint(d,r,c); returngetPath(p1,p2); } //算渐变色 Color[]getColors() { intdr=(int)((endColor.R-beginColor.R)/(double)wid); intdg=(int)((endColor.G-beginColor.G)/(double)wid); intdb=(int)((endColor.B-beginColor.B)/(double)wid); Listcolors=newList (); for(inti=0;i =wid?cindex-wid:cindex; g.FillPath(newSolidBrush(colors[cindex]),p); } } //画字符串 voiddrawString(Graphicsg) { if(Size.Height>=Size.Width)return; Rectanglerect=newRectangle(newPoint(Size.Height,0),newSize(Size.Width-Size.Height,Size.Height)); StringFormatsf=newStringFormat(); sf.Alignment=StringAlignment.Center; sf.LineAlignment=StringAlignment.Center; g.DrawString(loadStr,Font,Brushes.Black,rect,sf); } protectedoverridevoidOnPaint(PaintEventArgspe) { base.OnPaint(pe); Graphicsg=pe.Graphics; g.SmoothingMode=SmoothingMode.HighQuality; g.PixelOffsetMode=PixelOffsetMode.HighQuality; drawRect(g); drawString(g); } protectedoverridevoidOnSizeChanged(EventArgse) { base.OnSizeChanged(e); if(Size.Height>Size.Width) { Size=newSize(Size.Height,Size.Height); } } } }
总结
以上所述是小编给大家介绍的C#实现简单的loading提示控件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!