面板背景图像移动 [英] Panel Background image moving

查看:72
本文介绍了面板背景图像移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有背景图像的大面板。此面板包含另一个具有相同尺寸但透明的面板。当我向下滚动此面板时,背景图像和控件一样移动一点点。当我只是把背景颜色工作正常。问题是什么?我该如何解决?

解决方案

不知道为什么会这样做但是我做了这个并且它有效......

  private   void  panel2_Scroll( object  sender,ScrollEventArgs e)
{
panel1.Refresh();
}





然而,因为missak和我发现你仍然有一些动作 - 特别是对于更大的面板。



我现在找到了这个解决方案哎哟! [ ^ ]


这是其他论坛解决方案的C#翻译......



 使用系统; 
使用 System.Windows.Forms;
使用 System.Drawing;
使用 System.Runtime.InteropServices;
public class MyPanel:Panel
{
public MyPanel()
{
this .AutoScroll = ;
this .SetStyle(ControlStyles.SupportsTransparentBackColor, true );
this .SetStyle(ControlStyles.OptimizedDoubleBuffer, true );
}

private 图片mImage;
public 图片图片
{
获取 {返回 mImage; }
set
{
mImage = value ;
Invalidate();
}
}
受保护 覆盖 void OnScroll(ScrollEventArgs se)
{
if (se.Type == ScrollEventType.First)
{
LockWindowUpdate( this .Handle);
}
else if
se.Type == ScrollEventType .ThumbTrack ||
se.Type == ScrollEventType.ThumbPosition)
{
LockWindowUpdate( IntPtr .Zero);
this .Refresh();
LockWindowUpdate( this .Handle);
}
else
{
LockWindowUpdate( IntPtr 。零);
this .Invalidate();
}
base .OnScroll(se);
}

受保护 覆盖 void OnPaint(PaintEventArgs e)
{
使用(SolidBrush br = new SolidBrush( this .BackColor))
{
e.Graphics.FillRectangle(br, .ClientRectangle);
}
if (mImage!= null
{
e.Graphics.DrawImage(mImage, 0 0 );
}
base .OnPaint(e);
}
[DllImport( user32.dll)]
private static extern bool LockWindowUpdate( IntPtr hWnd);
}



图像不再移动但是仍然有一个恼人的闪烁,因为最顶部的面板滚动...可能仍然是一些工作做


I have a large panel with a background image. This panel contains another panel with the same size but transparent. When i scroll down this panel the background image is moving a little bit as well as the controls. When I just put a background color its working fine. What is the problem? How can I fix it?

解决方案

Not sure why it''s doing it but I did this and it worked ...

private void panel2_Scroll(object sender, ScrollEventArgs e)
{
    panel1.Refresh();
}



However as missak and I have discovered you still get some movement - especially with larger panels.

I''ve now found this solution ouch![^]


Here''s the C# translation of the other forum''s solution ...

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
public class MyPanel : Panel 
{ 
	public MyPanel() 
	{ 
		this.AutoScroll = true; 
		this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
		this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
	} 
	
	private Image mImage; 
	public Image Image 
	{ 
		get { return mImage; } 
		set 
		{ 
			mImage = value; 
			Invalidate(); 
		} 
	} 
	protected override void OnScroll(ScrollEventArgs se) 
	{ 
		if (se.Type == ScrollEventType.First) 
		{ 
			LockWindowUpdate(this.Handle); 
		} 
		else if (
				se.Type == ScrollEventType.ThumbTrack || 
				se.Type == ScrollEventType.ThumbPosition) 
		{ 
			LockWindowUpdate(IntPtr.Zero); 
			this.Refresh(); 
			LockWindowUpdate(this.Handle); 
		} 
		else 
		{ 
			LockWindowUpdate(IntPtr.Zero); 
			this.Invalidate(); 
		} 
		base.OnScroll(se); 
	} 
	
	protected override void OnPaint(PaintEventArgs e) 
	{ 
		using (SolidBrush br = new SolidBrush(this.BackColor)) 
		{ 
			e.Graphics.FillRectangle(br, this.ClientRectangle); 
		} 
		if (mImage != null) 
		{ 
			e.Graphics.DrawImage(mImage, 0, 0); 
		} 
		base.OnPaint(e); 
	} 
	[DllImport("user32.dll")] 
	private static extern bool LockWindowUpdate(IntPtr hWnd);
}


The image doesn''t move any more but there is still an annoying flicker as the topmost panel scrolls ... may still be some work to do


这篇关于面板背景图像移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆