使用双缓冲来减少闪烁现象
相关连接
http://www.microsoft.co.ke/china/msdn/archives/library/dnnetcomp/html/imagebutton.asp
绘制位图时需要聪明些
http://www.microsoft.co.ke/china/msdn/archives/library/dnnetcomp/html/imagebutton.asp
protected override void OnPaint(PaintEventArgs e )
{
Graphics gxOff; //屏幕外的图像
if (m_bmpOffscreen == null) //要双缓冲的位图
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
//绘制一些位图
gxOff.DrawImage(bmpParent, 0, 0, bmpRect, GraphicsUnit.Pixel);
//边界矩形
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
//绘制边界
gxOff.DrawRectangle(new Pen(Color.Black), rc);
//从内存位图绘制
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
{
Graphics gxOff; //屏幕外的图像
if (m_bmpOffscreen == null) //要双缓冲的位图
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
//绘制一些位图
gxOff.DrawImage(bmpParent, 0, 0, bmpRect, GraphicsUnit.Pixel);
//边界矩形
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
//绘制边界
gxOff.DrawRectangle(new Pen(Color.Black), rc);
//从内存位图绘制
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
绘制位图时需要聪明些
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Bitmap m_bmpOffscreen = null;
Graphics gxOff; //Offscreen graphics
Rectangle imgRect; //image rectangle
Brush backBrush; //brush for filling a backcolor
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
if (!bPushed)
backBrush = new SolidBrush(this.BackColor);
else //change the background when it's pressed
backBrush = new SolidBrush(Color.LightGray);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
if (this.mImage != null)
{
//Center the image relativelly to the control
int imageLeft = (this.Width - mImage.Width) / 2;
int imageTop = (this.Height - mImage.Height) / 2;
if (!bPushed)
{
imgRect = new Rectangle(imageLeft, imageTop, mImage.Width,mImage.Height);
}
else //The button was pressed
{
//Shift the image by one pixel
imgRect = new Rectangle(imageLeft + 1, imageTop + 1, mImage.Width,
mImage.Height);
}
//设置色键(透明范围)
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(BackgroundImageColor(mImage),
BackgroundImageColor(mImage));//以图片的背景作为透明范围
//Draw image
gxOff.DrawImage(mImage, imgRect, 0, 0, mImage.Width, mImage.Height,
GraphicsUnit.Pixel, imageAttr);
}
if (bPushed) //The button was pressed
{
//Prepare rectangle
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
//Draw rectangle
gxOff.DrawRectangle(new Pen(Color.Black), rc);
}
gxOff.DrawRectangle(Pens.Black,0,0,this.Width-1,this.Height-1);
switch(this._drawKind)
{
case DrawKinds.Selected:
gxOff.DrawRectangle(Pens.Red,1,1,this.Width-3,this.Height-3);
break;
}
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
/// <summary>
/// 获取图片的背景色
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap(image);
return bmp.GetPixel(0, 0);//取第一个点的颜色作为其背景色
}
{
Bitmap m_bmpOffscreen = null;
Graphics gxOff; //Offscreen graphics
Rectangle imgRect; //image rectangle
Brush backBrush; //brush for filling a backcolor
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
if (!bPushed)
backBrush = new SolidBrush(this.BackColor);
else //change the background when it's pressed
backBrush = new SolidBrush(Color.LightGray);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
if (this.mImage != null)
{
//Center the image relativelly to the control
int imageLeft = (this.Width - mImage.Width) / 2;
int imageTop = (this.Height - mImage.Height) / 2;
if (!bPushed)
{
imgRect = new Rectangle(imageLeft, imageTop, mImage.Width,mImage.Height);
}
else //The button was pressed
{
//Shift the image by one pixel
imgRect = new Rectangle(imageLeft + 1, imageTop + 1, mImage.Width,
mImage.Height);
}
//设置色键(透明范围)
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(BackgroundImageColor(mImage),
BackgroundImageColor(mImage));//以图片的背景作为透明范围
//Draw image
gxOff.DrawImage(mImage, imgRect, 0, 0, mImage.Width, mImage.Height,
GraphicsUnit.Pixel, imageAttr);
}
if (bPushed) //The button was pressed
{
//Prepare rectangle
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
//Draw rectangle
gxOff.DrawRectangle(new Pen(Color.Black), rc);
}
gxOff.DrawRectangle(Pens.Black,0,0,this.Width-1,this.Height-1);
switch(this._drawKind)
{
case DrawKinds.Selected:
gxOff.DrawRectangle(Pens.Red,1,1,this.Width-3,this.Height-3);
break;
}
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
/// <summary>
/// 获取图片的背景色
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap(image);
return bmp.GetPixel(0, 0);//取第一个点的颜色作为其背景色
}