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();
    
            }
        }
    }
     
  • 相关阅读:
    第二阶段冲刺--第五天
    git托管代码随笔--运用ssh传输,不用每次提交频繁输入github账号密码
    项目冲刺--第十天
    项目冲刺--第九天
    随堂练习--用例图练习
    项目冲刺--第四天
    第五次个人作业: 案例分析--微软必应词典客户端
    Code.R团队展示
    Android 自定义AlertDialog
    Ubuntu打开系统监视器查看进程&资源等信息
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/6757279.html
Copyright © 2011-2022 走看看