zoukankan      html  css  js  c++  java
  • 【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();
    
            }
        }
    }
     
  • 相关阅读:
    ST3 插件和技巧
    博客园中 代码使用 sublime-text 的主题
    JavaScript中的 prototype 和 constructor
    JavaScript 代码小片段
    JavaScript 静态方法和实例方法
    JavaScript 闭包的例子
    关于linux 交叉编译器的安装
    scrapy 实现mysql 数据保存
    django 实现 内网访问 和 用花生壳进行内网穿透
    django 中从外界借助多个网站时 static 的存放和整理
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/6757279.html
Copyright © 2011-2022 走看看