title: Win32 C++ repaint the window
body: <p>I've read quite a bit about the subject, but either</p>
  
  <p>A. I'm not quite sure how to use the solution, or 
  B. The solution doesn't work.  </p>
  
  <p>For the sake of my own learning, I'm trying to make a draggable box in a C++ window.  I originally draw the rectangle through the <code>WM_PAINT</code> message in <code>LRESULT CALLBACK WndProc</code>:</p>
  
  <pre><code>case WM_PAINT:
      hdc = BeginPaint(hWnd, &amp;ps);
  
      DrawRect(hdc, 0, 0, width, 20, RGB(60, 60, 60));
  
      DrawRect(hdc, boxx, boxy, boxx + 100, boxy + 20, RGB(0, 100, 255));
  
      EndPaint(hWnd, &amp;ps);
      break;
  </code></pre>
  
  <p>I'm pretty sure my DrawRect function is working just fine, as it does draw the box.  </p>
  
  <p>To make the box draggable, I placed a method, I first define the point of the cursor relative to the window:</p>
  
  <pre><code>POINT pt;
  pt.x = ((int)(short)LOWORD(lParam));
  pt.y = ((int)(short)HIWORD(lParam));
  ScreenToClient(hWnd, &amp;pt);
  </code></pre>
  
  <p>Then, in the message WM_LBUTTONDOWN: I set the box x and y coordinates:</p>
  
  <pre><code>case WM_LBUTTONDOWN:
      boxx = pt.x;
      boxy = pt.y;
      break;
  </code></pre>
  
  <p>Although this does set the box x and y coordinates, I've come to realize that the window never attempts to draw the box again.  So to fix this problem, I tried to add <code>InvalidateRect(hWnd, 0, NULL);</code> to my code, to no avail. </p>
  
  <p>So my question persists, how do I get the program to redraw or repaint the window?</p>
  
  <p>EDIT: I have changed my code to handle both <code>WM_MOUSEMOVE</code> and <code>WM_LBUTTONUP</code> like so:</p>
  
  <pre><code>case WM_LBUTTONDOWN:
      boxdragmode = true;
      break;
  case WM_MOUSEMOVE:
      if(boxdragmode)
      {
          boxx = pt.x;
          boxy = pt.y;
      }
      break;
  case WM_LBUTTONUP:
      boxdragmode = false;
      InvalidateRect(hWnd, 0, TRUE);
      break;
  </code></pre>
  
  <p>It still doesn't update the window, or it might not update the box coordinates.  I get the coordinates through this: </p>
  
  <pre><code>POINT pt;
  pt.x = ((int)(short)LOWORD(lParam));
  pt.y = ((int)(short)HIWORD(lParam));
  ScreenToClient(hWnd, &amp;pt);
  </code></pre>
  
  <p>Your help is much appreciated</p>