【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 ...
随机推荐
- #undef取消宏定义
如果你想定义这个宏那就#define X 如果你不想让你已经#define X的宏在其他地方由于引入这个包含宏定义的.h文件而引起一些编译问题,那你就#undef X掉,就这么简单. 举个简单的例子: ...
- winform中使用TextBox滚动显示日志信息
代码如下: private void ShowInfo(string msg) { this.BeginInvoke((Action)(() => { textBox1.AppendText(s ...
- spark教程(16)-Streaming 之 DStream 详解
DStream 其实是 RDD 的序列,它的语法与 RDD 类似,分为 transformation(转换) 和 output(输出) 两种操作: DStream 的转换操作分为 无状态转换 和 有状 ...
- charindex函数的用法
例一: CustomName包含客户的First Name和Last Name,它们之间被一个空格隔开.我们用CHARINDX函数确定两个名字中间空格的位置.通过这个方法,我们可以分析ContactN ...
- 向量运算(lua,三维) 点乘、叉乘、模、夹角
向量运算在游戏制作中经常用到,稍微总结一下. 一.点乘 如图,假设 向量a与b的点乘表示a在b上的投影与b的模的乘积 公式: 代码: function MathHelper.GetVector3D ...
- Open API
OAuth和SSO都可以做统一认证登录,但是OAuth的流程比SSO复杂.SSO只能做用户的认证登录,OAuth不仅能做用户的认证登录,开可以做open api开放更多的用户资源. Open API即 ...
- spring-cloud 学习一 介绍
微服务Microservice,跟之相对应的是将功能从开发到交付都打包成一个很大的服务单元,一般称之为Monolith,也称「巨石」架构.微服务实现和实施思路更强调功能单一,服务单元小型化和微型化,倡 ...
- Java lesson18homework
package com.xt.lesson19; import java.util.Scanner;import java.util.Set;import java.util.TreeSet; /** ...
- 【html】 iframe 和 frameset 的区别
一.两者的相同点和不同点 相同点: iframe 和frameset都是html布局的框架布局 不同点: iframe是一个内联框架,是在页面里生成内部框架 frameset定义一个框架集,包含多个子 ...
- 佳能单反SDK 步骤
EdsInitializeSDK(); EdsGetCameraList(&eclr);//获取相机列表 EdsGetChildCount(eclr, &camCount); //获 ...