title: FindWindow by class name not working?
body: <p>To preface we have a strange requirement that all dialogs must be modeless for an MFC application. There is a particular dialog using region drawing and some custom controls to select dates and times for viewing past and future data per view. I need to be able to close this window when it loses focus, the main app gets a system command, etc.</p>
  
  <p>I figured the easiest way to do this would be to register the class like so:</p>
  
  <pre><code>    // for CWnd::FindWindow
      WNDCLASS wndcls;
      SecureZeroMemory(&amp;wndcls, sizeof(WNDCLASS));
      wndcls.lpszClassName    = L"CTransactionDialog";
      wndcls.style            = CS_HREDRAW | CS_VREDRAW;
      wndcls.lpfnWndProc      = AfxWndProc;
      wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
      wndcls.hInstance        = AfxGetInstanceHandle();
      wndcls.hIcon            = NULL;
  #ifndef _WIN32_WCE_NO_CURSOR
      wndcls.hCursor          = AfxGetApp()-&gt;LoadStandardCursor(IDC_ARROW);
  #else
      wndcls.hCursor          = 0;
  #endif
      wndcls.hbrBackground    = (HBRUSH) (COLOR_BACKGROUND + 1);
      wndcls.lpszMenuName     = NULL;
  
      BOOL retVal = AfxRegisterClass(&amp;wndcls);
      if (!retVal)
          AfxMessageBox(L"AfxRegisterClass(CTransactionDialog) Failed");
  </code></pre>
  
  <p>Then later in response to various event handlers and messages where I would want these modeless window or windows to be closed to do something simple like this:</p>
  
  <pre><code>CWnd* pFound = NULL;
  while ((pFound = CWnd::FindWindow(L"CTransactionDialog", NULL)) != NULL)
      pFound-&gt;DestroyWindow();
  </code></pre>
  
  <p>However despite the registration of the class succeeding and looking at <code>GetRuntimeClass</code> of the dialog in question in debug and seeing that is matches up as expected the <code>FindWindow</code> never seems to find or close these modeless dialogs as expected.</p>
  
  <p>What am I doing wrong or is there a better way to go about this?</p>
  
  <p><strong>Update:</strong> This is how the dialog is created via a static method on the dialog class. The dialog resource for the id specified in create has the Popup property set which should resolve to <code>WS_POPUP</code> style under the MFC covers. The dialog shouldn't and doesn't have a parent as far as I knew.</p>
  
  <pre><code>CTransactionDialog* CTransactionDialog::ShowTransactionDialog(const CRect&amp; crCtrlToFloatAbove, UINT dialogID, Itime defaultTime, Itime initialTime)
  {
      CTransactionDialog* pCTDialog = new CTransactionDialog(crCtrlToFloatAbove, dialogID, defaultTime, initialTime);
      pCTDialog-&gt;Create(CTransactionDialog::IDD);
      pCTDialog-&gt;ShowWindow(SW_SHOW);
  
      return pCTDialog;
  }
  </code></pre>
  
  <p><strong>Update:</strong> Doh! <code>FindWindowEx</code> isn't finding anything either.</p>
  
  <pre><code>CWnd::FindWindowEx(AfxGetMainWnd()-&gt;GetSafeHwnd(), NULL, L"CTransactionDialog", NULL);
  </code></pre>
  
  <p>However I have a new plan. I'm just going to make my own window message and handle it on the main frame. I think I can get away with passing a pointer to the dialog as the lParam of the message and then casting it to a <code>CWnd*</code> then calling <code>DestroyWindow</code>. It will work for most cases in a very round about way. I may run into trouble with minimizing and maximizing of the main frame window for dialogs that nothing is holding a pointer too but we'll see.</p>