找到的别人的代码,很好用,把Panel类替换成BackGroundPanelTrend就可以了,但奇怪的是为什么图片右和下会有两道黑边,把width和height各加了200,解决问题了,好像是没画全??郁闷了
原文链接:http://www.cublog.cn/u2/67893/showart_2267690.html
重写Pannel控件
给BackGroundPanelTrend 对象的背静图片初始化一张图片
public class BackGroundPanelTrend : Panel
{
// Don't use the Paint-event like this: panel.Paint += ...
//Instead of using a Panel, create a custom control class and override the OnPaint-method. To reduce flickering, also override the OnPaintBackground-method.
// using System.Drawing;
//using System.Windows.Forms;
//public class RenderControl : Control
//{
// protected override void OnPaint(PaintEventArgs e)
// {
// // render-code goes here
// base.OnPaint(e);
// }
// protected override void OnPaintBackground(PaintEventArgs e)
// {
// // do nothing here: doesn't paint background => no flickering
// }
//}
//
//Then use the RenderControl in your form, like any other control.
//
protected override void OnPaintBackground(PaintEventArgs e)
{
//
// 重载基类的背景擦除函数,
// 解决窗口刷新,放大,图像闪烁
// do nothing here: doesn't paint background => no flickering
return;
}
protected override void OnPaint(PaintEventArgs e)
{
// render-code goes here
this.DoubleBuffered = true;
if (this.BackgroundImage != null)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// e.ClipRectangle
e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height ),
0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height,
System.Drawing.GraphicsUnit.Pixel);
}
base.OnPaint(e);
}
}
//********************************************************************************************
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel_wholeFrameTrend = new BackGroundPanelTrend();
}
BackGroundPanelTrend panel_wholeFrameTrend;
//***************************************************************************************