必备类库
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
重载控件的话,应该都有、都有。
代码示例
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.MyChecked)
{
Graphics g = e.Graphics;
int left = this.ContentRectangle.Left;
int top = this.ContentRectangle.Top;
int width = this.ContentRectangle.Width;
int height = this.ContentRectangle.Height;
float deltaHeight = 1;
//g.Clear(Color.FromArgb(0, 255, 255, 255));
for (int i = 0; i < height; i++)
{
Color brushColor = Color.FromArgb(
(int)(Math.Pow(2, ((double)i * (topColor - bottomColor)) / height + bottomColor) - 1),
checkedbackgroundColor.R,
checkedbackgroundColor.G,
checkedbackgroundColor.B);
SolidBrush opcBrush = new SolidBrush(brushColor);
g.FillRectangle(opcBrush, left, top + i * deltaHeight, width, 1);
}
//g.DrawImage(this.Image, this.ContentRectangle);
}
}
private Color checkedbackgroundColor = Color.FromArgb(255, 255, 128, 0);
[Category("设置"), Description("背景颜色底色")]
public Color HightLightBackgroundColor
{
get { return checkedbackgroundColor; }
set {
checkedbackgroundColor = value;
Invalidate();
}
}
private int topColor = 8;
[Category("设置"), Description("背景色上限")]
public int TopTransparent
{
get { return topColor; }
set
{
if (value >= 8)
topColor = 8;
if (value <= 0)
topColor = 0;
else
topColor = value;
Invalidate();
}
}
private int bottomColor = 0;
[Category("设置"), Description("背景色下限")]
public int BottomTransparent
{
get { return bottomColor; }
set {
if (value >= 8)
bottomColor = 8;
if (value <= 0)
bottomColor = 0;
else
bottomColor = value;
Invalidate();
}
}
[Category("设置"), Description("是否被选中")]
public bool MyChecked
{
get { return this.Checked; }
set
{
this.Checked = value;
Invalidate();
}
}
}
这里我闲的做了个颜色渐变,哈哈。于是OnPaint里我使用了好多浮点运算,控件Size太大的话效率可能不会太高,可以考虑挪出来。
注意事项
Graphics.Clear(Color);
//使用这会覆盖控件上的所有的Image、backColor、foreColor等颜色,并设置Color为背景色,Color透明时,背景色为黑色。
//在底色杂乱,或为了屏蔽某些信息时,可以使用
//A 代表下面图层的颜色值;
//B 代表上面图层的颜色值;
//C 代表混合图层的颜色值;
//d 表示上面图层的透明度。
//不透明度 C=d×A+(1-d)×B
this.ContentRectangle;
//不是所有控件都会有
//比如Button 就只有 this.DisplayRectangle this.ClientRectangle