Finding Display Size of Dialog From Resource

This article explains how to find display size of the dialog from it's resource handle. It will be helpful, if you need to arrange position and size of other windows based upon the dialog size before the dialog gets displayed.

There is no MFC support to find the display size of dialog from it's resource and it becomes even more painful to implement if dialog resides in a DLL and contains some ActiveX controls.

Here is the function to find it :


CRect CalcDialogSize(INT nResourceId, 
                     const char* strDllName=NULL)
{
 CRect rectSize;

 // if the dialog resource resides in a DLL ...
 //	
 HINSTANCE hExeResourceHandle;
 HINSTANCE hModule;

 if(strDllName != NULL)
 {
  hExeResourceHandle = AfxGetResourceHandle();

  hModule= ::LoadLibrary(strDllName);
  ASSERT(hModule);

  AfxSetResourceHandle(hModule);
 }

 HINSTANCE hInst = 
  AfxFindResourceHandle(MAKEINTRESOURCE(nResourceId),
                        RT_DIALOG);

 ASSERT(hInst != NULL);

 HRSRC hRsrc = ::FindResource(hInst, 
                              MAKEINTRESOURCE(nResourceId),
                              RT_DIALOG);
 ASSERT(hRsrc != NULL);

 HGLOBAL hTemplate = ::LoadResource(hInst, hRsrc);
 ASSERT(hTemplate != NULL);

 DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)::LockResource(hTemplate);

 //Load coresponding DLGINIT resource 
 //(if we have any ActiveX components)
 //
 void* lpDlgInit;
 HGLOBAL hDlgInit = NULL;
 CDialog dlg;

 HRSRC hsDlgInit = ::FindResource(hInst, 
                                  MAKEINTRESOURCE(nResourceId),
                                  RT_DLGINIT);
 if (hsDlgInit != NULL)
 {
  // load it
  hDlgInit = ::LoadResource(hInst, hsDlgInit);
  ASSERT(hDlgInit != NULL);

  // lock it
  lpDlgInit = ::LockResource(hDlgInit);
  ASSERT(lpDlgInit != NULL);

  dlg.CreateIndirect(pTemplate, NULL, lpDlgInit);   
 }
 else
 {
  dlg.CreateIndirect(pTemplate, NULL);  
 }
		
 CRect rect;
 dlg.GetClientRect(rectSize);

 dlg.DestroyWindow();

 ::UnlockResource(hTemplate);
 ::FreeResource(hTemplate);
 if (hDlgInit) 
 {
  ::UnlockResource(hDlgInit);   
  ::FreeResource(hDlgInit);
 }	

 if(strDllName != NULL)
 {
  AfxSetResourceHandle(hExeResourceHandle);
  ::FreeLibrary(hModule);
 }

 return rectSize;
}



Comments

  • Little shorter and better method

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

    Originally posted by: Sukhanandan

    void CMyAppApp::CalcDialogSize(UINT nResourceId, SIZE& size)
    {
    HRSRC hRsrc = ::FindResource(m_hInstance, MAKEINTRESOURCE(nResourceId), RT_DIALOG);
    ASSERT(hRsrc != NULL);
    HGLOBAL hTemplate = ::LoadResource(m_hInstance, hRsrc);
    ASSERT(hTemplate != NULL);

    DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)::LockResource(hTemplate);
    CDialogTemplate dlgtemplate(pTemplate);
    dlgtemplate.GetSizeInPixels(&size);
    ::UnlockResource(hTemplate);

    return bResult;
    }

    Reply
  • Nice job but I have a slight problem.

    Posted by Legacy on 10/16/2001 12:00am

    Originally posted by: bphillips

    I'm trying to size a CChildframe, in PreCreateWindow, based on the dialog size. It works fine except for the width and height are slightly off. Any suggestions?
    Thanks

    Reply
  • Very well done

    Posted by Legacy on 08/10/2001 12:00am

    Originally posted by: Hamid Yaseen

    Dear
    You have done very well.

    Thanks

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds