获取资源文件中定义的对话框大小

时间:2012-02-27 00:05:39

标签: windows api dialog

我需要确定资源文件中定义的对话框客户区的大小。

GetClientRect根据桌面大小剪裁设计大小。我需要按屏幕单位设计的尺寸。我发现this声称从对话框资源中检索大小,但遗憾的是它使用了MFC。

我打开对话框并拥有其HWND和它的ID。如何在不使用MFC的情况下以屏幕单位获取dsign大小?

1 个答案:

答案 0 :(得分:4)

SIZE GetDialogSize(INT nResourceId, BOOL bApproximateCalcMethod = FALSE, LPCTSTR strDllName = NULL)
{

    SIZE dlgSize = {0}; 
    HINSTANCE hModule = 0;

    if(strDllName != NULL)   
        hModule= ::LoadLibrary(strDllName);              
    else
        hModule = ::GetModuleHandle(NULL);   

    HRSRC hRsrc = ::FindResource(hModule, MAKEINTRESOURCE(nResourceId), RT_DIALOG);  

    HGLOBAL hTemplate = ::LoadResource(hModule, hRsrc);  

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

    if (bApproximateCalcMethod) // the approximate method of calculating
    {
        LONG dlgBaseUnits = GetDialogBaseUnits();
        int baseunitX = LOWORD(dlgBaseUnits), baseunitY = HIWORD(dlgBaseUnits);
        dlgSize.cx = MulDiv(pTemplate->cx, baseunitX, 4);
        dlgSize.cy = MulDiv(pTemplate->cy, baseunitY, 8);
    }
    else // the accurate method of calculation
    {
        HWND hDlg = ::CreateDialogIndirect(0, pTemplate, NULL, DialogProc);         
        RECT rc = {0};
        ::GetWindowRect(hDlg, &rc);
        ::DestroyWindow(hDlg);

        dlgSize.cx = rc.right - rc.left;
        dlgSize.cy = rc.bottom - rc.top;
    }

    UnlockResource(hTemplate);
    ::FreeResource(hTemplate);

    if(strDllName != NULL)
        ::FreeLibrary(hModule);

    return dlgSize;
}