Announcement

Collapse
No announcement yet.

Texthoehe/Breite in Pixeln ?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Texthoehe/Breite in Pixeln ?

    Hallo,

    wie kann ich die Textbreite oder -hoehe eines texts in Pixeln ermitteln, ohne Canvas.TextWidth / Canvas.TextHeight zu verwenden ?

    Frank

  • #2
    <pre>

    function TextDimensions(const Text: String; Font: TFont = nil): TSize;
    var
    DC: hDC;
    F: hFont;
    begin
    F := 0;
    DC := GetDC(0);
    try
    if Font <> nil then F := SelectObject(DC, Font.Handle);
    R := Bounds(0, 0, Screen.Width, Screen.Height);
    if DrawText(DC, PChar(Text), Length(Text), R, dt_CalcRect or dt_ExpandTabs or dt_WordBreak) <= 0 then SetRectEmpty(R);
    Result.cx := R.Right;
    Result.cy := R.Bottom;
    finally
    if F <> 0 then SelectObject(DC, F);
    ReleaseDC(0, DC);
    end;
    end;<br>

    // oder <br>

    function TextDimensions(const Text: String; Font: TFont = nil): TSize;
    var
    DC: hDC;
    F: hFont;
    R: TRect;
    begin
    F := 0;
    DC := GetDC(0);
    try
    if Font <> nil then F := SelectObject(DC, Font.Handle);
    if not GetTextExtentPoint32(DC, PChar(Text), Length(Text), @Result) then
    begin
    Result.cx := 0;
    Result.cy := 0;
    end;
    finally
    if F <> 0 then SelectObject(DC, F);
    ReleaseDC(0, DC);
    end;
    end;

    </pre&gt

    Comment

    Working...
    X