In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to implement custom rounded buttons in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The button included in Winform does not have a fillet property, so we inherit the Button class and override the OnPaint event to draw the fillet button.
1. Drawing the fillet button box requires the drawing method that comes with the system: first, the CreateRoundRectRgn method in Gdi32.dll is introduced. It has been proved that the use of this method in the case of a large number of controls will lead to GDI+ drawing problems! Now use their own fillet GraphicsPath to fill the display, the effect is better, the fillet is more round!
Override the OnPaint method:
Protected override void OnPaint (PaintEventArgs pe) {if (! roundCorner) {base.OnPaint (pe); return;} Graphics g = pe.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; / / g.SmoothingMode = SmoothingMode.HighQuality; / / g.CompositingQuality = CompositingQuality.HighQuality / / g.InterpolationMode = InterpolationMode.HighQualityBicubic; Rectangle rect = this.ClientRectangle; Brush brhBorder = new SolidBrush (crBorderPainting); Brush brhRect = new SolidBrush (BackColor); Brush b0 = new SolidBrush (this.Parent.BackColor); Brush bfont = new SolidBrush (ForeColor); try {g.Clear (this.Parent.BackColor) Int borderSize = FlatAppearance.BorderSize; try {GraphicsPath path = CreateRoundRect (rect.Left, rect.Top, rect.Left + rect.Width-borderSize, rect.Top + rect.Height-borderSize); g.FillPath (brhBorder, path); path.Dispose () Path = CreateRoundRect (rect.Left + borderSize / 2f, rect.Top + borderSize / 2f, rect.Left + rect.Width-borderSize * 2, rect.Top + rect.Height-borderSize * 2); g.FillPath (brhRect, path); path.Dispose () } catch (Exception e) {Console.WriteLine ("FillPath:" + e.Message);} if (this.Text! = string.Empty) {StringFormat sf = StringFormat.GenericTypographic; sf.FormatFlags | = StringFormatFlags.MeasureTrailingSpaces SizeF sizeoftext = g.MeasureString (this.Text, Font); float tx = (float) ((this.Width-sizeoftext.Width) / 2.0); float ty = (float) ((this.Height-sizeoftext.Height) / 2.0); g.DrawString (this.Text, Font, bfont, tx, ty) }} finally {b0.Dispose (); brhBorder.Dispose (); brhRect.Dispose (); bfont.Dispose ();}} private GraphicsPath CreateRoundRect (float rleft, float rtop, float rwidth, float rheight) {float r = radius If (rwidth
< rheight) { if (radius >Rwidth / 2f) r = rwidth / 2f;} else {if (radius > rheight / 2f) r = rheight / 2f;} GraphicsPath path; RectangleF rectRow = new RectangleF (rleft, rtop + r, rwidth, rheight-r * 2) RectangleF rectColumn = new RectangleF (rleft + r, rtop, rwidth-r * 2, rheight); path = new GraphicsPath (FillMode.Winding); path.AddRectangle (rectRow); path.AddRectangle (rectColumn); / / Upper left path.AddEllipse (rleft, rtop, r * 2, r * 2) / upper right path.AddEllipse (rleft + rwidth-r * 2, rtop, r * 2, r * 2); / lower left path.AddEllipse (rleft, rtop + rheight-r * 2, r * 2, r * 2); / / lower right path.AddEllipse (rleft + rwidth-r * 2, rtop + rheight-r * 2, r * 2, r * 2); return path }
two。 If you need to increase the hovering effect, you can override the OnMouseEnter and OnMouseLeave events to change the boundary and background color.
Private Color crBorderActive = Color.FromArgb (Convert.ToInt32 ("FF3283C4", 16)); private Color crRectActive = Color.FromArgb (Convert.ToInt32 ("FFE3F3FB", 16)); private Color crBorderDefault = Color.FromArgb (215,215215); protected override void OnMouseEnter (EventArgs e) {base.OnMouseEnter (e); this.BackColor = crRectActive; this.FlatAppearance.BorderColor = crBorderActive } protected override void OnMouseLeave (EventArgs e) {base.OnMouseLeave (e); this.BackColor = Color.White; this.FlatAppearance.BorderColor = crBorderDefault;}
At this point, a round corner button is complete. The effect is as follows:
Thank you for reading! This is the end of this article on "how to implement custom fillet buttons in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.