Gold mine of Visual C++ tricks!
How to update bulk amount of data to GUI controls without flicker.
![]()
Usually for Listbox and Tree control, may be you need to add and delete thousands of entries. While doing so, its obvious that the control will flicker. It might be annoying to the user. So how can you avoid it?
![]()
You can use the api – LockWindowUpdate() and UnlockWindowUpdate() for remove the flicker while updating the UI. If a window is locked by calling LockWindowUpdate(), the the further call for getting the device context will return one with empty visible region. When you call UnlockWindowUpdate(), system invalidates the area and will send WM_PAINT. The WM_PAINT will be send only if some drawing is done to the window after locking.
You can download sample from here, which shows the difference.
See sample code snippet below.
// Lock the EditBox.
m_EditBox.LockWindowUpdate();
// Add any number of strings to it.
for( DWORD Index = 0; Index < 5000; ++Index )
{
CString csMessage;
csMessage.Format( _T("Item %d"), Index );
m_EditBox.AddString( csMessage );
}
// After updating, unlock it.
m_EditBox.UnlockWindowUpdate();
![]()
Please note that, only one window can be locked at a time.
![]()
Targeted Audience – Beginners.
| Print article | This entry was posted by Jijo Raj on April 19, 2008 at 6:45 am, and is filed under Codeproject, MFC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |