【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();

        }
    }
}
 

转载于:https://www.cnblogs.com/Sukie-s-home/p/6757279.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值