- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[C#] Hướng dẫn custom Button trên Winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục chia sẻ đến các bạn cách Custom Button trong lập trình C# winform.
[C#] Custom Button Winform
Mặc định button trong Visual studio toolbox mặc định, các bạn sẽ không bo tròn được button.
Và không có chọn đường màu viền cho border button.
Dưới đây, mình sẽ gởi cho các bạn đoạn code Custom Button này lại, các bạn có thể download và chỉnh sửa lại theo ý của bạn.
Giao diện demo ứng dụng Custom Button C#, Winform:

Đầu tiên, các bạn tạo cho mình một class với tên: VBButton.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace CustomButton
{
    public class VBButton : Button
    {
        //Fields
        private int borderSize = 0;
        private int borderRadius = 20;
        private Color borderColor = Color.PaleVioletRed;
        //Properties
        [Category("Custom Props")]
        public int BorderSize
        {
            get { return borderSize; }
            set
            {
                borderSize = value;
                this.Invalidate();
            }
        }
        [Category("Custom Props")]
        public int BorderRadius
        {
            get { return borderRadius; }
            set
            {
                borderRadius = value;
                this.Invalidate();
            }
        }
        [Category("Custom Props")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                this.Invalidate();
            }
        }
        [Category("Custom Props")]
        public Color BackgroundColor
        {
            get { return this.BackColor; }
            set { this.BackColor = value; }
        }
        [Category("Custom Props")]
        public Color TextColor
        {
            get { return this.ForeColor; }
            set { this.ForeColor = value; }
        }
        //Constructor
        public VBButton()
        {
            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;
            this.Size = new Size(150, 40);
            this.BackColor = Color.MediumSlateBlue;
            this.ForeColor = Color.White;
            this.Resize += new EventHandler(Button_Resize);
        }
        //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, 270, 90);
            path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
            path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
            path.CloseFigure();
            return path;
        }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle rectSurface = this.ClientRectangle;
            Rectangle rectBorder = Rectangle.Inflate(rectSurface, -borderSize, -borderSize);
            int smoothSize = 2;
            if (borderSize > 0)
                smoothSize = borderSize;
            if (borderRadius > 2) //Rounded button
            {
                using (GraphicsPath pathSurface = GetFigurePath(rectSurface, borderRadius))
                using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
                using (Pen penSurface = new Pen(this.Parent.BackColor, smoothSize))
                using (Pen penBorder = new Pen(borderColor, borderSize))
                {
                    //Button surface
                    this.Region = new Region(pathSurface);
                    //Draw surface border for HD result
                    pevent.Graphics.DrawPath(penSurface, pathSurface);
                    //Button border                    
                    if (borderSize >= 1)
                        //Draw control border
                        pevent.Graphics.DrawPath(penBorder, pathBorder);
                }
            }
            else //Normal button
            {
                //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.Width - 1, this.Height - 1);
                    }
                }
            }
        }
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            this.Parent.BackColorChanged += new EventHandler(Container_BackColorChanged);
        }
        private void Container_BackColorChanged(object sender, EventArgs e)
        {
            this.Invalidate();
        }
        private void Button_Resize(object sender, EventArgs e)
        {
            if (borderRadius > this.Height)
                borderRadius = this.Height;
        }
    }
}Sau khi tạo xong, các bạn chạy build project hoặc bấm F5 xong đóng ứng dụng lại.
Các bạn vào Form1.cs, nhìn ở bên thanh Toolbox bên trái, các bạn sẽ thấy thêm Component VBButton.
Và các bạn có thể kéo ctrol này ra sử dụng bình thường.

Thanks for watching!

![[C#] Hướng dẫn custom Button trên Winform](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Source code phần mềm Food Order sử dụng FlowLayoutPanel và Usercontrol](https://laptrinhvb.net/uploads/source/vbnet/dat_mon_thumb.jpg)
![[C#] Hướng dẫn vẽ biểu đồ cột xuất file excel sử dụng thư viện Epplus](https://laptrinhvb.net/uploads/source/web/chart_excel.png)
![[C#] Checksum File trong lập trình Csharp](https://laptrinhvb.net/uploads/source/csharp/check_sum_file_thumb.jpg)

![[C#] Import dữ liệu file Excel (xls, xlsx, csv) vào Dataset hoặc Datatable](https://laptrinhvb.net/uploads/source/image_baiviet/95dae3f410d40a147787f979ba5cc059.png)
![[C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog](https://laptrinhvb.net/uploads/source/new_image_baiviet/serial_log_winform.png)
![[C#] Kiểm tra máy tính có cài đặt phần mềm diệt virus nào không?](https://laptrinhvb.net/uploads/source/csharp/detect_antivirus.jpg)
![[C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform](https://laptrinhvb.net/uploads/source/vbnet/blur_hash_demo.jpg)
![[C#] Hướng dẫn viết ứng dụng check live SSH Server](https://laptrinhvb.net/uploads/source/image_baiviet/6bc4d5b0ca4995dfce771849892f925e.jpg)
![[C#] Hướng dẫn tạo mã QRcode Style trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/demo_qrcode_style.png)
![[C#] Hướng dẫn quản lý danh sách, kết nối, ngắt kết nối mạng Wifi](https://laptrinhvb.net/uploads/source/image_baiviet/fa5a4f11e5efc4d291ae15c5dc44b513.jpg)
![[C#] Sử dụng DotNetBrowser nhân Chromium giải pháp thay thế WebBrowser trên Winform](https://laptrinhvb.net/uploads/source/vbnet/dot_net_browser_thumb.jpg)

![[C#] Hướng dẫn khởi động lại Retart process trên winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/retart_process_in_csharp.png)
![[C#] Sử dụng Reactive NET load Big data vào Gridview](https://laptrinhvb.net/uploads/source/csharp/reactive_csharp_thumb.png)
![[C#] Hướng dẫn đọc gmail sử dụng Gmail API lập trình Csharp](https://laptrinhvb.net/uploads/source/image_baiviet/5464ba09477a626d1ac456291143e5ed.png)
![[C#] Hướng dẫn tạo textbox tạo mã sản phẩm trên winform](https://laptrinhvb.net/uploads/source/vbnet/code_product_csharp.png)
![[C#] Hướng dẫn khóa chuột và bàn phím trong lập trình Winform](https://laptrinhvb.net/uploads/source/csharp/lock_keyboard_thumb.png)
![[C#] Hàm chuyển tiếng việt có dấu sang không dấu lập trình C#](https://laptrinhvb.net/uploads/source/image_baiviet/ac6c490aadbb2443382e72656e9e3a22.gif)
![[C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui](https://laptrinhvb.net/uploads/source/new_image_baiviet/console_gui.png)
![[C#] Khóa, mở khóa, protected file readonly USB trên winform](https://laptrinhvb.net/uploads/source/csharp/usb_management_thumb.png)
![[C#] Hướng dẫn custom label Winform thành Auto Resize Label](https://laptrinhvb.net/uploads/source/image_baiviet/7b787aa8f551aab0050759067efc5fcd.jpg)
![[C#] Number Effect Counter up and down in winform](https://laptrinhvb.net/uploads/source/new_image_baiviet/effect_number.png)
![[C#] Hướng dẫn LoadAsync PictureBox và Overlay trên Winform](https://laptrinhvb.net/uploads/source/csharp/overlay_picturebox.gif)
![[C#] Hướng dẫn lấy số điện thoại việt nam từ TextBox](https://laptrinhvb.net/uploads/source/csharp/find_phone_number_thumb.jpg)
