Click here to Skip to main content
11,414,561 members (66,832 online)
Click here to Skip to main content

A flicker issue in MDI applications

, 23 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Provides a fix to the flicker problem in MDI applications.

Sample Image - MDIFlickDemo.jpg

Issue

This article illustrates how to fix a common flicker problem in MDI applications when you create or activate an MDI child and an application is in maximized state. You catch a glimpse of the new activated MDI child shown at the picture above.

To reproduce the problem:

  1. Create a new MFC MDI application project.
  2. Run and maximize the application.
  3. In maximized state, create a few MDI children and try switching between them.

When you create a new or activate an inactive window, you will see the flicker.

Workaround

The solution to the problem is very simple. The flicker comes from drawing the caption, the frame and the client area of the restored MDI child. To eliminate it, override "WindowProc" in your class derived from CMDIChildWnd. For the non-client area, filter WM_NCPAINT message, and for the client area, handle WM_SIZE message like this:

if(message==WM_SIZE) // client area
{
    if(wParam==SIZE_MAXIMIZED && pChildFrame==this)
    // active and maximized
        return CMDIChildWnd::WindowProc(message, wParam, lParam);

    SetRedraw(FALSE); // disable drawing
    LRESULT ret = CMDIChildWnd::WindowProc(message, 
                   wParam, lParam); // default Window procedure
    SetRedraw(); // enable drawing
    return  ret;
}

In the previous version of this article I also filtered message 174. Ignoring this message prevents drawing non-themed caption buttons. The picture of the buttons looks like this:

Caption Buttons

This message is also sent when a window is restored. Now drawing for restored windows is disabled, so we don't need "message==174" in the "if" statement anymore.

I have also added if(message==WM_NCPAINT || message==WM_SIZE) as a first line in the window procedure. It prevents from assertion in objcore.cpp (AfxAssertValidObject) when you try to close the MDI child, the view is derived from CHtmlView and the active configuration is "Debug".

So the workaround code looks like this:

LRESULT CChildFrame::WindowProc(UINT message, 
                       WPARAM wParam, LPARAM lParam) 
{
    if(message==WM_NCPAINT || message==WM_SIZE)
    {
        BOOL bMax;
        CMDIFrameWnd* pParentFrame = (CMDIFrameWnd*)GetParentFrame();
        if(pParentFrame)
        {
            CMDIChildWnd* pChildFrame = pParentFrame->MDIGetActive(&bMax);
            if(bMax)
            {    
                if(message==WM_NCPAINT) // non client area
                    return 0;
                    
                if(message==WM_SIZE) // client area
                {
                    if(wParam==SIZE_MAXIMIZED && 
                       pChildFrame==this) // active and maximized
                      return CMDIChildWnd::WindowProc(message, wParam, lParam);

                    SetRedraw(FALSE);
                    LRESULT ret = 
                      CMDIChildWnd::WindowProc(message, wParam, lParam);
                    SetRedraw();
                    return  ret;
                }
            }
        }
    }
    return CMDIChildWnd::WindowProc(message, wParam, lParam);
}

A demo application

Check/uncheck the "FIX" button to enable/disable the fix code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Andrzej Markowski
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionA workaround solution for the flickering problem Pin
szorik31-Jul-11 23:55
memberszorik31-Jul-11 23:55 
QuestionMaximize window Pin
manojv18-Oct-07 1:51
membermanojv18-Oct-07 1:51 
GeneralSolution for Managed Code Pin
tonyt8-Jul-07 19:47
membertonyt8-Jul-07 19:47 
GeneralKeyboard navigation Pin
Marcus Deecke27-Apr-07 2:26
memberMarcus Deecke27-Apr-07 2:26 
AnswerThe Real solutions Pin
Ali Rafiee9-Feb-06 6:30
memberAli Rafiee9-Feb-06 6:30 
GeneralAlternative Pin
Gast12824-Aug-05 23:10
memberGast12824-Aug-05 23:10 
GeneralSmall correction Pin
Andrzej Markowski14-Aug-05 8:44
memberAndrzej Markowski14-Aug-05 8:44 
GeneralA correct version of the code Pin
Andrzej Markowski13-Aug-05 11:04
memberAndrzej Markowski13-Aug-05 11:04 
GeneralGood enough to say, "Seemed to have solved the flickering issue." Pin
WREY13-Aug-05 12:51
memberWREY13-Aug-05 12:51 
GeneralCase where code doesnt work Pin
picazo23-Jan-06 13:14
memberpicazo23-Jan-06 13:14 
GeneralRe: Case where code doesnt work Pin
Andrzej Markowski26-Jan-06 17:17
memberAndrzej Markowski26-Jan-06 17:17 
QuestionWouldn't LockWindowUpdate be better and easier? Pin
Jeff LeBert11-Aug-05 5:45
memberJeff LeBert11-Aug-05 5:45 
AnswerRe: Wouldn't LockWindowUpdate be better and easier? Pin
robrich12-Aug-05 5:36
memberrobrich12-Aug-05 5:36 
GeneralIn C# Pin
robrich10-Aug-05 22:05
memberrobrich10-Aug-05 22:05 
GeneralRe: In C# Pin
robrich12-Aug-05 5:26
memberrobrich12-Aug-05 5:26 
GeneralRe: In C# Pin
Tim McCurdy30-Aug-05 5:28
memberTim McCurdy30-Aug-05 5:28 
GeneralRe: In C# Pin
robrich30-Aug-05 7:22
memberrobrich30-Aug-05 7:22 
GeneralRe: In C# Pin
robrich30-Aug-05 7:30
memberrobrich30-Aug-05 7:30 
GeneralRe: In C# Pin
Tim McCurdy30-Aug-05 17:01
memberTim McCurdy30-Aug-05 17:01 
GeneralRe: In C# [modified] Pin
Ming Liu21-Jul-06 18:55
memberMing Liu21-Jul-06 18:55 
GeneralRe: In C# Pin
Yvescion20-Oct-06 12:39
memberYvescion20-Oct-06 12:39 
GeneralRe: In C# Pin
gobgob15-May-07 20:41
membergobgob15-May-07 20:41 
GeneralRe: Infragistics TabbedMDIManager flicker Pin
Mark Treveil1-Jul-07 11:56
memberMark Treveil1-Jul-07 11:56 
GeneralMy friend flicker Pin
RancidCrabtree10-Aug-05 14:10
memberRancidCrabtree10-Aug-05 14:10 
GeneralSometimes it still flickers. Pin
WREY4-Aug-05 7:57
memberWREY4-Aug-05 7:57 
QuestionRe: Sometimes it still flickers. Pin
Uwe Keim4-Aug-05 10:25
sitebuilderUwe Keim4-Aug-05 10:25 
GeneralRe: Sometimes it still flickers. Pin
Andrzej Markowski4-Aug-05 16:25
memberAndrzej Markowski4-Aug-05 16:25 
GeneralRe: Sometimes it still flickers. Pin
Andrzej Markowski4-Aug-05 16:19
memberAndrzej Markowski4-Aug-05 16:19 
GeneralRe: Sometimes it still flickers. Pin
Andrzej Markowski13-Aug-05 11:16
memberAndrzej Markowski13-Aug-05 11:16 
GeneralMuch better !! Pin
WREY13-Aug-05 12:28
memberWREY13-Aug-05 12:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.4 | Last Updated 23 Aug 2005
Article Copyright 2005 by Andrzej Markowski
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid