C # rounded corner button,

Source: Internet
Author: User

C # rounded corner button,

Because the built-in button is not very nice-looking, here I am looking for a code on the Internet to change it into a self-use button, the painting operation is not limited to the button, you can also draw other

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 {public partial class XButton: Button {enum model {hover, enter, press, enable} public Color HoverBackColor {get; set;} public Color EnterBackColor {get; set;} public Color PressBackColor {get; set;} public Color HoverForeColor {get; set;} public Color EnterForeColor {get; set;} public Color PressForeColor {get; set ;} public int Radius {get; set;} model paintmodel = model. hover; public XButton () {InitializeComponent (); // These must be carried, otherwise there will be a Black edge FlatStyle = FlatStyle. flat; FlatAppearance. borderSize = 0; FlatAppearance. borderColor = Color. fromArgb (0, 0, 0, 0); FlatAppearance. mouseDownBackColor = Color. transparent; FlatAppearance. mouseOverBackColor = Color. transparent; SetDefaultColor ();} public void SetDefaultColor () {// give the initial value HoverBackColor = Color. lightBlue; EnterBackColor = Color. blue; PressBackColor = Color. darkBlue; HoverForeColor = Color. white; EnterForeColor = Color. white; PressForeColor = Color. white; Radius = 18 ;}

      

        protected override void OnLayout(LayoutEventArgs levent)        {            SetDefaultColor();            Refresh();            base.OnLayout(levent);        }        protected override void OnParentChanged(EventArgs e)        {            Parent.SizeChanged += delegate { Refresh(); };            base.OnParentChanged(e);        }

 

Protected override void OnPaint (PaintEventArgs e) {base. onPaint (e); // This cannot be used, and it must be placed in front of it. Otherwise there will be some inexplicable things such as the Black Box var colorback = HoverBackColor; var colorfore = HoverForeColor; switch (paintmodel) {case model. hover: colorback = HoverBackColor; colorfore = HoverForeColor; break; case model. enter: colorback = EnterBackColor; colorfore = EnterForeColor; break; case model. press: colorback = PressBackColor; colgrading Ore = PressForeColor; break; case model. enable: colorback = Color. lightGray; break; default: colorback = HoverBackColor; colorfore = HoverForeColor; break;} Draw (e. clipRectangle, e. graphics, false, colorback); DrawText (e. clipRectangle, e. graphics, colorfore);} protected override void OnMouseEnter (EventArgs e) {paintmodel = model. enter; base. onMouseEnter (e);} protected override void OnMouseLeav E (EventArgs e) {paintmodel = model. hover; base. onMouseLeave (e);} protected override void OnMouseDown (MouseEventArgs mevent) {paintmodel = model. press; base. onMouseDown (mevent);} protected override void OnEnabledChanged (EventArgs e) {paintmodel = Enabled? Model. hover: model. enable; Invalidate (); // when the value of false is converted to true, the base is forcibly refreshed here. onEnabledChanged (e);} void Draw (Rectangle rectangle, Graphics g, bool cusp, Color begin_color, Color? End_colorex = null) {Color end_color = end_colorex = null? Begin_color: (Color) end_colorex; int span = 2; // g. smoothingMode = SmoothingMode. antiAlias; // gradient filled LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush (rectangle, begin_color, end_color, LinearGradientMode. vertical); // draw a vertex if (cusp) {span = 10; PointF p1 = new PointF (rectangle. width-12, rectangle. Y + 10); PointF p2 = new PointF (rectangle. width-12, rectangle. Y + 30); PointF p3 = new PointF (rectangle. width, rectangle. Y + 20); PointF [] ptsArray = {p1, p2, p3}; g. fillPolygon (myLinearGradientBrush, ptsArray);} // fill g. fillPath (myLinearGradientBrush, DrawRoundRect (rectangle. x, rectangle. y, rectangle. width-span, rectangle. height-1, Radius);} void DrawText (Rectangle rectangle, Graphics g, Color color) {SolidBrush sbr = new SolidBrush (color); var rect = new RectangleF (); switch (TextAlign) {case ContentAlignment. middleCenter: rect = getTextRec (rectangle, g); break; default: rect = getTextRec (rectangle, g); break;} g. drawString (Text, Font, sbr, rect);} RectangleF getTextRec (Rectangle rectangle, Graphics g) {var rect = new RectangleF (); var size = g. measureString (Text, Font); if (size. width> rectangle. width | size. height> rectangle. height) {rect = rectangle;} else {rect. size = size; rect. location = new PointF (rectangle. X + (rectangle. width-size. width)/2, rectangle. Y + (rectangle. height-size. height)/2);} return rect;} GraphicsPath DrawRoundRect (int x, int y, int width, int height, int radius) {// GraphicsPath gp = new GraphicsPath (); gp. addArc (x, y, radius, radius, 180, 90); gp. addArc (width-radius, y, radius, radius, 270, 90); gp. addArc (width-radius, height-radius, 0, 90); gp. addArc (x, height-radius, 90, 90); gp. closeAllFigures (); return gp ;}}}

 

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.