Thema: Delphi String mit ... abkürzen

Einzelnen Beitrag anzeigen

Nuclear-Ping
(Gast)

n/a Beiträge
 
#9

Re: String mit ... abkürzen

  Alt 20. Mär 2008, 13:48
TextWidth berücksichtigt aber keine Zeilenumbrüche oder?

Habs aber glaube gleich. Wenn ich einen Breakpoint auf DrawText setze, kürzt er den String zwar, aber das Rect der Zelle ist irgendwie visuell viel kleiner, als da wo er kürzt ...
*weiterbastel*

[edit]
Versteh das nicht ...

- Ohne DT_CALCRECT kürzt er mir den String zwar, aber viel zu spät.

- Mit DT_CALCRECT kürzt er mir den String nicht.
Was laut MSDN auch soweit Sinn macht:
Zitat:
If there are multiple lines of text, DrawText [...] extends the base of the rectangle to bound the last line of text [...]
Aber warum funktioniert das dann aber in dem Beispiel? Wahrsch. weil er nur eine Zeile hat?

- Die Maße des TRect passen. soweit ich das beurteilen kann. FListView.Canvas.DrawFocusRect(Rect) zeichnet genau um die Zelle das FocusRect.

- Also nochmal: Was mache ich falsch?
[/edit]

[nochmal edit]
Danke stahli, jetzt hats klick gemacht.

Hab's jetzt auch so ähnlich gelöst.
Delphi-Quellcode:
//
          // Prüfen, ob Text ins Rect passt
          TextRect := Rect;
          colStr := PChar (Item.SubItems[SubItem - 1]);
          TruncateText := FALSE;
          repeat
            h := DrawText (FListView.Canvas.Handle, PChar (colStr), Length (colStr),
                           TextRect, DT_CALCRECT or
                                 DT_WORDBREAK or
                                 DT_NOPREFIX);
            if h > Rect.Bottom - Rect.Top then
              begin
                Delete (colStr, Length (colStr), 1);
                TruncateText := TRUE;
              end
            else break;
          until FALSE;

          // Wenn Text abgeschnitten wurde, letztes Wort finden und ... anhängen
          if TruncateText then
            begin
              Delete (colStr, Length (colStr) - 3, 4);
              h := 0;
              for a := Length (colStr) downto 1 do
                if (colStr[a] = ' ') then
                  break
                else inc (h);
              Delete (colStr, Length (colStr) - h, h + 1);
              if Length (colStr) > 0 then
                colStr := colStr + ' ...';
            end;

          DrawText (FListView.Canvas.Handle, PChar (colStr), Length (colStr),
                    Rect, DT_WORDBREAK or
                          DT_NOPREFIX);
Ist aber sicher in nem OnDraw-Event nicht das Performantest, aber erstmal 'ne Notlösung.

Würde mich trotzdem noch interessieren, warum das mit dem DrawText und DT_XYZ_ELLIPSIS nicht geklappt hat.
[/nochmal edit]
  Mit Zitat antworten Zitat