Mirror

DialogUnits To Pixels (Views: 100)


Problem/Question/Abstract:

How to convert dialogs units in pixels if the dialog do not use system font

Answer:

function DialogUnitsToPixels(DialogUnits: Integer; Canvas: TCanvas; Font: TFont):
  Integer;
var
  A: array[0..52] of char;
  Z: Integer;
  U: Word;
begin
  // select the current font
  SelectObject(Canvas.Handle, Font.Handle);
  // Get DialogBaseUnit for system font
  U := HiWord(GetDialogBaseUnits) div 4;
  // compute mean width of characters in current font
  // as recommended by Microsoft
  A := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  Z := (Canvas.TextWidth(A) div 26 + 1) div 2;
  // Compute result and adjust for screen resolution
  Result := DialogUnits * Z div U * Screen.PixelsPerInch div 96;
end;

<< Back to main page