制作一个自定义按钮,使用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 = ; //第一种颜色透明度
public int Color1Transparent
{
get { return color1Transparent; }
set { color1Transparent = value; Invalidate(); }
}
private int color2Transparent = ; //第二种颜色透明度
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, );
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 = ; //第一种颜色透明度
public int Color1Transparent
{
get { return color1Transparent; }
set { color1Transparent = value; Invalidate(); }
}
private int color2Transparent = ; //第二种颜色透明度
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, );
pe.Graphics.FillRectangle(b,ClientRectangle);
b.Dispose(); }
}
}
 

【Winform-自定义控件】可以使用2种半透明的颜色来填充Button的更多相关文章

  1. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

  2. (二十二)c#Winform自定义控件-半透明窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. winform 自定义控件属性在属性面板中显示

    Jan.David Nothing is impossible, the word itself says 'I'm possible'!" — Audrey Hepburn winform ...

  4. Winform自定义控件实例

    本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...

  5. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (三十一)c#Winform自定义控件-文本框(四)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. Elasticsearch5.x 引擎健康情况

    查看引擎健康情况 [root@w]# curl -XGET "http://localhost:9200/_cat/health?v" epoch timestamp cluste ...

  2. The import javax.websocket cannot be resolved的解决问题

    在eclipse中导入项目的时候出现了这个问题,废了我半天劲,才搞明白,把问题记录下来,方便大家以后遇到这问题好处理.提供参考. 出现的问题截图: 因为我用的是tomcat8, 大体步骤:项目上点右键 ...

  3. Linux系列(3):入门之正确的关机方法

    前言:在Windows(非NT主机系统)系统中,由于是单人假多任务的情况,所以即使你计算机关机,对于别人也没有丝毫影响!不过,在Linux下面,由于每个程序(或者服务)都是在在背景下执行,因此,在看不 ...

  4. Number of Containers ZOJ - 3175(数论题)

    Problem Description For two integers m and k, k is said to be a container of m if k is divisible by  ...

  5. JWT的认识和session的区别

    1.前后端分离框架中前端和后端域名不同,不能跨域请求,加上移动端无cookie,所以无法使用session.2.基于token的认证和传统的session认证的区别: 传统的session认证: 我们 ...

  6. 简单Kibana命令

    1 查看健康状态 GET _cat/health?v epoch timestamp cluster status node.total node.data shards 1531290005 14: ...

  7. T100 事务的开始与结束

    例如: IF cl_ask_confirm('apm-00801') THEN CALL s_transaction_begin() IF NOT axmt500_change_xmdc015('u' ...

  8. 怎样理解 DOCTYPE 声明

    1. HTML 4.01 Strict <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www. ...

  9. maven 常见命令 学习笔记(一)之 -pl -am -amd

    假设现有项目结构如下 dailylog-parent|-dailylog-common|-dailylog-web 三个文件夹处在同级目录中 dailylog-web依赖dailylog-common ...

  10. js修改当前页面地址栏参数

    利用HTML5 history新特性replaceState方法可以修改当前页面地址栏参数,示例代码: //选择日期后改变地址栏 var urlSearch = location.href; var ...