Viewing Dialog Template Resources at Runtime

In many resource applications, it is necessary to display a preview of the application's dialog template resource(s). This article presents a class (CPreviewStatic) that can be used to preview dialog template resources at runtime with a single line of code!

Using the CPreviewStatic class

In order to use this class, simple place a static text control on the dialog where you want to display the dialog template resource at runtime. Then, subclass the control with a call to SubclassDlgItem.
    m_stcPreview.SubclassDlgItem(IDC_STATIC_PREVIEW, this);

Once you have subclassed the text control, modify the CPreviewStatic::PreSubclassWindow function to load the desired dialog template resource. To do this, simply modify the LoadResource function call by specifying the correct resource id.

That's it! With only two steps you can view any of your application's dialog templates at runtime. For the more curious among you, the actual code to implement this functionality is presented below. If you have any questions or need any help, please feel free to contact me: Mihai Filimon. Enjoy!

Implementation of the CPreviewStatic class

The (CPreviewStatic::Preview) member function takes, as its only argument, a handle to a memory block where a dialog template resource is stored. A dialog template resource can be loaded into memory using the LoadResource Win32 SDK function. You can see how this is done in the demo project (CPreviewStatic::PreSubclassWindow). CPreviewStatic::Preview then returns a pointer to a bitmap that is a preview of the desired dialog template resource.

    // Function name : CPreviewStatic::Preview // Description : Take a handle of memory dialog resource and return the bitmap with preview // Return type : CBitmap* // Argument : HGLOBAL hGlobal CBitmap* CPreviewStatic::Preview(HGLOBAL hGlobal) { CBitmap* pBitmap = NULL, bitmapNonClient; if (LPCDLGTEMPLATE pTemplate = (LPCDLGTEMPLATE)::LockResource(hGlobal)) { CWnd* pParent = AfxGetMainWnd(); if (HWND hwnd = CreateDialogIndirectParam(AfxGetInstanceHandle(), pTemplate, pParent->m_hWnd, NULL, NULL)) { CWnd* pWnd = CWnd::FromHandle(hwnd); CRect rectWindow; pWnd->GetWindowRect(rectWindow); CRect rectClient; pWnd->GetClientRect(rectClient); CPoint p(0,0); pWnd->ClientToScreen(&p); rectClient.OffsetRect(p.x - rectWindow.left, p.y -; CDC* pDC = pWnd->GetDC(); CDC dcMemSourceNonClient, dcMemDestination; if (dcMemSourceNonClient.CreateCompatibleDC(pDC)) if (dcMemDestination.CreateCompatibleDC(pDC)) if (pBitmap = new CBitmap()) if (pBitmap->CreateCompatibleBitmap(pDC, rectWindow.Width(), rectWindow.Height())) if (bitmapNonClient.CreateCompatibleBitmap(pDC, rectWindow.Width(), rectWindow.Height())) { CBitmap* pOldNonClientBitmap = dcMemSourceNonClient.SelectObject(&bitmapNonClient); CBrush brush(RGB(192, 192, 192)); CBrush* pOldBrush = dcMemSourceNonClient.SelectObject(&brush); dcMemSourceNonClient.PatBlt(0,0,rectWindow.Width(), rectWindow.Height(), PATCOPY); dcMemSourceNonClient.SelectObject(pOldBrush); dcMemDestination.SelectObject(pBitmap); pWnd->Print(&dcMemSourceNonClient, PRF_NONCLIENT ); dcMemDestination.BitBlt(0,0,rectWindow.Width(), rectWindow.Height(), &dcMemSourceNonClient, 0,0, SRCCOPY); pWnd->Print(&dcMemDestination, PRF_CHILDREN | PRF_CLIENT); dcMemSourceNonClient.SelectObject(pOldNonClientBitmap); bitmapNonClient.DeleteObject(); } pWnd->DestroyWindow(); } } return pBitmap; }


  • Modified and much smaller version!

    Posted by Legacy on 03/28/2001 12:00am

    Originally posted by: Sushant Kumar

    Same functionality can be achieved with this modified and much smaller version also!

    CBitmap* Preview(HGLOBAL hGlobal)
    CBitmap* pBitmap = NULL;

    if(LPCDLGTEMPLATE pTemplate = (LPCDLGTEMPLATE)::LockResource(hGlobal))
    CWnd* pParent = AfxGetMainWnd();
    if(HWND hwnd = CreateDialogIndirectParam(AfxGetInstanceHandle(), pTemplate, pParent->m_hWnd, NULL, NULL))
    CWnd* pWnd = CWnd::FromHandle(hwnd);
    CRect rectWindow;

    CDC* pDC = pWnd->GetDC();
    CDC dcMemDestination;
    if(pBitmap = new CBitmap())
    if(pBitmap->CreateCompatibleBitmap(pDC, rectWindow.Width(), rectWindow.Height()))
    CBitmap* pOldBitmap = dcMemDestination.SelectObject(pBitmap);


    return pBitmap;


  • Code?

    Posted by Legacy on 03/22/2000 12:00am

    Originally posted by: Tom Archer

    Hi Milhai,

    Do you think you could throw together a demo for this great article?

    Thanks in advance,
    Tom Archer
    Web Master - CodeGuru

  • Where's the goods?

    Posted by Legacy on 07/27/1999 12:00am

    Originally posted by: Jason

    Where is this phantom class?

  • Here's the code

    Posted by Legacy on 10/29/1998 12:00am

    Originally posted by: Mihai

    It's just a simple snippet. I am not responsabile if your explorer isn't in folder winnt 9 you can chage
    this) or if that explorer has a dialog with 9 as identifier. You can change this. I choise the screen to put
    the preview for make independentsnippet.

    void Test()
    if (CBitmap* p = Preview(LoadResource(h, FindResource(h = LoadLibrary(_T("c:\\winnt\\explorer.exe")), MAKEINTRESOURCE(9) ,RT_DIALOG))))
    BITMAP b; p->GetBitmap(&b);
    CDC dcMem, *pDC = CDC::FromHandle(::GetDC(NULL));
    if (dcMem.CreateCompatibleDC(pDC))
    CBitmap* pOld = dcMem.SelectObject(p);
    pDC->StretchBlt(0,0,100,100, &dcMem, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY);
    delete p;
    Sincerely, Mihai

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

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds