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?
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?
First we start with what a dialog unit is.
For that i'll quote one of my own un-answered questions:
i'll also quote another of my own un-answered questions:
So what you need is the average size of a character. Microsoft has an official technique for calculating the average character size.
average height:
average width:
Measure the string ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz using
GetTextExtentPoint32
, and divide by 52:So now you need the the size of a horizontal and a vertical dialog units. Remember that a horizontal dialog unit is 1/4 the average character width, and a vertical dlu is 1/8 the average character height:
For base value (and naturally, system font) call
GetDialogBaseUnits
. See alsoremarks
paragraph there for the alternate method of translating dialog units <-> pixels withGetTextMetrics
and/orGetTextExtentPoint32
without dialog HWND.You should use the
MapDialogRect()
function.Pass in a
RECT
in dialog units, and the equivalentRECT
in pixel units is returned. Note that you need a handle to a dialog in order to giveMapDialogRect()
sufficient context. The function needs to know the font in order to perform the conversion.In case you are tempted to use
GetDialogBaseUnits()
, remember what Raymond Chen said, GetDialogBaseUnits is a crock.If you have to calculate pixel dimensions from DLUs, and you don't have a handle to a dialog, then you must use the method outlined here: How To Calculate Dialog Base Units with Non-System-Based Font
However, you made it clear in the comments that, for your problem, you do not actually need to convert from DLUs to pixels. You can use Delphi's built in form scaling to ensure that your forms are sized appropriately for the prevailing font scaling.