C # simple loading prompt control,

Source: Internet
Author: User

C # simple loading prompt control,

Draw a circle control by 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 the Point getPoint (double d, double r, Point center) of various circles {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 Gra PhicsPath (); 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 // obtain two points for solving the equation: 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 and B overlap, an error is returned, and false return 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);} // calculate gradient Color [] getColors () {int dr = (int) (endColor. r-beginColor. r)/(doub Le) 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 ();} // circled void drawRect (Graphics g) {int r = (int) (Size. height/2.0); Point c Enter = 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 );}}}}

 

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.