image = img; borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2); gPen = new Pen(c.BackColor,2); fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70)); } private static Color CalculateColor(Color front, Color back, int alpha) {
// Use alpha blending to brigthen the colors but don't use it // directly. Instead derive an opaque color that we can use. // -- if we use a color with alpha blending directly we won't be able // to paint over whatever color was in the background and there // would be shadows of that color showing through Color frontColor = Color.FromArgb(255, front); Color backColor = Color.FromArgb(255, back);
因为从.NET Compact Framework 1.0 SP2 开始才可以对控件设置ForeColor 和 BackColor属性。可以设置相关属性的控件如下:Button, Checkbox, ComboBox, DomainUpDown, Label, Listbox, ListView, NumericUpDown, RadioButton, Trackbar, TreeView你可以安装.NET Compact Framework 1.0 SP2 Redistributable 或者更高版本,下面是相关的简体中文版本的下载地址:
.NET Compact Framework 1.0 SP2 Redistributable:
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=359ea6da-fc5d-41cc-ac04-7bb50a134556.NET Compact Framework 1.0 SP3 Redistributable:
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=a5a02311-194b-4c00-b445-f92bec03032f
这是几乎所有控件的公共属性之一
this.button1.refresh();
可是没有反应,不知为何,可能还是用的系统背景色。
{
private Control control;
private bool mouse_honver = false;
private bool mouse_down = false; private Image image;
private int x1,x2,y1,y2;
private float cxImage,cyImage;
private RectangleF rectf;
private Pen borderPen;
private Pen gPen ;
private Brush fillBrush;
private Brush defualtFillBrush = SystemBrushes.Control; public CoolButton(Control c,Image img)
{
SetStyle(ControlStyles.UserPaint,true);
this.Cursor = Cursors.Hand;
control = c;
locate(c.Location);
this.Size = c.Size;
x1 = c.Location.X - 1;
y1 = c.Location.Y - 1;
x2 = c.Size.Width;
y2 = c.Size.Height;
image = img;
borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2);
gPen = new Pen(c.BackColor,2);
fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70)); } private static Color CalculateColor(Color front, Color back, int alpha)
{
// Use alpha blending to brigthen the colors but don't use it
// directly. Instead derive an opaque color that we can use.
// -- if we use a color with alpha blending directly we won't be able
// to paint over whatever color was in the background and there
// would be shadows of that color showing through
Color frontColor = Color.FromArgb(255, front);
Color backColor = Color.FromArgb(255, back);
float frontRed = frontColor.R;
float frontGreen = frontColor.G;
float frontBlue = frontColor.B;
float backRed = backColor.R;
float backGreen = backColor.G;
float backBlue = backColor.B;
float fRed = frontRed*alpha/255 + backRed*((float)(255-alpha)/255);
byte newRed = (byte)fRed;
float fGreen = frontGreen*alpha/255 + backGreen*((float)(255-alpha)/255);
byte newGreen = (byte)fGreen;
float fBlue = frontBlue*alpha/255 + backBlue*((float)(255-alpha)/255);
byte newBlue = (byte)fBlue; return Color.FromArgb(255, newRed, newGreen, newBlue); }
public Brush DefualtFillBrush
{
set
{
this.defualtFillBrush = value;
}
}
private void locate(Point p)
{
this.Location = p;
x1 = p.X;
y1 = p.Y;
control.Location = new Point(0,0);
}
protected override void OnMouseEnter(EventArgs e)
{
mouse_honver = true;
Refresh();
} protected override void OnMouseLeave(EventArgs e)
{
mouse_honver = false;
Refresh();
}
protected override void OnMouseUp(MouseEventArgs e)
{
mouse_down = false;
Refresh();
}
protected override void OnMouseDown(MouseEventArgs e)
{
mouse_down = true;
Refresh();
}
private void DefaultPaint(Graphics g)
{
CacultRectangle(g);
g.FillRectangle(this.defualtFillBrush,ClientRectangle);
g.DrawRectangle(gPen,0,0,x2-2,y2-2);
g.DrawImage(image,(rectf.Width - cxImage)/2+2,(rectf.Height - cyImage)/2+2); }
private void CacultRectangle(Graphics g)
{
rectf = g.VisibleClipBounds;
cxImage = g.DpiX * image.Width / image.HorizontalResolution;
cyImage = g.DpiY * image.Height / image.VerticalResolution;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics; if(mouse_down)
{
g.FillRectangle(Brushes.SteelBlue,x1,y1,x2,y2);
g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
g.DrawImage(image,(rectf.Width - cxImage)/2+1,(rectf.Height - cyImage)/2+1);
}
else if (mouse_honver)
{
g.FillRectangle(fillBrush,x1,y1,x2,y2);
g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
ControlPaint.DrawImageDisabled(g,image,(int)(rectf.Width - cxImage)/2+3,(int)(rectf.Height - cyImage)/2+3,SystemColors.ControlDarkDark);
g.DrawImage(image,(rectf.Width - cxImage)/2,(rectf.Height - cyImage)/2);
}
else
{
DefaultPaint(g);
} } }