title: Win32 program double buffering doesn't work
body: <p>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 <code>SetTimer(hwnd, 1, 2, NULL)</code> to a bigger value.</p>
  <p>I have googled and checked for an answer for almost two hours, but I can't find where the bug is.</p>
  <p>Here is the code:</p>
  <pre><code>#include &lt;windows.h&gt;
  
  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
      PSTR szCmdLine, int iCmdShow)
  {
      static TCHAR szAppName[] = TEXT(&quot;HelloWin&quot;);
      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(&amp;wndclass))
      {
          MessageBox(NULL, TEXT(&quot;Something Wrong!&quot;),
              szAppName, MB_ICONERROR);
          return 0;
      }
  
      hwnd = CreateWindow(szAppName,      // window class name
          TEXT(&quot;DoubleBuffer&quot;), // 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(&amp;msg, NULL, 0, 0))
      {
          TranslateMessage(&amp;msg);
          DispatchMessage(&amp;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 &gt;= 200)
              {
                  bFlipUD ^= TRUE;
              }
          }
          else
          {
              i -= 20;
              if (i &lt;= 0)
              {
                  bFlipUD ^= TRUE;
              }
          }
          //
          if (bFlipLR)
          {
              j += 20;
              if (j &gt;= 200)
              {
                  bFlipLR ^= TRUE;
              }
          }
          else
          {
              j -= 20;
              if (j &lt;= 0)
              {
                  bFlipLR ^= TRUE;
              }
          }
  
          //background
          RECT rtClient;
          GetClientRect(hwnd, &amp;rtClient);
          SetBkMode(hdcMem, TRANSPARENT);
          HBRUSH hBrush = CreateSolidBrush(RGB(155, 155, 155));
          FillRect(hdcMem, &amp;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(&quot;88&quot;), 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(&quot;1439&quot;), 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(&quot;1439&quot;), 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, &amp;ps);
          BitBlt(hdc, 10, 10, 750, 824,
              hdcMem, 0, 0, SRCCOPY);
  
          EndPaint(hwnd, &amp;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);
  }
  </code></pre>