How C # Implements a simple instance of the loading hint control

Source: Internet
Author: User
This article through the instance code to introduce the C # implementation simple loading hint control function, the code is very simple, has the reference value, needs the friend reference next

Draw a Circle control yourself


Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.drawing.drawing2d;using system.linq;using system.text;using system.threading.tasks;using System.windows.forms;namespace exerciseuiprj.controls{public partial class Loadcontrol:control {Color Begincolor    = Color.Blue;    Color endcolor = color.red;    int wid = 10;    int curindex = 0;    Timer timer;    int instervel = 200;    String loadstr = "Loading ....";      Public LoadControl () {InitializeComponent (); SetStyle (Controlstyles.userpaint | Controlstyles.allpaintinginwmpaint|      Controlstyles.optimizeddoublebuffer, True); This.      MinimumSize = new Size (40, 80); if (!      DesignMode) {Start ();        }} public void Start () {if (timer = = NULL) {timer = new timer (); Timer.        Interval = Instervel; Timer.      Tick + = Timer_tick; } timer.    Enabled = true; } public void Stop () {if (Timer! = NULL) {timer.      Enabled = false;      }} void Timer_tick (object sender, EventArgs e) {curindex++; Curindex = Curindex >= wid?      0:curindex;    Refresh (); }//Calculate various circle-related point GetPoint (double D, double R, point center) {int x = (int) (R * Math.Cos (d * math.pi/180      .0));      int y = (int) (R * Math.sin (d * math.pi/180.0)); return new Point (center. x + x, center.    Y-Y);      } GraphicsPath GetPath (point A, point B) {point C, D, E, F;      int h = 2;      Vertical (A, B, H, out C, out D);      Vertical (b, A, h, out E, out F);      GraphicsPath path = new GraphicsPath (); Path.      Addpolygon (new point[] {c, D, E, f}); Path.      Closeallfigures ();    return path; } bool Vertical (Point Pointa, point pointb, Double R, out Point Pointc, out point pointd) {POINTC = new Point (      );      Pointd = new Point ();  try {//(X-XA) ^2+ (y-ya) ^2=r*r distance formula//(X-XA) * (XB-XA) + (Y-ya) * (Yb-ya) =0 Vertical      The solution to the equation is two points for the point var cx = Pointa. X-(POINTB. Y-pointa.        Y) * R/distance (Pointa, POINTB); var cy = Pointa. Y + (POINTB. X-pointa.        X) * R/distance (Pointa, POINTB); var dx = Pointa. X + (POINTB. Y-pointa.        Y) * R/distance (Pointa, POINTB); var dy = Pointa. Y-(POINTB. X-pointa.        X) * R/distance (Pointa, POINTB);        POINTC = new Point ((int) CX, (int) CY);        Pointd = new Point ((int) dx, (int) dy);      return true;      } catch {//if a, b two points coincident will error, then return false to false;      }} double Distance (double xa, double ya, double xb, double yb) {double L;      L = Math.sqrt (Math.pow (XA-XB, 2) + Math.pow (YA-YB, 2));    return L; } Double Distance (point PA, point Pb) {return Distance (Pa. X, Pa. Y, PB. X, PB.    Y);      } GraphicsPath GetPath (double D, Double R, point c) {var p1 = GetPoint (d, r/2.0, C);      var P2 = getpoint (d, R, c);    Return GetPath (P1, p2); }//Gradient gradient color[] GetcoloRS () {int dr = (int) ((ENDCOLOR.R-BEGINCOLOR.R)/(double) wid);      int dg = (int) ((ENDCOLOR.G-BEGINCOLOR.G)/(double) wid);      int db = (int) ((endcolor.b-begincolor.b)/(double) wid);      list<color> colors = new list<color> (); for (int i = 0; i < wid; i++) {colors.      ADD (Color.FromArgb (BEGINCOLOR.R + dr * I, BEGINCOLOR.G + dg * I, BEGINCOLOR.B + db * i)); } return colors.    ToArray ();      }//Draw circle void DrawRect (Graphics g) {int r = (int) (size.height/2.0);      Point Center = new Point (R, R);      var colors = getcolors ();      int findex = Curindex;        for (int i = 0; i < wid; i++) {Double d = (360.0/wid) * i;        var p = GetPath (d, R, center);        int CIndex = Findex + i; CIndex = CIndex >= wid?        Cindex-wid:cindex;      G.fillpath (New SolidBrush (Colors[cindex]), p);    }}//Draw the String void drawstring (Graphics g) {if (size.height >= size.width) return;  Rectangle rect = new Rectangle (new Point (size.height, 0), New Size (Size.width-size.height, size.height));      StringFormat SF = new StringFormat (); sf.      Alignment = Stringalignment.center; sf.      LineAlignment = Stringalignment.center;    g.DrawString (Loadstr, Font, Brushes.black, RECT,SF); } protected override void OnPaint (PaintEventArgs pe) {base.      OnPaint (PE); Graphics g = pe.      Graphics;      G.smoothingmode = smoothingmode.highquality;      G.pixeloffsetmode = pixeloffsetmode.highquality;      DrawRect (g);    DrawString (g); } protected override void Onsizechanged (EventArgs e) {base.      Onsizechanged (e);      if (Size.Height > size.width) {size = new Size (size.height, size.height); }    }  }}

Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.