title: Implementing callback function for dialog-based application
body: <p>I was reading a very old tutorial on how to create a dialog-based GUI application in Visual C++ (<a href="http://www.arquivodecodigos.net/principal/diretorios/c_plus_plus/artigos_tutoriais/criando_uma_aplicacao_cplusplus_win32_com_interface_grafica_usando_o_visual_cplusplus.php" rel="nofollow noreferrer">source</a> - in portuguese). Based on my weak knowledge on WinAPI programming I decided to modify the proposed code mainly to achieve two things:</p>
  
  <p><strong>1)</strong> Consistency with Unicode programming standard (using <code>wWinMain</code> and <code>MessageBoxW</code> instead of <code>WinMain</code> and <code>MessageBox/MessageBoxA</code> for instance). Such "standard" seems to be enforced all around. Example: <a href="https://stackoverflow.com/questions/4681443/apientry-twinmain-and-winapi-winmain-difference">this question (comments)</a>  </p>
  
  <p><strong>2)</strong> Consistency with the model presented by Visual Studio when creating a non-empty <em>Win32 Project</em>. I did that and noticed that:</p>
  
  <ul>
  <li>The callback function for the "About" dialog box is of type <code>INT_PTR</code> instead of <code>BOOL</code>;</li>
  <li>The <code>WndProc</code> function returns 0, instead of TRUE (which is 1) as is in the tutorial;</li>
  <li>The <code>switch</code> statement for the <code>msg</code> variable defaults to return DefWindowProc() instead of FALSE;</li>
  <li>WM_CLOSE handling is not specified (I guess dialogs have default handling for some events).</li>
  </ul>
  
  <p>_<br>
  As a result, there's a weird behavior in which the MessageBox entitled Confirm is out-of-focus - i.e I can't click the OK and Cancel buttons.</p>
  
  <p><strong>Question:</strong> Am I correct in my assumption that the template code generated by Visual Studio is more correct than the code in the tutorial, which just doesn't seem trustable to me? If yes, did I forget anything? And what's wrong with my code, why can't I click the Messagebox buttons?</p>
  
  <p>My final code follows:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  #include &lt;Windows.h&gt;
  #include "resource.h"
  
  INT_PTR CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch (msg)
      {
      case WM_INITDIALOG:
          return (INT_PTR)TRUE;
      case WM_DESTROY:
          PostQuitMessage(0);
          break;
      case WM_COMMAND:
          switch (LOWORD(wParam))
          {
          case IDOK:
              if (MessageBoxW(hWnd, L"Close?", L"Confirm", MB_OKCANCEL) == IDOK)
                  DestroyWindow(hWnd);
              break;
          // more code to place here
          }
          break;
      default:
          return DefWindowProc(hWnd, msg, wParam, lParam);
      }
      return 0;
  }
  
  int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
  {
      int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DIALOG1), NULL, WndProc);
  
      return 0;
  }
  </code></pre>
  
  <p>For completeness, below is the code based on the tutorial, which actually works fine:</p>
  
  <pre><code>BOOL CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      switch (msg)
      {
      case WM_INITDIALOG:
          return TRUE;
      case WM_CLOSE:
          DestroyWindow(hWnd);
          break;
      case WM_DESTROY:
          PostQuitMessage(0);
          break;
      case WM_COMMAND:
          switch (LOWORD(wParam))
          {
          case IDOK:
              if (MessageBoxW(hWnd, L"Close?", L"Confirm", MB_OKCANCEL) == IDOK)
                  DestroyWindow(hWnd);
              break;
          }
          break;
      default:
          return FALSE;
      }
  
      return TRUE;
  }
  </code></pre>