Author Topic: How get correctly Textlength to max width rect  (Read 372 times)

0 Members and 1 Guest are viewing this topic.

Offline Stefan Midegaal

  • Newbie
  • *
  • Posts: 13
How get correctly Textlength to max width rect
« on: February 12, 2017, 08:06:15 AM »
i am parse any char for the correctly width like this.

Code: [Select]
function TSkinListView.CalculateCharWith(DC: HDC; const C: Char): Integer;
var
  Fam: GpFontFamily;
  TempFont: GpFont;
  zdW: integer;
  Graphics: cardinal;
begin
  zdW := 0;

  GdipCreateFontFamilyFromName(FFontName, nil, Fam);

  if Assigned(Fam) then
  begin
    GdipCreateFont(Fam, FPointSize, FFontStyle, 2, TempFont);
    if Assigned(TempFont) then
    begin
      GdipCreateStringFormat(0, 0, strFormat);
      GdipCreateFromHDC(DC, Graphics);
      GdipMeasureString(Graphics, C, length(C), TempFont, @layoutRect, strFormat,
        @boundingBox, nil, nil);

      if Assigned(strFormat) then
        GdipDeleteStringFormat(strFormat);

      zdW := round(boundingBox.Width);
    end;
    GdipDeleteGraphics(Graphics);
    GdipDeleteFont(TempFont);
  end;
  GdipDeleteFontFamily(Fam);

  Result := zdW;
end;

Code: [Select]
          for HeaderItem := 0 to HeaderItemCount - 1 do
          begin
            ZeroMemory(@buf, sizeof(buf));
            ListView_GetItemText(WinHandle, Item, HeaderItem, buf, sizeof(buf));

            Header_GetItemRect(HeaderHandle, HeaderItem, @rcHeader);

            MaxLength := (rcHeader.Right + rc.Left) - rcHeader.Left;
            ItemText := PWideChar(ShortenText(hTempDC, buf, MaxLength, CalculateCharWith));
            DrawItem(WinHandle, hTempDC, Item, HeaderItem, rc, ItemText, nSelected);
          end;
          OffsetRect(rc, 0, ItemHeight);

the Trouble is now the text is smaler then the Max Rect as shown.
I want to cut the text if longer than the maximum rect and added  ELLIPSIS

any help?

EDIT:
I think this is not possible without DrawText and DT_END_ELLIPSIS

greets
« Last Edit: March 04, 2017, 09:33:00 PM by Stefan Midegaal »

Offline Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2382
  • Gender: Male
    • www.zapsolution.com
Re: How get correctly Textlength to max width rect
« Reply #1 on: February 12, 2017, 07:28:57 PM »
Render the text into a bitmap then BitBlt to the final DC using a croped section
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Stefan Midegaal

  • Newbie
  • *
  • Posts: 13
Re: How get correctly Textlength to max width rect
« Reply #2 on: February 12, 2017, 11:09:42 PM »
Render the text into a bitmap then BitBlt to the final DC using a croped section
i do it..
the Problem is not the render part the Trouble is the width of any Char with GdipMeasureString.

It works as it should, with DrawText and DT_END_ELLIPSIS see shot
the Problem then the text is wrong with BlurEffect.

greets
« Last Edit: February 14, 2017, 09:57:01 AM by Stefan Midegaal »