您的位置:首页 > 其它

解决Winform背景图片闪烁的问题

2018-03-07 20:00 1556 查看
       Winform窗体,里面放置了一个Panel,Dock属性为Fill,BackgroundImage使用一个本地图片文件,
BackgroundImageLayout使用了Stretch。嵌入图片的Panel作为Winform应用程序的背景,这个界面现在有两个问题:1、在窗体第一次被打开时,背景图片会出现明显的闪烁2、在拉动窗体的边界以调整窗体大小时,背景图片非出现明显的闪烁  



解决方案:

需要新建一个PanelEnhanced类继承Panel类,代码如下:

C# Code:

/// <summary>
/// 加强版 Panel
/// </summary>
class PanelEnhanced : Panel
{
/// <summary>
/// OnPaintBackground 事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaintBackground(PaintEventArgs e)
{
// 重载基类的背景擦除函数,
// 解决窗口刷新,放大,图像闪烁
return;
}

/// <summary>
/// OnPaint 事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
// 使用双缓冲
this.DoubleBuffered = true;
// 背景重绘移动到此
if (this.BackgroundImage != null)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
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);
}
}
参考链接:http://www.csframework.com/ar
4000
chive/1/arc-1-20170622-2307.htm
         www.csframework.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: