title: Win32 C++ Create a Window and Procedure Within a Class
body: <p><strong>Pre-Text/ Question</strong></p>
  
  <p>I am trying to make a fairly simple tool to help debug variable values. For it to be completely self contained within the class is what I am aiming for. The end product I can use a function in the class like ShowThisValue(whatever).</p>
  
  <p>The problem I am having is that I can't figure out, if possible, to have the procedure within the class. Here is the short version, with the problem.</p>
  
  <p>-Code updated again 11/29/13-
  -I have put this in its own project now.</p>
  
  <p>[main.cpp]</p>
  
  <pre><code>viewvars TEST; // global
  TEST.CreateTestWindow(hThisInstance); // in WinMain() right before ShowWindow(hwnd, nFunsterStil);
  </code></pre>
  
  <p>[viewvars.h] The entire updated </p>
  
  <pre><code>class viewvars {
  
  private:
      HWND hWindow;            // the window, a pointer to
      LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);        
      static LRESULT CALLBACK ThisWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  
  public:        
      viewvars(); // blank constructor
      int CreateTestWindow(HINSTANCE hInst);
  
  };
  
  // blank constructor
  viewvars::viewvars() {}
  
  
  
  // create the window
  int viewvars::CreateTestWindow(HINSTANCE hInst) {
  
  // variables
  char thisClassName[] = "viewVars";     
  MSG msg;       
  WNDCLASS wincl;  
  
  
  // check for class info and modify the info
  if (!GetClassInfo(hInst, thisClassName, &amp;wincl)) {
      wincl.style = 0;
      wincl.hInstance = hInst;
      wincl.lpszClassName = thisClassName;
      wincl.lpfnWndProc = &amp;ThisWindowProc;
      wincl.cbClsExtra = 0;
      wincl.cbWndExtra = 0;
      wincl.hIcon = NULL;
      wincl.hCursor = NULL;
      wincl.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
      wincl.lpszMenuName = NULL;
  
      if (RegisterClass(&amp;wincl) == 0) {
          MessageBox(NULL,"The window class failed to register.","Error",0); 
          return -1;
      }
  }  
  
  // create window
  hWindow = CreateWindow(thisClassName, "Test", WS_POPUP | WS_CLIPCHILDREN, 10, 10, 200, 200, NULL, NULL, hInst, this);
  if (hWindow == NULL) { 
      MessageBox(NULL,"Problem creating the window.","Error",0); 
      return -1; 
  } 
  
  // show window
  ShowWindow(hWindow, TRUE);
  
  // message loop
  while (GetMessage(&amp;msg, hWindow, 0, 0))
  {
      TranslateMessage(&amp;msg);  
      DispatchMessage(&amp;msg); 
  }    
  
  // then quit window?
  DestroyWindow(hWindow);
  hWindow = NULL;
  
  return msg.wParam;
  }   
  
  
  // window proc
  LRESULT CALLBACK viewvars::ThisWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  
  MessageBox(NULL,"Has it gone this far?","Bench",0); 
  
  // variable
  viewvars *view;
  
  // ????
  if (message == WM_NCCREATE) {
      CREATESTRUCT *cs = (CREATESTRUCT*)lParam; 
      view = (viewvars*) cs-&gt;lpCreateParams;
  
      SetLastError(0);
      if (SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR) view) == 0) {
          if (GetLastError() != 0) {
              MessageBox(NULL,"There has been an error near here.","Error",0); 
              return FALSE;
          }
      }
  }
  else {
      view = (viewvars*) GetWindowLongPtr(hwnd, GWL_USERDATA);
  }
  
  
  if (view) return view-&gt;WindowProc(message, wParam, lParam);
  
  MessageBox(NULL,"If shown, the above statement did not return, and the statement below did.","Error",0); 
  
  return DefWindowProc(hwnd, message, wParam, lParam);
  }
  
  
  LRESULT viewvars::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) {
  // you can access non-static members in here...
  MessageBox(NULL,"Made it to window proc.","Error",0);
  
  switch (message)
  {
      case WM_PAINT: 
          return 0;
      break;
      case WM_DESTROY:
          PostQuitMessage(0);       
          return 0;
      break;
      default:
          MessageBox(NULL,"DefWindowProc Returned.","Error",0);
          return DefWindowProc(hWindow, message, wParam, lParam);
      break;
  }
  }
  </code></pre>
  
  <p>The message boxes appear in this order:</p>
  
  <ul>
  <li>Has it made it this far?</li>
  <li>Made it to window proc</li>
  <li>DefWindowProc returned</li>
  <li>Has it made it this far? // repeated?</li>
  <li>Made it to window proc</li>
  <li>DefWindowProc returned</li>
  <li>Problem Creating the Window</li>
  </ul>
  
  <p>Thanks for the help so far. Do you know where the problem might be?<img src="https://i.stack.imgur.com/LDw3d.jpg" alt="enter image description here"></p>