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 (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


  • Wholesale Oakley Radar Range fast delivery

    Posted by xtbtxblpo on 07/03/2013 07:06am

    Fake Ray Ban Sunglasses ,A brandname Oakley progress, it's by accepting the truth of some duration and job definitely pick up the development of new style. This is the reason each sunshade found in manufacturing the newest glasses trick. Vibrant and sun-damage people's eyes. Has to be possible for the glasses up your eyes on the sun's harmful sun rays and mud particles may hover more than the atmosphere of any protective covering. Oakley sunglasses are always leading in neuro-scientific science and technology, since the by using high-tech, faster and better preventive effect. OAkLey FLAk JAcket ,Oakley sunglasses designed on a yearly basis to modify, like almost every other styles. However, the several alternatives, not a practical fashion occasions. For those who have enable you to go to the beach during the summer time carnival, marketing and advertising to consider startingsuch a sunglasses, obviously, merely a exclusive edition, isn't a small obstacle. In recent times, oversized sunglasses became popular, many men and women inside choice of sunglasses, will focus on the big sunglasses, have confidence in love, cool to learn the brand Oakley. Oakley sunglasses additional protection vision, as the lens draped over your WATS remain more moderate versus the standard lens and UV may cause further reluctant to implement a simple wide-brimmed sun defense. Replica Oakley Sunglasses ,You can pick a black frame, gold, guns, brown or ivory. Flexible flexible frame to what you can keep your ears behind the lens and from the framework resized. Oakley sunglasses sunglasses is quite good burden. In particular, the actual use, with the visual appearance, but in addition in good agreement using the successful. Fake ray bans ,Product design or number of materials, has become a group of advanced scientific experiments and testing to be sure the comfort and high-quality, high level of integration and functionality and fashion. A scale of Oakley sunglasses has established numerous and also have to pay to relieve significantly the U.S. dollar together with time, chance to lure and well-being to allow them to follow. Sac à épaule Longchamp ,A perpetual lens coating to counteract rain and sweat building around the lens, the exclusion of skin oils and other contaminants, and so the lens is smudge as well as simple to help keep clean. Oakley sunglasses they HDO (high-definition optical) is known, and so they continue to use this breakthrough technology into their design. If you want wholesale sunglasses online purchase is a good choice. This is a good way to compare different sunglasses provide them with the expense of their articles, their brand of wholesalers and retailers.

  • Clicking on inner view in release mode causes the program to crash

    Posted by rsnprabha on 07/20/2005 12:45am

    Clicking on view in release mode causes the program to crash. Looks like the program crahses in OnMouseActivate. any solutions?

  • Easy way.. and works.

    Posted by Legacy on 01/20/2004 12:00am

    Originally posted by: Rajesh

    CRect rect;
    //m_view is static control on dialog.


    CString strMyClass = AfxRegisterWndClass( CS_VREDRAW | CS_HREDRAW, ::LoadCursor(NULL, IDC_ARROW), (HBRUSH) ::GetStockObject(BLACK_BRUSH), ::LoadIcon(NULL, IDI_APPLICATION));

    CWnd* pWnd = new CWnd;
    pWnd->Create( strMyClass, "", WS_CHILD | WS_VISIBLE, rect, this, 1234);

  • Dialog

    Posted by Legacy on 09/26/2003 12:00am

    Originally posted by: Saleem Anwar

    i have controls on the dailog based application and i want to print the dialog as a whole. plz someone help me.

  • help me...

    Posted by Legacy on 10/24/2002 12:00am

    Originally posted by: mihee

    From Dialog , I made Split Window and composed of view separately.

    I have a trouble when i tried to call the above dialog from mainframe..

    In the debug mode, i solved it by overloading the OnMouseActivate event.

    But it was crashed in the Release mode.

    So, If you have a solution about what I said, please send me your answer.

    Thank your for your consideration.

  • An easier way to create a view in Dialog(just 11 lines)

    Posted by Legacy on 08/08/2002 12:00am

    Originally posted by: Z Mohamed Mustafa

           CCreateContext pContext;
    CWnd* pFrameWnd = this;
    pContext.m_pCurrentDoc = new CMyDocument;
    pContext.m_pNewViewClass = RUNTIME_CLASS(CMyVw);
    CMyVw *pView =
    (CMyVw *)((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
    CRect rectWindow;
    rectWindow.right += 15; -= 10;

    * Steps:
    * 1. Create a view class (better to use class wizard)
    * 2. Create a document class (better to use class wizard)
    * 3. Add the above code snippet to your InitDialog
    * CMyVw - any CView derived class
    * CMyDocument - any CDocument derived class

    Note: there is a sample available in the following url:

  • a similar idea

    Posted by Legacy on 06/17/2002 12:00am

    Originally posted by: Thomas Schuster

    Just look up this ...

  • CView in a Dialog-Based works fine. How do I print it now?

    Posted by Legacy on 04/03/2002 12:00am

    Originally posted by: Hugo

    I've followed Jean-Claude`s example using a CHtmlView and it works fine, but I need to print the view now. How do I print the web pages displayed in my view?
    Thanks in advance

  • CRichEditCtrl in a Dialog based app

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: John

    Has any had tried inserting RichEditCtrl in Dialog based
    app? I would like to display .rtf content in a small
    RichEditCtrl window in the app dialog.


    Posted by Legacy on 01/04/2002 12:00am

    Originally posted by: sameer

    is it possible to add the controls of a parent dialog to another dialog on switching at run time so that the controls behave as part of both the dialogs.
    pls help


  • Loading, Please Wait ...

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