title: WinApi drawing in every frame
body: <p>I want to draw a sine wave on every frame. In every frame, after drawing, the angle will be increased and I want to have a smooth wave moving effect, but I don't know how to achieve drawing in every frame.</p>
  <p>This is my code:</p>
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;cmath&gt;
  #include &lt;iostream&gt;
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  
  HWND hwnd;
  float t = 0.0f;
  
  HANDLE tickThreadHandle;
  
  DWORD WINAPI tickThreadProc(HANDLE handle)
  {
      Sleep(50);
      ShowWindow(hwnd, SW_SHOW);
  
      PAINTSTRUCT ps;
      HDC hdc = BeginPaint(hwnd, &amp;ps);
  
      RECT r;
      GetWindowRect(hwnd, &amp;r);
  
      HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
      HPEN hOldPen = static_cast&lt;HPEN&gt;(SelectObject(hdc, hPen));
  
      int delay = 1000 / 50;
  
      while (true)
      {
          for (int x = 0; x&lt;r.right; x += 5, t += 0.3f)
          {
              LineTo(hdc, x, sin(t) * 50 + 200);
          }
  
          t += 0.1f;
          Sleep(delay);
      }
  
      SelectObject(hdc, hOldPen);
      DeleteObject(hPen);
      EndPaint(hwnd, &amp;ps);
  }
  
  int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
  {
      LPCTSTR lpszClassName = L&quot;WNDCLASS&quot;;
  
      WNDCLASSEX wcex;
      ZeroMemory(&amp;wcex, sizeof(wcex));
  
      wcex.cbSize = sizeof(wcex);
      wcex.hInstance = hInstance;
      wcex.lpszClassName = lpszClassName;
      wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
      wcex.style = CS_DBLCLKS;
      wcex.lpfnWndProc = WndProc;
      wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 150));
  
      if (!RegisterClassEx(&amp;wcex))
      {
          return -1;
      }
  
      hwnd = CreateWindowEx(
          NULL,
          lpszClassName,
          L&quot;WINDOW&quot;,
          WS_OVERLAPPEDWINDOW,
          0,
          0,
          400,
          400,
          HWND_DESKTOP,
          NULL,
          hInstance,
          NULL
      );
  
      if (hwnd == NULL)
      {
          return -2;
      }
  
      //ShowWindow(hwnd, nCmdShow);
      UpdateWindow(hwnd);
      //InvalidateRect(hwnd, NULL, TRUE);
  
      MSG msg;
      //SendMessage(hwnd, WM_PAINT, NULL, NULL);
  
      while (GetMessage(&amp;msg, hwnd, 0, 0) &gt; 0)
      {
          /*RedrawWindow(hwnd, &amp;windowRect, NULL, RDW_INTERNALPAINT);*/
          DispatchMessage(&amp;msg);
      }
  
      return (int)msg.wParam;
  }
  
  LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  {
      switch (uMsg)
      {
          case WM_CREATE:
          {
              tickThreadHandle = CreateThread(NULL, NULL, &amp;tickThreadProc, NULL, NULL, NULL);
          }
          case WM_DESTROY:
          {
              PostQuitMessage(NULL);
          }
          //case WM_PAINT:
          //{
  
          //}
          default:
          {
              return DefWindowProc(hwnd, uMsg, wParam, lParam);
          }
      }
  
      return 0;
  }
  </code></pre>
  <p>I tried many strategies, in my last attempt I was trying to do another thread, and draw from there, but my window does not even show. I was trying to send to my window messages <code>WM_PAINT</code> too, or using <code>RedrawWindow</code> function but that gave me nothing. I know that I could use it in wrong way, please correct me and give me a hint what I could do.</p>