• 【Winform-自定义控件】可以使用2种半透明的颜色来填充Button


    制作一个自定义按钮,使用2种半透明的颜色来填充Button

    1.添加一个自定义控件类,并改变基类,继承自Button

     public partial class CustomControl1 : Button

    2.为控件创建一些自定义属性

            private Color color1 = Color.White; //第一种颜色
            public Color Color1
            {
                get { return color1; }
                set { color1 = value; Invalidate(); }
            }
            private Color color2 = Color.Black; //第二种颜色
            public Color Color2
            {
                get { return color2; }
                set { color2 = value; Invalidate(); }
            }
            private int color1Transparent = 64;  //第一种颜色透明度
            public int Color1Transparent
            {
                get { return color1Transparent; }
                set { color1Transparent = value; Invalidate(); }
            }
            private int color2Transparent = 64; //第二种颜色透明度
            public int Color2Transparent
            {
                get { return color2Transparent; }
                set { color2Transparent = value; Invalidate(); }
            }
    Invalidate()方法用于刷新设计图。

    3.重写Paint事件
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);//调用基类
    
                //用两种半透明的颜色填充Button
                Color c1 = Color.FromArgb(color1Transparent, color1);
                Color c2 = Color.FromArgb(color2Transparent, color2);
                Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);
                pe.Graphics.FillRectangle(b,ClientRectangle);
                b.Dispose();
    
            }

    4.到这里就完成了。

    完整代码:

    using System;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace ctlCuteButton
    {
        public partial class CustomControl1 : Button
        {
    
            private Color color1 = Color.White; //第一种颜色
            public Color Color1
            {
                get { return color1; }
                set { color1 = value; Invalidate(); }
            }
            private Color color2 = Color.Black; //第二种颜色
            public Color Color2
            {
                get { return color2; }
                set { color2 = value; Invalidate(); }
            }
            private int color1Transparent = 64;  //第一种颜色透明度
            public int Color1Transparent
            {
                get { return color1Transparent; }
                set { color1Transparent = value; Invalidate(); }
            }
            private int color2Transparent = 64; //第二种颜色透明度
            public int Color2Transparent
            {
                get { return color2Transparent; }
                set { color2Transparent = value; Invalidate(); }
            }
    
            public CustomControl1()
            {
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);//调用基类
    
                //用两种半透明的颜色填充Button
                Color c1 = Color.FromArgb(color1Transparent, color1);
                Color c2 = Color.FromArgb(color2Transparent, color2);
                Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, 10);
                pe.Graphics.FillRectangle(b,ClientRectangle);
                b.Dispose();
    
            }
        }
    }
     
  • 相关阅读:
    Java实现 蓝桥杯 算法训练 Number Challenge(暴力)
    Java实现 蓝桥杯 算法训练 Number Challenge(暴力)
    Java实现 蓝桥杯 算法训练 Number Challenge(暴力)
    Java实现 蓝桥杯 算法训练 Rotatable Number(暴力)
    Java实现 蓝桥杯 算法训练 Rotatable Number(暴力)
    Java实现 蓝桥杯 算法训练 Rotatable Number(暴力)
    Java实现 蓝桥杯 算法训练 猴子吃包子(暴力)
    Java实现 蓝桥杯 算法训练 猴子吃包子(暴力)
    Java实现 蓝桥杯 算法训练 猴子吃包子(暴力)
    Python位运算符
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/6757279.html
Copyright © 2020-2023  润新知