How to convert DLU into pixels?

Viewed 7923

Microsoft uses dialog length units (DLU) in their guidelines for UI. How can I convert them into pixels?

As I know, DLU depending on system font size. Can you advise some simple method of such conversion in Delphi for Win32?

4 Answers

Here's C code for converting DLU ↔ pixels:

HWND hDlg = ...;                   // The handle to the dialog

LPDLGTEMPLATE *dlgTemplate = ...;  // The template for the same dialog
SIZE dlgSize;                      // Only needed for converting DLU -> pixels
if (dlgTemplate->style == 0xFFFF0001)
{
    dlgSize.cx = ((DLGTEMPLATEEX *)dlgTemplate)->cx;
    dlgSize.cy = ((DLGTEMPLATEEX *)dlgTemplate)->cy;
}
else
{
    dlgSize.cx = dlgTemplate->cx;
    dlgSize.cy = dlgTemplate->cy;
}

RECT rc = { 0, 0, 4, 8 };
MapDialogRect(hDlg, &rc);

// To convert dlgSize to pixels, use:
SIZE wndSize = { dlgSize.cx * rc.right / 4, dlgSize.cy * rc.bottom / 8 };

// To convert wndSize to DLUs, use:
SIZE dlgSize2 = { size.cx * 4 / rc.right, size.cy * 8 / rc.bottom };
assert(dlgSize1 == dlgSize2);
Related