I think this is a basic double buffering win32 program. At the start of launching, it works well, but about 1 minute later (on my computer), it gets stuck. The time it takes to get stuck increases as you set the third parameter of SetTimer(hwnd, 1, 2, NULL)
to a bigger value.
I have googled and checked for an answer for almost two hours, but I can't find where the bug is.
Here is the code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Something Wrong!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, // window class name
TEXT("DoubleBuffer"), // window caption
WS_OVERLAPPEDWINDOW, // window style
GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CXSCREEN) / 4,
GetSystemMetrics(SM_CXSCREEN) / 3,
GetSystemMetrics(SM_CXSCREEN) / 3,
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap, moldbmp;
static HDC hdcMem;
static int cxClient, cyClient;
HDC hdc;
HMENU hMenu;
int x, y;
PAINTSTRUCT ps;
SIZE size;
static int i = 50, j = 70;
static BOOL bFlipUD = TRUE, bFlipLR = TRUE;
static BOOL bTimerRunning = TRUE;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, 750, 824);
ReleaseDC(hwnd, hdc);
moldbmp = SelectObject(hdcMem, hBitmap);
SetTimer(hwnd, 1, 2, NULL);
return 0;
case WM_TIMER:
if (bFlipUD)
{
i += 20;
if (i >= 200)
{
bFlipUD ^= TRUE;
}
}
else
{
i -= 20;
if (i <= 0)
{
bFlipUD ^= TRUE;
}
}
//
if (bFlipLR)
{
j += 20;
if (j >= 200)
{
bFlipLR ^= TRUE;
}
}
else
{
j -= 20;
if (j <= 0)
{
bFlipLR ^= TRUE;
}
}
//background
RECT rtClient;
GetClientRect(hwnd, &rtClient);
SetBkMode(hdcMem, TRANSPARENT);
HBRUSH hBrush = CreateSolidBrush(RGB(155, 155, 155));
FillRect(hdcMem, &rtClient, hBrush);
DeleteObject(hBrush);
//background
// 1
HPEN hPen = CreatePen(PS_SOLID, 7, RGB(255, 0, 0));
HPEN hOldPen = (HPEN)SelectObject(hdcMem, hPen);
MoveToEx(hdcMem, 0, i, NULL);
LineTo(hdcMem, cxClient, i);
TextOut(hdcMem, 30, i + 20, TEXT("88"), 2);
// 2
hPen = CreatePen(PS_SOLID, 7, RGB(55, 250, 0));
hOldPen = (HPEN)SelectObject(hdcMem, hPen);
MoveToEx(hdcMem, 0, i + 20, NULL);
LineTo(hdcMem, cxClient, i + 20);
TextOut(hdcMem, 70, i + 40, TEXT("1439"), 4);
// 3
hPen = CreatePen(PS_SOLID, 7, RGB(225, 150, 5));
hOldPen = (HPEN)SelectObject(hdcMem, hPen);
MoveToEx(hdcMem, j, 0, NULL);
LineTo(hdcMem, j, cyClient);
TextOut(hdcMem, 70, i + 40, TEXT("1439"), 4);
//InvalidateRect(hwnd, NULL, FALSE);
InvalidateRgn(hwnd, NULL, FALSE);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
BitBlt(hdc, 10, 10, 750, 824,
hdcMem, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1);
SelectObject(hdcMem, moldbmp);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
question from:
https://stackoverflow.com/questions/65866683/win32-program-double-buffering-doesnt-work