【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 = ; //第一种颜色透明度
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的更多相关文章
- winform 自定义控件:半透明Loading控件
winform 自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...
- (二十二)c#Winform自定义控件-半透明窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- winform 自定义控件属性在属性面板中显示
Jan.David Nothing is impossible, the word itself says 'I'm possible'!" — Audrey Hepburn winform ...
- Winform自定义控件实例
本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...
- c#Winform自定义控件-目录
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二)c#Winform自定义控件-按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三)c#Winform自定义控件-有图标的按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十一)c#Winform自定义控件-文本框(四)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十二)c#Winform自定义控件-表格
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- SDL2 程序 编译 错误 及 解决方案
main函数应写为int main( int argc, char* args[] )而不是int main()形式 链接库时应注意顺序 mingw32;SDL2main;SDL2; ...
- X86逆向4:VMP壳内寻找注册码
本节课将讲解一下重启验证,重启验证在软件中也是非常的常见的,重启验证的原理很简单,用户在注册界面输入注册码以后程序会自动将输入的注册信息保存到配置文件中,这里可能保存到注册表,也可能使用INI文件来保 ...
- Springboot使用javaMail进行邮件发送
导入相关依赖 <!--邮件发送--> <dependency> <groupId>javax.mail</groupId> <artifactId ...
- CAS实现逻辑(JWT)
由于没有获取正规做CAS的流程,这里根据网上的资料,写了一个自己觉得还可以的方案流程,留着备用 名称介绍: token:用于验证请求是否合法 refreshToken:当token失效后,客户端发送t ...
- Python 风格指南
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/contents/ 目前个人遵循的基本规范 ...
- JS基础_break和continue
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- UVA10603Fill题解--BFS
题目链接 https://cn.vjudge.net/problem/UVA-10603 分析 经典的倒水问题,直接BFS. 对于喜闻乐见的状态判重,一开始想来个哈希函数把一个三元组映射成一个数,后面 ...
- java后台读取配置文件
前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...
- sql server 2012使用新特性offset和fetch next完成分页操作
1 select * from HumanResources.Department order by DepartmentID offset rows fetch next rows only; of ...
- R安装包的问题
package 'tswge' is not available (for R version 3.5.1) install.packages('tswge', repos="http:// ...