Custom Component UITypeEditor

Source: Internet
Author: User
Tags cos dbx sin

Using System;
Using System.ComponentModel;
Using System.Drawing;
Using System.Drawing.Design;
Using System.Windows.Forms;
Using System.Windows.Forms.Design; namespace Mylib {//This uitypeeditor can is associated with Int32, Double and single//properties to provide a
    Design-mode Angle Selection interface. [System.Security.Permissions.PermissionSet (System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust
        ')] public class AngleEditor:System.Drawing.Design.UITypeEditor {public Angleeditor () { }//Indicates whether the UITypeEditor provides a form-based (modal) dialog,//Drop down dialog, or no
        UI outside of the Properties window. public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle (
        System.ComponentModel.ITypeDescriptorContext context) {return uitypeeditoreditstyle.dropdown;
        }//Displays the UI for value selection. public override ObjecT EditValue (System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider Provider, object value)
            {//Return the value if ' is ' not ' of type Int32, Double and single. if (value. GetType ()!= typeof (Double) && value. GetType ()!= typeof (float) && value.
            GetType ()!= typeof (int)) return value;
            Uses the IWindowsFormsEditorService to display A//Drop-down UI in the Properties window. IWindowsFormsEditorService edsvc = (iwindowsformseditorservice) provider.
            GetService (typeof (IWindowsFormsEditorService));
                if (edsvc!= null) {//Display a angle selection control and retrieve the value.
                Anglecontrol Anglecontrol = new Anglecontrol (double) value;
                Edsvc.dropdowncontrol (Anglecontrol);
                Return the value in the Appropraite data format. if (value. GetType () = = typeof (double) return anglecontrol.angle; else if (value.
                GetType () = = typeof (float)) return (float) anglecontrol.angle; else if (value.
            GetType () = = typeof (int)) return (int) Anglecontrol.angle;
        return value;
        }//Draws a representation of the property ' s value. public override void PaintValue (System.Drawing.Design.PaintValueEventArgs e) {int normalx = (e.bounds .
            WIDTH/2);
            int normaly = (E.BOUNDS.HEIGHT/2);
            Fill Background and Ellipse and center point.
            E.graphics.fillrectangle (New SolidBrush (Color.darkblue), e.bounds.x, E.bounds.y, E.bounds.width, e.Bounds.Height); E.graphics.fillellipse (New SolidBrush (Color.White), e.bounds.x + 1, E.bounds.y + 1, e.bounds.width-3, E.Bounds.Hei
            GHT-3); E.graphics.fillellipse (New SolidBrush (Color.slategray), Normalx + e.bounds.x-1, normaly + E.bounds.y-1, 3, 3);
            Draw line along the current angle.
            Double radians = (double) e.value * Math.PI)/(double) 180; E.graphics.drawline (New Pen (New SolidBrush (color.red), 1), Normalx + e.bounds.x, normaly + e.bounds.y, e.b Ounds. X + (normalx + (int) (double) normalx * Math.Cos (radians)), E.bounds.y + (normaly + (int) (double) normaly
        * Math.sin (radians)));
        }//Indicates whether the UITypeEditor supports painting A//representation of a property ' s value.
            public override bool GetPaintValueSupported (System.ComponentModel.ITypeDescriptorContext context) {
        return true;
    }//provides a user interface for adjusting a angle value.
        Internal class AngleControl:System.Windows.Forms.UserControl {//Stores the angle.
        public double angle;
        Stores the rotation offset.
        private int rotation = 0; ControL State Tracking variables.
        private int dbx =-10;
        private int dby =-10;
        private int Overbutton =-1;
            Public Anglecontrol (double initial_angle) {this.angle = Initial_angle; This.
        SetStyle (Controlstyles.allpaintinginwmpaint, true);  } protected override void OnPaint (System.Windows.Forms.PaintEventArgs e) {//Set angle Origin
            Point at Center for control. int Originx = (this.
            WIDTH/2); int originy = (this.
            HEIGHT/2);
            Fill Background and Ellipse and center point. E.graphics.fillrectangle (New SolidBrush (Color.darkblue), 0, 0, this. Width, this.
            Height); E.graphics.fillellipse (New SolidBrush (Color.White), 1, 1, this. Width-3, this.
            HEIGHT-3);
            E.graphics.fillellipse (New SolidBrush (Color.slategray), OriginX-1, OriginY-1, 3, 3);
            Draw angle Markers.
 int startangle = (270-rotation)% 360;           E.graphics.drawstring (startangle. ToString (), New Font ("Arial", 8), New SolidBrush (Color.darkgray), (this.
            WIDTH/2)-10, 10);
            StartAngle = (startangle + 90)% 360; E.graphics.drawstring (startangle. ToString (), New Font ("Arial", 8), New SolidBrush (Color.darkgray), this. Width-18, (this.
            HEIGHT/2)-6);
            StartAngle = (startangle + 90)% 360; E.graphics.drawstring (startangle. ToString (), New Font ("Arial", 8), New SolidBrush (Color.darkgray), (this. WIDTH/2)-6, this.
            HEIGHT-18);
            StartAngle = (startangle + 90)% 360; E.graphics.drawstring (startangle. ToString (), New Font ("Arial", 8), New SolidBrush (Color.darkgray), ten, (this.
            HEIGHT/2)-6);
            Draw line along the current angle.
            Double radians = (((angle + rotation) + 360)% 360) * Math.PI)/(double) 180; E.graphics.drawline (New Pen (New SolidBrush (color.red), 1), Originx, Originy, Originx + (int) (double) Originx * (double) Math.Cos (radians)), Originy + (int) (double) Originy (double) Math.sin (radians));
            Output angle information. E.graphics.fillrectangle (New SolidBrush (Color.gray), this.
            Width-84, 3, 82, 13); E.graphics.drawstring ("Angle:" + Angle.) ToString ("F4"), New Font ("Arial", 8), New SolidBrush (Color.yellow), this.
            Width-84, 2);
            Draw square at mouse position's last angle adjustment.
            E.graphics.drawrectangle (New Pen (New SolidBrush (Color.Black), 1), Dbx-2, Dby-2, 4, 4);
            Draw rotation adjustment buttons. if (Overbutton = = 1) {E.graphics.fillrectangle (new SolidBrush (Color.green), this. Width-28, this.
                HEIGHT-14, 12, 12); E.graphics.fillrectangle (New SolidBrush (Color.gray), 2, this.
                HEIGHT-13, 110, 12); E.graphics.drawstring ("Rotate degrees left", New Font ("Arial", 8), New SolidBrush (Color.White), 2, this.
            HEIGHT-14); else E.graphics.fillrectangle (new SolidBrush (Color.darkgreen), this. Width-28, this.
            HEIGHT-14, 12, 12); if (Overbutton = = 2) {E.graphics.fillrectangle (new SolidBrush (Color.green), this. Width-14, this.
                HEIGHT-14, 12, 12); E.graphics.fillrectangle (New SolidBrush (Color.gray), 2, this.
                HEIGHT-13, 116, 12); E.graphics.drawstring ("Rotate degrees Right", New Font ("Arial", 8), New SolidBrush (Color.White), 2, this.
            HEIGHT-14); else E.graphics.fillrectangle (new SolidBrush (Color.darkgreen), this. Width-14, this.
            HEIGHT-14, 12, 12); E.graphics.drawellipse (New Pen (New SolidBrush (Color.White), 1), this. Width-11, this.
            HEIGHT-11, 6, 6); E.graphics.drawellipse (New Pen (New SolidBrush (Color.White), 1), this. Width-25, this.
            HEIGHT-11, 6, 6); if (Overbutton = 1) e.gRaphics. FillRectangle (New SolidBrush (Color.green), this. Width-25, this.
            Height-6, 4, 4); else E.graphics.fillrectangle (new SolidBrush (Color.darkgreen), this. Width-25, this.
            Height-6, 4, 4); if (Overbutton = = 2) e.graphics.fillrectangle (new SolidBrush (Color.green), this. Width-8, this.
            Height-6, 4, 4); else E.graphics.fillrectangle (new SolidBrush (Color.darkgreen), this. Width-8, this.
            Height-6, 4, 4); E.graphics.fillpolygon (New SolidBrush (Color.White), new point[] {new point. Width-7, this. HEIGHT-8), new Point (this. Width-3, this. HEIGHT-8), new Point (this. Width-5, this.
            HEIGHT-4)}); E.graphics.fillpolygon (New SolidBrush (Color.White), new point[] {new point. Width-26, this. HEIGHT-8), new Point (this. Width-21, this. HEIGHT-8), new Point (this. Width-25, this.
        HEIGHT-4)}); } protected override void OnMouseDown (System.Windows.FormS.mouseeventargs e) {//Handle rotation adjustment button clicks. if (e.x >= this. Width-28 && e.x <= this. Width-2 && e.y >= this. Height-14 && e.y <= this. Height-2) {if (e.x <=).
                Width-16) Rotation-= 90; else if (e.x >= this.
                WIDTH-14) rotation + + 90;
                if (rotation < 0) rotation + + 360;
                rotation = rotation% 360;
                dbx =-10;
            Dby =-10;
            else Updateangle (e.x, e.y); This.
        Refresh ();  } protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e) {if (E.button = =
                MouseButtons.Left) {Updateangle (e.x, e.y);
            Overbutton =-1; else if (e.x >= this.) Width-28 &&amP E.x <= this. Width-16 && e.y >= this. Height-14 && e.y <= this.
            Height-2) Overbutton = 1; else if (e.x >= this. Width-14 && e.x <= this. Width-2 && e.y >= this. Height-14 && e.y <= this.
            Height-2) Overbutton = 2;
            else Overbutton =-1; This.
        Refresh ();
            private void Updateangle (int mx, int my) {//Store mouse coordinates.
            dbx = mx;
            Dby = my;
            Translate y coordinate input to getangle function to correct for ellipsoid distortion. Double widthtoheightratio = (double) this. Width/(double) this.
            Height;
            int tmy;
            if (i = = 0) tmy = my; else if (Me < this. HEIGHT/2) Tmy = (this. HEIGHT/2)-(int) ((this.
            HEIGHT/2)-My) * widthtoheightratio); else tmy = (thiS.HEIGHT/2) + (int) (double) (this.
            HEIGHT/2)) * widthtoheightratio);
            Retrieve updated angle based on rise over run. Angle = (Getangle (this. WIDTH/2, this.
        HEIGHT/2, MX, tmy)-rotation)% 360;
            Private double getangle (int x1, int y1, int x2, int y2) {double degrees;
            Avoid divide by zero run values.
                if (x2-x1 = = 0) {if (y2 > y1) degrees = 90;
            else degrees = 270;
                else {//Calculate angle from offset.
                Double Riseoverrun = (double) (y2-y1)/(double) (X2-X1);
                Double radians = Math.atan (Riseoverrun);
                degrees = radians * (double) 180/math.pi);
                Handle Quadrant specific transformations. if ((X2-X1) < 0 | |
 (Y2-Y1) < 0) degrees + + 180;               if ((x2-x1) > 0 && (y2-y1) < 0) degrees-= 180;
            if (degrees < 0) degrees + + 360;
        return degrees; 
        } public class AngleEditorTestControl:System.Windows.Forms.UserControl {private Double int_angle;
        [BrowsableAttribute (True)]
            [Editor (typeof (Angleeditor), typeof (System.Drawing.Design.UITypeEditor))] public double Angle {
            get {return int_angle;}
        set {Int_angle = value;}
            Public Angleeditortestcontrol () {int_angle = 90; This.
            size = new Size (190, 42); This.
        BackColor = Color.beige; } protected override void OnPaint (System.Windows.Forms.PaintEventArgs e) {if (this. DesignMode) {e.graphics.drawstring ("Use of the Properties window to access", NEW Font ("Arial", 8), New SolidBrush (Color.Black), 3, 2);
                E.graphics.drawstring ("The Angleeditor UITypeEditor by", New Font ("Arial", 8), New SolidBrush (Color.Black), 3, 14);  E.graphics.drawstring ("Configuring the" Angle\ "property.", New Font ("Arial", 8), New SolidBrush (Color.Black),
            3, 26); else E.graphics.drawstring ("This example requires design mode.", New Font ("Arial", 8), new S
        Olidbrush (Color.Black), 3, 2); }
    }
}

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.