├── LICENSE ├── README.md └── RJButton.cs /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Custom-Button-WinForm 2 | Windows Form C# 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJButton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using System.Windows.Forms; 8 | using System.Drawing; 9 | using System.Drawing.Drawing2D; 10 | using System.ComponentModel; 11 | 12 | namespace CustomControls.RJControls 13 | { 14 | public class RJButton : Button 15 | { 16 | //Fields 17 | private int borderSize = 0; 18 | private int borderRadius = 0; 19 | private Color borderColor = Color.PaleVioletRed; 20 | 21 | //Properties 22 | [Category("RJ Code Advance")] 23 | public int BorderSize 24 | { 25 | get { return borderSize; } 26 | set 27 | { 28 | borderSize = value; 29 | this.Invalidate(); 30 | } 31 | } 32 | 33 | [Category("RJ Code Advance")] 34 | public int BorderRadius 35 | { 36 | get { return borderRadius; } 37 | set 38 | { 39 | borderRadius = value; 40 | this.Invalidate(); 41 | } 42 | } 43 | 44 | [Category("RJ Code Advance")] 45 | public Color BorderColor 46 | { 47 | get { return borderColor; } 48 | set 49 | { 50 | borderColor = value; 51 | this.Invalidate(); 52 | } 53 | } 54 | 55 | [Category("RJ Code Advance")] 56 | public Color BackgroundColor 57 | { 58 | get { return this.BackColor; } 59 | set { this.BackColor = value; } 60 | } 61 | 62 | [Category("RJ Code Advance")] 63 | public Color TextColor 64 | { 65 | get { return this.ForeColor; } 66 | set { this.ForeColor = value; } 67 | } 68 | 69 | //Constructor 70 | public RJButton() 71 | { 72 | this.FlatStyle = FlatStyle.Flat; 73 | this.FlatAppearance.BorderSize = 0; 74 | this.Size = new Size(150, 40); 75 | this.BackColor = Color.MediumSlateBlue; 76 | this.ForeColor = Color.White; 77 | this.Resize += new EventHandler(Button_Resize); 78 | } 79 | 80 | //Methods 81 | private GraphicsPath GetFigurePath(Rectangle rect, int radius) 82 | { 83 | GraphicsPath path = new GraphicsPath(); 84 | float curveSize = radius * 2F; 85 | 86 | path.StartFigure(); 87 | path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90); 88 | path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90); 89 | path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90); 90 | path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90); 91 | path.CloseFigure(); 92 | return path; 93 | } 94 | 95 | protected override void OnPaint(PaintEventArgs pevent) 96 | { 97 | base.OnPaint(pevent); 98 | 99 | 100 | Rectangle rectSurface = this.ClientRectangle; 101 | Rectangle rectBorder = Rectangle.Inflate(rectSurface, -borderSize, -borderSize); 102 | int smoothSize = 2; 103 | if (borderSize > 0) 104 | smoothSize = borderSize; 105 | 106 | if (borderRadius > 2) //Rounded button 107 | { 108 | using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius)) 109 | using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize)) 110 | using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize)) 111 | using (Pen penBorder = new Pen(borderColor, borderSize)) 112 | { 113 | pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 114 | //Button surface 115 | this.Region = new Region(pathSurface); 116 | //Draw surface border for HD result 117 | pevent.Graphics.DrawPath(penSurface, pathSurface); 118 | 119 | //Button border 120 | if (borderSize >= 1) 121 | //Draw control border 122 | pevent.Graphics.DrawPath(penBorder, pathBorder); 123 | } 124 | } 125 | else //Normal button 126 | { 127 | pevent.Graphics.SmoothingMode = SmoothingMode.None; 128 | //Button surface 129 | this.Region = new Region(rectSurface); 130 | //Button border 131 | if (borderSize >= 1) 132 | { 133 | using (Pen penBorder = new Pen(borderColor, borderSize)) 134 | { 135 | penBorder.Alignment = PenAlignment.Inset; 136 | pevent.Graphics.DrawRectangle(penBorder, 0, 0, this.Width - 1, this.Height - 1); 137 | } 138 | } 139 | } 140 | } 141 | protected override void OnHandleCreated(EventArgs e) 142 | { 143 | base.OnHandleCreated(e); 144 | this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged); 145 | } 146 | 147 | private void Container_BackColorChanged(object sender, EventArgs e) 148 | { 149 | this.Invalidate(); 150 | } 151 | private void Button_Resize(object sender, EventArgs e) 152 | { 153 | if (borderRadius > this.Height) 154 | borderRadius = this.Height; 155 | } 156 | } 157 | } 158 | --------------------------------------------------------------------------------