Creating a View on a Dialog | CodeGuru

Creating a View on a Dialog

–> Environment: NT4 SP3 Creating a view on a child frame is a basic action when programming in MFC, but what happens when you want your view to be attached to a popup dialog? This article shows a simple way to make this possible. The idea is to create the view in the usual way […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 9, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

–>

Environment: NT4 SP3

Creating a view on a child frame is a basic action when programming
in MFC, but what happens when you want your view to be attached to a
popup dialog? This article shows a simple way to make this possible.

The idea is to create the view in the usual way (through the document
template, for example), and then have the dialog set as the parent. The previous parent–probably the child frame–should be stored and set back when the dialog is destroyed. Two more steps are needed:

  1. You should hide the previous parent window.
  2. You should override the OnMouseActivate method in your view in order to prevent an assertion in the base implementation
// =============================================
// In the creator window —
// =============================================
// when you want to show the view
void CViewOnDlgView::OnShowdlgBtn()
{
  // Add the desired document template
  CDocTemplate* pDocTemplate = NULL;
  pDocTemplate = new CMultiDocTemplate(
    IDR_INNERVIEW,
    RUNTIME_CLASS(CrInnerDoc),
    RUNTIME_CLASS(CChildFrame),
// custom MDI child frame
    RUNTIME_CLASS(CrInnerView));
  AfxGetApp()->AddDocTemplate(pDocTemplate);
  
  // Create an instance of the desired document template
  CString processName;
  bool bIsFound = false;
  CWinApp* pApp = AfxGetApp();
  POSITION curTemplatePos =
              pApp->GetFirstDocTemplatePosition();
  while(curTemplatePos)
  {
    pDocTemplate =
           pApp->GetNextDocTemplate(curTemplatePos);
    if (pDocTemplate)
    {
      pDocTemplate->GetDocString(processName ,
                                 CDocTemplate::docName);
      if( processName == “InnerView”)
      {
        bIsFound = true;
        break;
      }
      else
        processName = “”;
    }
  }
  if (bIsFound)
    pDoc = pDocTemplate->OpenDocumentFile(NULL);

  // send a message to the view to move itself to
  // the dialog
  POSITION pos = pDoc->GetFirstViewPosition();
  while (pos)
  {
    CView* pView = pDoc->GetNextView(pos);
    pView->SendMessage(WM_SHOW_ON_DLG, 0, 0);
  }
}

// =============================================
// In the view you want to put on the dialog
// =============================================
int CrInnerView::ShowOnDlg(WPARAM wParam, LPARAM lParam)
{
  // hiding the previous parent
  GetParent()->ShowWindow(SW_HIDE);
  // creating the bearer dialog
  CrPopupDlg dlg;
  dlg.SetView(this);
  dlg.DoModal();
  return 0;
}
// this method needs to be overridden to prevent
//     an assertion
int CrInnerView::OnMouseActivate( CWnd* pDesktopWnd,
                                  UINT nHitTest,
                                  UINT message )
{
  return MA_ACTIVATE;
}
// =============================================
// In the bearer dialog
// =============================================
BOOL CrPopupDlg::OnInitDialog()
{
  if (m_pView && ::IsWindow(m_pView->m_hWnd))
  {
    // set this as the view’s parent and save
    // the old parent
    m_pPrevParent = m_pView->SetParent(this);
    // resize the view to the measures of the dlg
    CRect rect;
    GetClientRect(rect);
    m_pView->MoveWindow(rect);
  }
  return TRUE;
}
void CrPopupDlg::SetView(CView* pView)
{
  // saves a pointer to the view we want to attach
  // to this dlg
  m_pView = pView;
}
void CrPopupDlg::OnDestroy()
{
  CDialog::OnDestroy();
  // set the previous parent window back as active
  // parent and destroy both windows
  if (m_pPrevParent && ::IsWindow(m_pPrevParent->m_hWnd))
  {
    m_pView->SetParent(m_pPrevParent);
    m_pView->GetParent()->DestroyWindow();
  }
}

Downloads

Download demo project – 36 Kb

Download source – 24 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.