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.