Finding Display Size of Dialog From Resource
Posted
by Shridhar Guravannavar
on December 6th, 2000
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
There are no comments yet. Be the first to comment!