Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 6-Feb-13 5:11am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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![^]
  Permalink  
v2
Comments
missak boyajian at 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 at 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 at 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 at 7-Feb-13 4:13am
   
Haven't tried it yet - hence the "ouch!" If I get time today I'll have a go
CHill60 at 7-Feb-13 4:55am
   
I've added to the solution
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
missak boyajian at 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 at 7-Feb-13 6:52am
   
I'm not having much luck improving the solution I'm afraid
CHill60 at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100