I have return ed 1L from the WM_ERASEBKGND handler, processed WM_SIZE , adding InvalidateRect( hWnd, NULL, FALSE ); and deleting the CS_HREDRAW | CS_VREDRAW style CS_HREDRAW | CS_VREDRAW CS_HREDRAW | CS_VREDRAW from my window class.
Flicker is most noticeable in the tab control. Here is a small demo that illustrates the problem:
1.) Create an empty C++ project in Visual Studio.
2.) Create a pomocne_funkcije.h header 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 a .cpp file called 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 work in Visual Studio 2008 on Windows XP using C++ and WinAPI .
Question:
How to remove flicker from a tab control (and the rest of my window)?
Thanks. Best wishes.