Viewing Dialog Template Resources at Runtime | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 26, 1998
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

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;
}


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.