Problikne to při přesunu okna, při překrytí okna jiným a odsunutí, při kliknutí na button.
Udělal jsem úplně čístý app, jen s pozadim a dvema buttony, a i v tomto čístém příkladu to bliká.
#include "stdafx.h"
#include "Buttons.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
HWND hwndCB;
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
HBITMAP hBackground = NULL;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BUTTONS));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
hInst = hInstance;
LoadString(hInstance, IDC_BUTTONS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 480, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
void On_DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
HBRUSH hbPozadi = CreatePatternBrush(LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BUTTON)));
InflateRect(&lpDIS->rcItem, 0,0);
FillRect(lpDIS->hDC, &lpDIS->rcItem, hbPozadi);
SetBkMode(lpDIS->hDC, TRANSPARENT);
SetTextColor(lpDIS->hDC, 0x00A0FFFF);
switch ( lpDIS->CtlID )
{
case 1:
lpDIS->rcItem.top = lpDIS->rcItem.bottom - 50;
DrawText(lpDIS->hDC, TEXT("BUTTON A"), -1, &lpDIS->rcItem,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
break;
case 2:
lpDIS->rcItem.top = lpDIS->rcItem.bottom - 50;
DrawText(lpDIS->hDC, TEXT("BUTTON B"), -1, &lpDIS->rcItem,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
break;
}
DeleteObject(hbPozadi);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case 1:
break;
case 2:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_ERASEBKGND:
return 0;
break;
case WM_CREATE:
hBackground = (HBITMAP)LoadBitmap(hInst, MAKEINTRESOURCE(IDB_POZADI));
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
if (hBackground==NULL) {
GetClientRect(hWnd, &rt);
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
DrawText(hdc, szHello, _tcslen(szHello), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
} else {
HDC hdcMem = CreateCompatibleDC(hdc);
HGDIOBJ oldBitmap = SelectObject(hdcMem, hBackground);
BITMAP bitmap;
GetObject(hBackground, sizeof(bitmap), &bitmap);
BitBlt(hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteDC(hdcMem);
}
EndPaint(hWnd, &ps);
CreateWindowEx(NULL, L"button", TEXT("Button 1"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON | BS_OWNERDRAW, 1, 50, 160, 120, hWnd, (HMENU)1, hInst, NULL);
CreateWindowEx(NULL, L"button", TEXT("Button 2"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON | BS_OWNERDRAW, 1, 175, 160, 120, hWnd, (HMENU)2, hInst, NULL);
break;
case WM_DRAWITEM:
On_DrawItem((LPDRAWITEMSTRUCT)lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}