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 - rectWindow.top); 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; }


Comments

  • 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;
    pWnd->GetWindowRect(rectWindow);

    CDC* pDC = pWnd->GetDC();
    CDC dcMemDestination;
    if(dcMemDestination.CreateCompatibleDC(pDC))
    if(pBitmap = new CBitmap())
    if(pBitmap->CreateCompatibleBitmap(pDC, rectWindow.Width(), rectWindow.Height()))
    {
    CBitmap* pOldBitmap = dcMemDestination.SelectObject(pBitmap);
    pWnd->Print(&dcMemDestination, PRF_ERASEBKGND | PRF_CHILDREN | PRF_CHILDREN | PRF_NONCLIENT);
    dcMemDestination.SelectObject(pOldBitmap);
    }
    }

    pWnd->DestroyWindow();
    }

    return pBitmap;
    }

    Regards
    Sushant

    Reply
  • 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

    Reply
  • Where's the goods?

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

    Originally posted by: Jason

    Where is this phantom class?

    Reply
  • 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()
    {
    HMODULE h = NULL;
    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);
    dcMem.SelectObject(pOld);
    }
    delete p;
    }
    }
    Sincerely, Mihai

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

Top White Papers and Webcasts

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds