Author: Jean Claude Servaye
How to convert dialogs units in pixels if the dialog do not use system font
Answer:
1 2 function DialogUnitsToPixels(DialogUnits: Integer; Canvas: TCanvas; Font: TFont):
3 Integer;
4 var5 A: array[0..52] of char;
6 Z: Integer;
7 U: Word;
8 begin9 // select the current font10 SelectObject(Canvas.Handle, Font.Handle);
11 // Get DialogBaseUnit for system font12 U := HiWord(GetDialogBaseUnits) div 4;
13 // compute mean width of characters in current font14 // as recommended by Microsoft15 A := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
16 Z := (Canvas.TextWidth(A) div 26 + 1) div 2;
17 // Compute result and adjust for screen resolution18 Result := DialogUnits * Z div U * Screen.PixelsPerInch div 96;
19 end;