Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

Not sure why it's doing it but I did this and it worked ...
C#
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![^]
 
Share this answer
 
v2
Comments
missak boyajian 6-Feb-13 10:30am    
Thanks it's much better but there is still just a little movement. How can I keep the image constant? I am scrolling the other panel, why it should interfere the background panels image.
CHill60 6-Feb-13 10:55am    
I got the same when I made my panel bigger. See my solution - I found a very similar problem (with answer!!) on another forum
missak boyajian 6-Feb-13 13:00pm    
Do I have to override Panel and create a new class ? I saw the solution but it was written in visual basic. Did you try in c#?
CHill60 7-Feb-13 4:13am    
Haven't tried it yet - hence the "ouch!" If I get time today I'll have a go
CHill60 7-Feb-13 4:55am    
I've added to the solution
Here's the C# translation of the other forum's solution ...

C#
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
 
Share this answer
 
Comments
missak boyajian 7-Feb-13 6:50am    
Yes Thanks it worked.The image is not moving, but when I scroll down it's not smooth,it's doing slowly. Also I can't do anything on the panel until something is refreshed. For example, in microsoft word, there is a blue image background and when you scroll down nothing happens. How is it done ? :)
CHill60 7-Feb-13 6:52am    
I'm not having much luck improving the solution I'm afraid
CHill60 7-Feb-13 8:29am    
@downvoter ... care to explain why you down-voted my solutions?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900