Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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:
C++
#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 btnOK = CreateWindowEx( 0, WC_BUTTON, L"Сачувај",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                rcClient.right - 180,  // preporuceno + 10 px
                rcClient.bottom - 33, // preporuceno + 10 px
                75, 23, hwnd, (HMENU)4000, 
                ((LPCREATESTRUCT)lParam)->hInstance, 0 );

            HWND btnCancel = CreateWindowEx( 0, WC_BUTTON, L"Одустани",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                rcClient.right - 85,  // preporuceno + 10 px
                rcClient.bottom - 33, // preporuceno + 10 px
                75, 23, hwnd, (HMENU)4001, 
                ((LPCREATESTRUCT)lParam)->hInstance, 0 );

            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 );

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

            SendMessage( btnCancel, WM_SETFONT, 
                (WPARAM)(HFONT)GetStockObject(DEFAULT_GUI_FONT),
                (LPARAM)TRUE );
        }
        return 0L;
    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 );

            SetWindowPos( GetDlgItem( hwnd, 4000 ), NULL, 
                rcClient.right - rcClient.left - 180, 
                rcClient.bottom - rcClient.top - 33, 
                75, 23, SWP_NOZORDER | SWP_NOCOPYBITS );

            SetWindowPos( GetDlgItem( hwnd, 4001 ), NULL, 
                rcClient.right - rcClient.left - 85, 
                rcClient.bottom - rcClient.top - 33, 
                75, 23, 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.
Posted
Comments
enhzflep 22-Nov-14 20:09pm    
Not sure how much benefit it will bring, as I dont have access to an XP machine currently. Have you tried/considered using the BeginDeferWindowPos, DeferWindowPos and EndDeferWindowPos functions? Basically, their use allows you to move a bunch of windows en-masse, before updating their positions at the same time - preventing staggered window updates.
Quoting from the help: "The EndDeferWindowPos function simultaneously updates the position and size of one or more windows in a single screen-refreshing cycle."

You can also consider handling WM_WINDOWPOSCHANGING as a more efficient means to handle resizing.(More efficient since the DefWindowProc sends WM_SIZE and WM_MOVE msgs to the window -you can handle those conditions directly, without the additional sending of 2 messages)
AlwaysLearningNewStuff 22-Nov-14 23:24pm    
You know I always do my homework. I have found this message but don;t know how to apply it in my case. Maybe I am doing something wrong since it is my first time... Can you edit the WM_SIZE handler to demonstrate its proper usage? I am sorry for being pushy but I need to finish this by Tuesday morning and am panicking... Thanks for helping as always, if I ever come to Australia I will PM to share a keg of bear! Best regards.
enhzflep 23-Nov-14 21:17pm    
Indeed - never once a 'send me teh codez' post, always one to provide ample background info and methods tried already. :)

I've not made use of the WM_WINDOWPOSCHANGING message before as far as I can tell from a search of my source code directory. I have however, made use of the DeferWindow stuff. In case it's any use, here's how I'd use them:

handle = BeginDeferWindowPos(n);
1. DeferWindowPos(....)
.....
n. DeferWindowPos(....)
EndDeferWindowPos(handle);


Or, with real code (sorry for the formatting):

case WM_SIZE:
{
RECT rcClient = {0};
GetClientRect( hwnd, &rcClient );

HDWP defStruct = BeginDeferWindowPos(3);

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

//SetWindowPos( GetDlgItem( hwnd, 4000 ), NULL,
DeferWindowPos(defStruct, GetDlgItem( hwnd, 4000 ), NULL,
rcClient.right - rcClient.left - 180,
rcClient.bottom - rcClient.top - 33,
75, 23, SWP_NOZORDER | SWP_NOCOPYBITS );

//SetWindowPos( GetDlgItem( hwnd, 4001 ), NULL,
DeferWindowPos(defStruct, GetDlgItem( hwnd, 4001 ), NULL,
rcClient.right - rcClient.left - 85,
rcClient.bottom - rcClient.top - 33,
75, 23, SWP_NOZORDER | SWP_NOCOPYBITS );

EndDeferWindowPos(defStruct);

InvalidateRect( hwnd, NULL, FALSE );
}
return 0L;


The use of DeferWindowPos rather than SetWindowPos makes a noticeable difference on this system. Unfortuately, I still dont have access to a slower, XP-based rig.
Hope (though doubt, to be honest) that this code helps. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900