title: Resizing window with Win32 API
body: <p>i'm writing a program with the Win32 API just to get a hang of it and have come across this annoying problem. After a little while of resizing the window minimizes and after bringing it up again it's visible but non-clickable and clicking on it just activates what's on the window below it. The only solution being exiting the program.</p>
  
  <p>At first i thought it was because of my custom-made function for sizing the window (i use a custom gui and don't want the default windows sizebox border) but then i reactivated the default sizebox and the problem still persisted. It's hard to tell but the problem seems to consistently occur after roughly the same amount of time/ticks being resized.</p>
  
  <p>I had a similar problem before where the window would minimize and then becoming completely white when brought up again, which for some reason was caused by a for-loop in the resizing function.</p>
  
  <p>I have excluded as many potential problems as possible in the code but still haven't found a solution. Below is the source file where all the code handling how the window is drawn lies (with the default sizebox).</p>
  
  <p>I appreciate any help i can get.</p>
  
  <p>Ps. I apologize for any bad language (non-native speaker), incorrectly used terms or bad syntax (first time venturing this far into the API).</p>
  
  <pre><code>//WinMain.cpp
  
  #include &lt;Windows.h&gt;
  
  #include "Utility.h"
  #include "Mouse.h"
  #include "AppInfo.h"
  #include "Buttons.h"
  
  //Function prototypes
  ATOM MainRegister();
  bool MainInit(HWND &amp;hWnd, int nCmdShow);
  void MatchRectToWnd(RECT &amp;rect);
  
  //Variables define in AppInfo.h
  HRGN rgnMain, rgnCaptionbar;
  bool _APPRUNNING = true;
  const char _APPTITLE[] = "Dark";
  
  //Variables
  bool WIREFRAME = false;
  
  //Pointers to buttons (singelton design)
  btnCloseClass * btnCloseClass::s_Instance = 0;
  btnMaximizeClass * btnMaximizeClass::s_Instance = 0;
  btnMinimizeClass * btnMinimizeClass::s_Instance = 0;
  
  LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch (msg)
      {
      case WM_LBUTTONDBLCLK: //Left mouse button double-clicked
          Mouse.CheckDblClick(hWnd, wParam);
          Mouse.m_Pressed = false;
      break;
      case WM_LBUTTONDOWN: //Left mouse button clicked
          //Update the mouse position variables
          GetCursorPos(&amp;Mouse.prevPt);
          GetCursorPos(&amp;Mouse.m_LastClick);
  
          Mouse.m_Pressed = true; 
  
          Mouse.CheckClickDown(hWnd);
      break;
      case WM_LBUTTONUP: //Left mouse button released
      {
          GetCursorPos(&amp;Mouse.prevPt);
          Mouse.CheckClickUp(hWnd);
          Mouse.m_Pressed = false;
      }
      break;
      case WM_SIZE: //Check if the window has been resized
      {
          //Update the buttons
          btnClose-&gt;Update(hWnd);
          btnMaximize-&gt;Update(hWnd);
          btnMinimize-&gt;Update(hWnd);
  
          //Update the regions
          RECT rect; GetWindowRect(hWnd, &amp;rect);
          rgnMain = CreateRectRgn(0, 0, rect.right - rect.left, rect.bottom- rect.top);
          rgnCaptionbar = CreateRectRgn(0, 0, rect.right - rect.left, CAPTIONBAR_HEIGHT);
  
      }
      break;
      case WM_PAINT: //Draw the window
      {
          HDC hdc;
          PAINTSTRUCT ps;
          HBRUSH hBrush;
  
          hdc = BeginPaint(hWnd, &amp;ps);
  
          //Color the mainregion
          hBrush = CreateSolidBrush(COLOR_MAIN);
          FillRgn(hdc, rgnMain, hBrush);
  
          //Color the captionbarregion
          hBrush = CreateSolidBrush(COLOR_CAPTIONBAR);
          FillRgn(hdc, rgnCaptionbar, hBrush);
  
          //Color the button backgrounds
          hBrush = CreateSolidBrush(COLOR_BUTTON_BACKGROUND);
          FillRgn(hdc, btnClose-&gt;GetRegion(), hBrush);
          FillRgn(hdc, btnMinimize-&gt;GetRegion(), hBrush);
          FillRgn(hdc, btnMaximize-&gt;GetRegion(), hBrush);
  
          //Color the button icons
          hBrush = CreateSolidBrush(COLOR_BUTTON_ICON);
          FillRgn(hdc, btnClose-&gt;GetIcon(), hBrush);
          FillRgn(hdc, btnMinimize-&gt;GetIcon(), hBrush);
          FillRgn(hdc, btnMaximize-&gt;GetIcon(), hBrush);
  
          //Paint the wireframe
          if (WIREFRAME)
          {
              hBrush = CreateSolidBrush(COLOR_WIREFRAME);
              FrameRgn(hdc, rgnMain, hBrush, 1, 1);
              FrameRgn(hdc, rgnCaptionbar, hBrush, 1, 1);
              FrameRgn(hdc, btnClose-&gt;GetRegion(), hBrush, 1, 1);
              FrameRgn(hdc, btnMaximize-&gt;GetRegion(), hBrush, 1, 1);
              FrameRgn(hdc, btnMinimize-&gt;GetRegion(), hBrush, 1, 1);
              FrameRgn(hdc, btnClose-&gt;GetIcon(), hBrush, 1, 1);
              FrameRgn(hdc, btnMaximize-&gt;GetIcon(), hBrush, 1, 1);
              FrameRgn(hdc, btnMinimize-&gt;GetIcon(), hBrush, 1, 1);
          }
  
          //Free up memomry
          DeleteObject(hBrush);
          EndPaint(hWnd, &amp;ps);
      }
      break;
      case WM_KEYDOWN:
      {
          switch (wParam)
          {
          case VK_TAB: //If TAB is pressed
          {
              if (WIREFRAME) //Activate the wireframe
                  WIREFRAME = false;
              else
                  WIREFRAME = true;
              InvalidateRgn(hWnd, NULL, true);
          }
              break;
          case VK_ESCAPE: //If the ESCAPE is pressed
              PostMessage(hWnd, WM_DESTROY, 0, 0);
              break;
          }
      }
      break;
      case WM_DESTROY: //Free up memory and exit the program
          _APPRUNNING = false;
          DeleteObject(rgnMain);
          DeleteObject(rgnCaptionbar);
          PostQuitMessage(0);
          break;
      default:
          return DefWindowProc(hWnd, msg, wParam, lParam);
          break;
      }
      return 0;
  }
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  {
      HWND hWnd;
      MSG msg;
      _APPRUNNING = true;
  
      //Register the main window
      if (!MainRegister())
      {
          MessageBox(hWnd, "Error registering main window!", "Error", MB_ICONERROR);
          return false;
      }
  
      //Initialize the main window
      MainInit(hWnd, nCmdShow);
  
      //App-loop
      while (_APPRUNNING)
      {
          if ((GetKeyState(VK_LBUTTON) &amp; 0x80) == 0) //Make sure the mouse's status gets updated
          {
              Mouse.m_Pressed = false;
          }
          Mouse.Update(hWnd);
  
          //Message-loop
          if (PeekMessage(&amp;msg, hWnd, 0, 0, PM_REMOVE) &gt; 0)
          {
              TranslateMessage(&amp;msg);
              DispatchMessage(&amp;msg);
          }
      }
      return msg.wParam;
  }
  
  bool MainInit(HWND &amp;hWnd, int nCmdShow)
  {
      //Create the window
      hWnd = CreateWindowEx(
          WS_EX_LAYERED,
          _APPTITLE,
          _APPTITLE,
          WS_OVERLAPPEDWINDOW,
          0, 0, //Starting positons (x,y)
          START_WIDTH, START_HEIGHT, //Width and height
          NULL, //Parent-handle
          NULL, //Menu-handle
          GetModuleHandle(NULL),
          NULL);
  
      //Make sure the window was created properly
      if (hWnd == NULL)
      {
          MessageBox(hWnd, "Error initializing main window!", "Error!", MB_ICONERROR);
          return false;
      }
  
      SetLayeredWindowAttributes(hWnd, NULL, NULL, NULL);
  
      //Get and set the window's style
      DWORD wndStyle = GetWindowLong(hWnd, GWL_STYLE);
      wndStyle &amp;= ~(WS_CAPTION);
      SetWindowLong(hWnd, GWL_STYLE, wndStyle);
  
      //Create regions
      rgnMain = CreateRectRgn(0, 0, START_WIDTH, START_HEIGHT);
      rgnCaptionbar = CreateRectRgn(0, 0, START_WIDTH, CAPTIONBAR_HEIGHT);
  
      ShowWindow(hWnd, nCmdShow);
      UpdateWindow(hWnd);
  
      return true;
  }
  
  ATOM MainRegister()
  {
      //Create the window's classEx (extended class)
      WNDCLASSEX wc;
      wc.cbClsExtra = 0;
      wc.cbSize = sizeof(WNDCLASSEX);
      wc.cbWndExtra = 0;
      wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.hIcon = NULL;
      wc.hIconSm = NULL;
      wc.hInstance = GetModuleHandle(NULL);
      wc.lpfnWndProc = MainWndProc;
      wc.lpszClassName = _APPTITLE;
      wc.lpszMenuName = NULL;
      wc.style = CS_DBLCLKS;
  
      //Register the classEx
      return RegisterClassEx(&amp;wc);
  }
  </code></pre>