Changing the 'invalid' area from within OnPaint handler
-
Hi all, I have a custom control, derived from System.Windows.Forms.UserControl. I'm using AutoScroll in this control to provide scrolling. I've overridden OnPaint in my control to do the drawing of the control. When the user scrolls, an Invalidate() is done on the control with the area that is newly exposed so that the invalidated area is as small as possible (this is done by the framework, not by me). Now my problem is that I want to update more of the control than just the part that is scrolled into view. I see two possibilities: - Update the whole control when the user scrolls. Unfortunately I don't see a way to tell ScrollableControl (the class that UserControl is derived from that provides scrolling capabilities) to update the whole control when scrolling; I also don't see a way to get to the scrollbars directly to connect to a Scrolled() event (as confirmed in the windows.forms Petzold). - Change the invalidated region from within the OnPaint handler. If I could find a way to tell the control to redraw everything and not just the rectangle that was passed to Invalidate() I could do that at the top of my OnPaint() member and be done. But no matter what I try, it seems I cannot update the invalidated area without calling Invalidate(), which will also send a WM_PAINT - I don't want since I'm already in OnPaint and that will create an infinite loop! The hack I came up with is something like this:
private bool m_DoingSecondOnPaint = false;
protected override void OnPaint(PaintEventArgs pea)
{
if (!m_DoingSecondOnPaint) {
m_DoingSecondOnPaint = true;
Invalidate();
} else {
m_DoingSecondOnPaint = false;
}... do rest of drawing here ...
}but as you can see this means that all the drawing code is executed twice for every time I want to draw the control. Is there a better way to do this? cheers & thanks, roel