|
-
October 25th, 2010, 01:25 AM
#1
How to optimize painting while scrolling (flickering problem)?
To simplify scrolling operation, I built my own CScrollableWnd, which acted just as a container window, and took advantage of this CScrollHelper (http://www.codeproject.com/KB/dialog...g_support.aspx). Meanwhile, a child shadow window, which contains real content, was included in that container window. The result would be as simple as scrolling entire shadow window, without be concerning for painting content with recalculated coordinates.
It so far worked well with just one problem, it flickered quite much, visually bothering.
Here's my code sample, I put some debugging code out there,
Code:
void CScrollHelper::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// calculate what position I'm about to go
// Scroll the window if needed.
if ( deltaPos != 0 )
{
m_scrollPos.cy += deltaPos;
m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
// set SW_SCROLLCHILDREN to have shadow window scrolled
// m_attachWnd is that container window
m_attachWnd->ScrollWindowEx(0, -deltaPos, NULL, NULL, NULL, NULL, SW_INVALIDATE | SW_SCROLLCHILDREN);
}
}
void CSrollableWindow::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
OutputDebugString(_T("on parent window painting..."));
}
void CShadowWindow::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call __super::OnPaint() for painting messages
OutputDebugString(_T("on shadow window painting..."));
drawSomething(dc);
}
I printed the messages received to check out what happened, then ended up with following conclusion.
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
[3840] on shadow window painting...
[3840] on vertical scrolling...
It clearly explained why it was so flickering.
But I'm wondering that why OnPaint was immediately invoked every time. I thought it's supposed to paint only once when I finished scrolling and released mouse.
How could I do to eliminate flicker?
thx in advance
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|