winform panel、label、button等控件,设置圆角

winform panel、label、button等控件,设置圆角


1、定义圆角类
2、封装一个调用类,里面包含对form、label、button的圆角处理方法
3、调用
4、效果图
直接上代码


    public class RoundOK
    {
        /*
        #region Window Const
        public const int WM_ERASEBKGND = 0x0014;
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        public const int WM_LBUTTONDBLCLK = 0x0203;
        public const int WM_WINDOWPOSCHANGING = 0x46;
        public const int WM_PAINT = 0xF;
        public const int WM_CREATE = 0x0001;
        public const int WM_ACTIVATE = 0x0006;
        public const int WM_NCCREATE = 0x0081;
        public const int WM_NCCALCSIZE = 0x0083;
        public const int WM_NCPAINT = 0x0085;
        public const int WM_NCACTIVATE = 0x0086;
        public const int WM_NCLBUTTONDOWN = 0x00A1;
        public const int WM_NCLBUTTONUP = 0x00A2;
        public const int WM_NCLBUTTONDBLCLK = 0x00A3;
        public const int WM_NCMOUSEMOVE = 0x00A0;

        public const int WM_NCHITTEST = 0x0084;

        public const int HTLEFT = 10;
        public const int HTRIGHT = 11;
        public const int HTTOP = 12;
        public const int HTTOPLEFT = 13;
        public const int HTTOPRIGHT = 14;
        public const int HTBOTTOM = 15;
        public const int HTBOTTOMLEFT = 0x10;
        public const int HTBOTTOMRIGHT = 17;
        public const int HTCAPTION = 2;
        public const int HTCLIENT = 1;

        public const int WM_FALSE = 0;
        public const int WM_TRUE = 1;
        #endregion
        */

        #region Public extern methods

        [DllImport("gdi32.dll")]
        public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);

        [DllImport("user32.dll")]
        public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);

        [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
        public static extern int DeleteObject(int hObject);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        #endregion
    }
    public class RoundControl
    {
        public static void SetFormRoundRectRgn(Form form, int rgnRadius)
        {
            int hRgn = 0;
            hRgn = RoundOK.CreateRoundRectRgn(0, 0, form.Width, form.Height, rgnRadius, rgnRadius);
            RoundOK.SetWindowRgn(form.Handle, hRgn, true);
            RoundOK.DeleteObject(hRgn);
        }
        public static void SetPanelRoundRectRgn(Panel panel, int rgnRadius)
        {
            int hRgn = 0;
            hRgn = RoundOK.CreateRoundRectRgn(0, 0, panel.Width, panel.Height, rgnRadius, rgnRadius);
            RoundOK.SetWindowRgn(panel.Handle, hRgn, true);
            RoundOK.DeleteObject(hRgn);
        }
        public static void SetLabelRoundRectRgn(Label label, int rgnRadius)
        {
            int hRgn = 0;
            hRgn = RoundOK.CreateRoundRectRgn(0, 0, label.Width, label.Height, rgnRadius, rgnRadius);
            RoundOK.SetWindowRgn(label.Handle, hRgn, true);
            RoundOK.DeleteObject(hRgn);
        }

    }

调用


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //SetFormRoundRectRgn(this,20);    //设置圆角
            this.BackColor = Color.Gray;
            RoundControl.SetPanelRoundRectRgn(panel1, 10);    //设置圆角
            RoundControl.SetLabelRoundRectRgn(label1, 5);    //设置圆角
        }
        
    }

效果图
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值