3

I have returned 1L from WM_ERASEBKGND handler, have handled WM_SIZE by adding InvalidateRect( hWnd, NULL, FALSE ); and have removed CS_HREDRAW | CS_VREDRAW style from my window class.

Flickering is most visible on the tab control. Here is a small demo that illustrates the problem:

1.) Create empty C++ project in Visual Studio.

2.) Create header pomocne_funkcije.h and copy/paste the following:

#include <windows.h>
#include <windowsx.h>
#include <comutil.h>
#include <commctrl.h>
#include <stdio.h>
#include <vector>
#include <ole2.h>
#include <string>
#include <stdlib.h>
#include <locale.h>
#include <Uxtheme.h>

#pragma comment( linker, "/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
language='*'\"")

#pragma comment( lib, "comctl32.lib")
#pragma comment( lib,"Msimg32.lib")
#pragma comment( lib, "comsuppw.lib")
#pragma comment( lib, "UxTheme.lib")

3.) Create .cpp file named main.cpp and copy/paste the following:

#include "pomocne_funkcije.h"

static HINSTANCE hInst;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        {
            RECT rcClient = {0};
            ::GetClientRect( hwnd, &rcClient );

            HWND hwndTab = CreateWindowEx( 0, WC_TABCONTROL, 
                L"Ugovori", WS_CHILD | WS_VISIBLE, 
                10, 10, rcClient.right - rcClient.left - 20,
                rcClient.bottom - rcClient.top - 63, 
                hwnd, (HMENU)3000, 
                ((LPCREATESTRUCT)lParam)->hInstance, 0 );

            TCITEM tci = {0};

            tci.mask = TCIF_TEXT;
            tci.pszText = L"Основни подаци";
            TabCtrl_InsertItem( hwndTab, 0, &tci );

            tci.pszText = L"Анекси";
            TabCtrl_InsertItem( hwndTab, 1, &tci );

            tci.pszText = L"Подиѕвођачки радови";
            TabCtrl_InsertItem( hwndTab, 2, &tci );

            SendMessage( hwnd, WM_SETFONT, 
                (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
                (LPARAM)TRUE );

            SendMessage( hwndTab, WM_SETFONT, 
                (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
                (LPARAM)TRUE );

        }
        return 0L;
    case WM_MOVE:
    case WM_MOVING:
    case WM_SIZING:
    case WM_SIZE:
        {
            RECT rcClient = {0};
            GetClientRect( hwnd, &rcClient );

            SetWindowPos( GetDlgItem( hwnd, 3000 ), NULL, 
                rcClient.left + 10, rcClient.top + 10, 
                rcClient.right - rcClient.left - 20, 
                rcClient.bottom - rcClient.top - 63,
                SWP_NOZORDER | SWP_NOCOPYBITS );

            InvalidateRect( hwnd, NULL, FALSE );
        }
        return 0L;
    case WM_ERASEBKGND:
        return 1L;
    case WM_PAINT:
        {
            PAINTSTRUCT ps = {0};
            HDC hdc = BeginPaint( hwnd, &ps );
            SendMessage( hwnd, WM_PRINTCLIENT, (WPARAM)hdc, 0 );
            EndPaint( hwnd, &ps );
        }
        return 0L;
    case WM_PRINTCLIENT:
        {
            RECT rcClient = {0};
            GetClientRect( hwnd, &rcClient );

            FillRect( (HDC)wParam, &rcClient, 
                (HBRUSH)GetStockObject(WHITE_BRUSH) );
        }
        return 0L;
    case WM_CLOSE:
        ::DestroyWindow(hwnd);
        return 0L;
    case WM_DESTROY:
        ::PostQuitMessage(0);
        return 0L;
    default:
        return ::DefWindowProc( hwnd, msg, wParam, lParam );
    }
    return 0;
}

// WinMain

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                   int nCmdShow)
{
    // store hInstance in global variable for later use

    hInst = hInstance;

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // initialize common controls

    INITCOMMONCONTROLSEX iccex;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_UPDOWN_CLASS | 
       ICC_STANDARD_CLASSES | ICC_TAB_CLASSES;
    InitCommonControlsEx(&iccex);

    // register main window class

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInst;
    wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wc.lpszMenuName = NULL;
    wc.lpszClassName = L"Main_Window";
    wc.hIconSm = LoadIcon( hInstance, IDI_APPLICATION );

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, 
            L"Window Registration Failed!", L"Error!", 
            MB_ICONEXCLAMATION | MB_OK);

        return 0;
    }

    // create main window

    hwnd = CreateWindowEx( 0, L"Main_Window", 
        L"Contract manager", 
        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        NULL, NULL, hInstance, 0 );

    if(hwnd == NULL)
    {
        MessageBox(NULL, L"Nemogu da napravim prozor!", L"Greska!",
            MB_ICONEXCLAMATION | MB_OK);

        return 0; 
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    return Msg.wParam;
}

I am working in Visual Studio 2008 on Windows XP using C++ and WinAPI.

QUESTION:

How can I remove flickering from the tab control ( and the rest of my window ) ?

Thank you. Best regards.

AlwaysLearningNewStuff
  • 2,939
  • 3
  • 31
  • 84
  • 2
    Why are you resizing your tab control on `WM_MOVE`/`WM_MOVING`? It won't have changed size, all that means is the parent window has moved. Also you can ignore `WM_SIZING` and just look for `WM_SIZE`. If I take out the three unnecessary case statements it doesn't flicker for me at all on Windows 8 when resizing the window. – Jonathan Potter Nov 26 '14 at 19:53
  • 1
    Jonathan is correct - you are handling messages that you should not be; you are literally forcing the flicker by making the redraw occur multiple times for each intermediate size change. – Jeff D. Nov 27 '14 at 14:46
  • @JonathanPotter: I have removed mentioned messages, but tab control flickers badly when resized. I have added simple button at the bottom of the window, and it does not flicker. I think that the problem is in tab control, but then again you said everything is OK on Windows 8... I don't know what to do, so I will Google for "win32 tab control flicker on xp", hoping to find a solution... – AlwaysLearningNewStuff Nov 27 '14 at 21:35
  • @JeffD.: I have removed mentioned messages, but tab control flickers badly when resized. I have added simple button at the bottom of the window, and it does not flicker. I think that the problem is in tab control... I don't know what to do, so I will Google for "win32 tab control flicker on xp", hoping to find a solution... – AlwaysLearningNewStuff Nov 27 '14 at 21:36
  • 1
    Some controls just flicker and there's nothing you can do about it easily - I've spent weeks on this myself in the past. The best solution I've come up with is to use `SWP_NOREDRAW` when resizing elements, draw the whole parent window's client area into a memory bitmap using `WM_PRINTCLIENT`, and then blit it to the screen (double-buffering it, effectively). But this has problems of its own, since not all controls support `WM_PRINTCLIENT` (or have problems even if they do), which means you still need lots of little kludges to make it work right. – Jonathan Potter Nov 27 '14 at 21:38
  • @JonathanPotter: Well, the problem is that child dialog boxes will flicker too ( I have tested this ) which is really annoying and looks unprofessional. To be honest, slight flicker of tab control is not a problem, but when its content starts to flicker then that is unacceptable behavior that I must avoid by all means ( +1 for sharing your experiences ). – AlwaysLearningNewStuff Nov 27 '14 at 22:03
  • @JeffD.: I have just stumbled upon [this](https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting) article. Adding `WS_EX_COMPOSITED` removes flicker altogether! Now I am going through the rest of the article, so I ask you as being better + more experienced developer to try and find the downsides of this solution ( if any ). I apologize for being annoying, but this is really important to me. Best regards. – AlwaysLearningNewStuff Nov 27 '14 at 22:11
  • @JonathanPotter: I have just stumbled upon [this](https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting) article. Adding `WS_EX_COMPOSITED` removes flicker altogether! Now I am going through the rest of the article, so I ask you as being better + more experienced developer to try and find the downsides of this solution ( if any ). I apologize for being annoying, but this is really important to me. Best regards. – AlwaysLearningNewStuff Nov 27 '14 at 22:12
  • If it works for you that's great; when I've tried `WS_EX_COMPOSITED` in the past it didn't seem to solve it completely. The MSDN [Extended Window Styles](http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543%28v=vs.85%29.aspx) page has this note at the bottom of it: `WS_EX_COMPOSITED appears to work fine until you minimize then restore the window. Then the child windows will start flashing. To avoid flashing do not set the WS_EX_COMPOSITED style, do your own double buffering, and ignore the WM_ERASEBKGND message. This is a know issue with Microsoft. ` – Jonathan Potter Nov 27 '14 at 22:22
  • @JonathanPotter: I have ran the app, minimize / restore it -> no flashing on both XP and Windows 8. Still, on Windows 8 flickering is still present. It is as if I never set the `WS_EX_COMPOSITED` style at first place. Back to the drawing board :sigh: – AlwaysLearningNewStuff Nov 27 '14 at 22:34

2 Answers2

1

I don't think you need the line:

InvalidateRect( hwnd, NULL, FALSE );

Since there is already a forced repaint when you resize the form. I removed it and the window still displays and redraws fine without the flicker.

Aaron
  • 151
  • 3
  • Tried it, and tab control still flickers... :( – AlwaysLearningNewStuff Dec 02 '14 at 04:24
  • Do you need to enable visual styles for your application? Does it still flicker when they are disabled, by not including UxTheme.lib or calling: SetWindowTheme(hwnd, L" ", L" "); as per http://msdn.microsoft.com/en-us/library/windows/desktop/bb773175%28v=vs.85%29.aspx#turnoff – Aaron Dec 02 '14 at 19:00
  • I have enabled Visual Styles. Disabling Visual Styles is not an option but I understand your point-> I will try and see what happens when I turn off Visual Styles and report my results. Although your answer doesn't solve my problem, I have awarded you the bounty as a token of gratitude for your efforts. Best regards. – AlwaysLearningNewStuff Dec 02 '14 at 23:04
  • Thanks, I hope you find a solution that works for ya. – Aaron Dec 03 '14 at 05:41
0

Make below changes : wc.hbrBackground = nullptr;

It will internally pass WM_ERASEBKGND message which will erase the background and flickering will not happen.

lorro
  • 10,687
  • 23
  • 36
Chetana
  • 11
  • 2