akeshet

Hi Guys, I've been struggling with this problem for several days now, perhaps somebody can shed some light.

I have a Panel that has a large number of controls in it (buttons etc.). The size of the panel is smaller than the area that the controls take up, so I have the panel's autoscroll property set to true, and thus when necessary the panel has scrollbars so you can get to the appropriate control.

So far, so good.

The problem is, when you scroll and thus "uncover" some of the controls in that panel that didn't used to be visible, there is a time delay before those newly visible controls get painted. This results in ugly scrolling that is hard for the user to parse.

Question 1) Is there any smart/easy way to fix this problem


I couldn't find a windows-built way to do this, surprisingly to me, so the way I've been trying to go about doing this is to write my own SmoothAutoscrollPanel class which inherits from Panel. The idea is that the SmoothAutoscrollPanel will override the OnPaint method of Panel. It will have a bitmap image of all of the controls, and then when you use the scroll bars it will just scroll around the bitmap. It seems to me like this should be very fast and should eliminate the slow-to-draw unveiled controls problem.

But I am facing problems with my overriding OnPaint. Specifically, even if I override the OnPaint method, somehow all of the child controls are getting drawn ANYWAY. I have no idea how this is possible. Let me explain with a code snippet. At the bottom of this post is an example version of SmoothAutoscrollPanel which does very little. All it does is override the OnPaint method, intentionally NOT calling the OnPaint method of the base class. Thus, I would think, none of the child controls in this panel should appear. And yet they do anyway. So:

Question 2) HOW ARE THE CHILD CONTROLS GETTING PAINTED! !

Thanks for your help,
Aviv

code snippet
-------------------
class SmoothAutoscrollPanel : Panel
{

public SmoothAutoscrollPanel()
: base()
{
SetStyle(ControlStyles.UserPaint, true);
}

protected override void OnPaint(PaintEventArgs e)
{
// Comment out the proper paint handling.
// base.OnPaint(e);
}

}



Re: Windows Forms General Smooth Scrolling and the mysteries of OnPaint

akeshet

Hi Guys, I've been struggling with this problem for several days now, perhaps somebody can shed some light.

I have a Panel that has a large number of controls in it (buttons etc.). The size of the panel is smaller than the area that the controls take up, so I have the panel's autoscroll property set to true, and thus when necessary the panel has scrollbars so you can get to the appropriate control.

So far, so good.

The problem is, when you scroll and thus "uncover" some of the controls in that panel that didn't used to be visible, there is a time delay before those newly visible controls get painted. This results in ugly scrolling that is hard for the user to parse.

Question 1) Is there any smart/easy way to fix this problem


I couldn't find a windows-built way to do this, surprisingly to me, so the way I've been trying to go about doing this is to write my own SmoothAutoscrollPanel class which inherits from Panel. The idea is that the SmoothAutoscrollPanel will override the OnPaint method of Panel. It will have a bitmap image of all of the controls, and then when you use the scroll bars it will just scroll around the bitmap. It seems to me like this should be very fast and should eliminate the slow-to-draw unveiled controls problem.

But I am facing problems with my overriding OnPaint. Specifically, even if I override the OnPaint method, somehow all of the child controls are getting drawn ANYWAY. I have no idea how this is possible. Let me explain with a code snippet. At the bottom of this post is an example version of SmoothAutoscrollPanel which does very little. All it does is override the OnPaint method, intentionally NOT calling the OnPaint method of the base class. Thus, I would think, none of the child controls in this panel should appear. And yet they do anyway. So:

Question 2) HOW ARE THE CHILD CONTROLS GETTING PAINTED! !

Thanks for your help,
Aviv

code snippet
-------------------
class SmoothAutoscrollPanel : Panel
{

public SmoothAutoscrollPanel()
: base()
{
SetStyle(ControlStyles.UserPaint, true);
}

protected override void OnPaint(PaintEventArgs e)
{
// Comment out the proper paint handling.
// base.OnPaint(e);
}

}





Re: Windows Forms General Smooth Scrolling and the mysteries of OnPaint

Chunsheng Tang - MSFT

Hi,

I have a Panel that has a large number of controls in it (buttons etc.).

The first thing to consider would be whether it is necessary to use so many controls. Is it possible to reduce the number For example, use only part of them one time.

It seems that you have tried to paint those controls in the paint event of the Panel, which only painted the surface of the panel but not those controls. The controls paint their surfaces themselves but not by the Panel. If you want to suppress their surfaces, you need to hide them, which means you cannot use them.

Depending on the requirements, you may consider discarding all these controls and do the hit-testing yourself.

Best Regards

Chunsheng Tang






Re: Windows Forms General Smooth Scrolling and the mysteries of OnPaint

Yu Guo – MSFT

Hi, akeshet,

Based on my understanding, you want to make a smooth scrolling panel, don't you

I think you should not change the OnPaint method, but do that job in OnScroll method.

For example,

Code Block

public class SmoothAutoscrollPanel : Panel

{

protected override void OnScroll(ScrollEventArgs se)

{

if (se.ScrollOrientation == ScrollOrientation.VerticalScroll)

{

this.VerticalScroll.Value = se.NewValue;

}

else if (se.ScrollOrientation == ScrollOrientation.HorizontalScroll)

{

this.HorizontalScroll.Value = se.NewValue;

}

}

}

In fact, in the panel's OnPaint method, you can only deal with the drawings for the panel itself.

All the controls on the panel draw themselves in their own OnPaint method (Or even drawn by Win32 API).

May this solve your second question.

More info

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.onpaint(VS.80).aspx

Hope this helps,

Regards






Re: Windows Forms General Smooth Scrolling and the mysteries of OnPaint

boomhauer

Im fighting this exact problem, would love to hear some good input on this.

recap: panel control with a bunch of child controls (which are usercontrols, which contain a couple labels)

scrolling hte panel causes the controls to be repiainted pretty slowly, doesnt "smoothly" scroll the autoscroll panel when dragging the control box.