Custom Button - Custom Controls WinForm C # - RJ Code Advance
Custom Button - Custom Controls WinForm C # - RJ Code Advance
Mango TV
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 1/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
Hello, continuing with the tutorials of custom controls in Windows Form, this time we will
make a flat button with a customizable border radius , thus obtaining a button with
rounded corners, in the shape of a pill, or a normal rectangular button.
Normally in Windows Form, round buttons have poor image quality at the corners, so I
applied a little trick to get a good-quality rounded corner button.
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
// Fields
private int borderSize = 0 ;
private int borderRadius = 20 ;
private Color borderColor = Color. PaleVioletRed ;
// Properties
[ Category ( "RJ Code Advance" )]
public int BorderSize
{
get { return borderSize; }
set
{
borderSize = value ;
this . Invalidate () ;
}
}
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 3/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
set
{
borderRadius = value ;
this . Invalidate () ;
}
}
6.- Builder
In the constructor, we will initialize some properties of the button for the default
appearance . For example, a flat, borderless style with a specific size, background color, and
text color. They can set any other properties they want, such as an icon, text alignment, and
image.
It will also be necessary to subscribe the Resize event of the button, to keep the ratio of
the border radius limited to the height of the button, since if the border radius is greater than
the height of the button, the button will deform because it is impossible to draw an arc
bigger in smaller dimensions.
//Builder
public RJButton ()
{
this . FlatStyle = FlatStyle. Flat ;
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 4/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
We will add an arc on the initial axis of the rectangle equal in size to the radius, starting at
a 180 degree angle with a 90 degree travel range.
We will add another arc in the upper right corner, starting at a 270 degree angle with a 90
degree range.
Another arc in the lower right corner, starting at a 0 degree angle with a 90 degree range.
Finally we will add the last arc in the lower left corner to close the button shape, starting
at a 90 degree angle with a 90 degree range.
Note that the direction of the unit circle angles in C # or Visual Basic are clockwise
( Clockwise ) as shown in the following image.
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 5/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
// Methods
private GraphicsPath GetFigurePath ( Rectangle rect, float radius )
{
GraphicsPath path = new GraphicsPath () ;
float curveSize = radius * 2F;
path. StartFigure () ;
path. AddArc ( rect. X , rect. Y , curveSize, curveSize, 180 , 90 ) ;
path. AddArc ( rect. Right - curveSize, rect. Y , curveSize, curveSize, 2
path. AddArc ( rect. Right - curveSize, rect. Bottom - curveSize, curveSi
path. AddArc ( rect. X , rect. Bottom - curveSize, curveSize, curveSize,
path. CloseFigure () ;
return path;
}
// Button border
if ( borderSize > = 1 )
// Draw control border
pevent. Graphics . DrawPath ( penBorder, pathBorder ) ;
}
}
else // Normal button
{
pevent. Graphics . SmoothingMode = SmoothingMode. None ;
// Button surface
this . Region = new Region ( rectSurface ) ;
// Button border
if ( borderSize > = 1 )
{
using ( Pen penBorder = new Pen ( borderColor, borderSize ))
{
penBorder. Alignment = PenAlignment. Inset ;
pevent. Graphics . DrawRectangle ( penBorder, 0 , 0 , this .
}
}
}
}
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 7/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
It is also necessary to override the OnHandleCreated method. We will use this method to
solve the following drawback: In the previous method, to have a button with good image
quality, the edge of the surface is drawn with the same color as its parent container.
Therefore, if the form or container control changes its background color, the border of the
button surface will become visible, leaving a bad impression. To solve the problem we must
detect if the container form or control changes color. For this we will subscribe the
BackColorChanged event of the container parent.
It should be mentioned that the HandleCreated event is the one that most closely resembles
the Load event of the form or user control, since it is executed first when the handle of the
control is created.
Alright, and that's it, you can now use the new custom button from the toolbox, do n't
forget to compile the project to save the changes .
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 8/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
GUSTAVO
on July 18, 2021 at 4:34 am
Thank you very much Friend for doing these types of tutorials, being very sincere you are a
crack I always get inspired by your videos
Answer
Leave a reply
Your email address will not be published. Required fields are marked with *
Commentary
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 9/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
Name *
E-mail *
Web
Welcome to blog
Look for …
Follow me
Category:
.NET
ASP .NET
C#
Mistakes
F#
Visual basic
Windows Forms
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 10/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
ro tài sản
Lưu đồ phối hợp quản lý thi công nội
thất
PMC
Mở
Layered Architecture
Database
MySQL
SQL Server
Custom controls
courses
.NET (Course)
Create .Net Installer
Full Login C #, VB, SQL Server
Software Patterns (Course)
OOP (Course)
Desk
GUI
Software Patterns
OOP
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 11/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
Uncategorized
Web
Recent logins
Circular Picture Box - Border Gradient color + Styles - C # & WinForms
Custom ProgressBar - WinForms & C #
Custom TextBox Full - Rounded & Placeholder - WinForm, C #
Custom ComboBox - Icon, Back, Text & Border Color - WinForm C #
Custom Text Box - Custom controls WinForm C #
Recent comments
gustavo on Custom Button - Custom controls WinForm C #
_Nooker in Modern Form + Font Awesome Icons, WinForm, C # - VB.NET
ro tài sản
Lưu đồ phối hợp quản lý thi công nội
thất
PMC
Mở
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 12/13
7/22/2021 Custom Button - Custom controls WinForm C # - RJ Code Advance
https://rjcodeadvance.com/rounded-button-custom-controls-winform-c/ 13/13