Creating a View on a Dialog


How to Help Your Business Become an AI Early Adopter


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(
// custom MDI child frame
  // Create an instance of the desired document template
  CString processName; 
  bool bIsFound = false;

  CWinApp* pApp = AfxGetApp();
  POSITION curTemplatePos = 
    pDocTemplate = 
    if (pDocTemplate)
      pDocTemplate->GetDocString(processName , 
      if( processName == "InnerView") 
        bIsFound = true;
        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
  // creating the bearer dialog
  CrPopupDlg dlg;

  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;

  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() 
  // set the previous parent window back as active 
  // parent and destroy both windows
  if (m_pPrevParent && ::IsWindow(m_pPrevParent->m_hWnd))


Download demo project - 36 Kb
Download source - 24 Kb


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date