Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For those that are interested, here's the code that I ended up using. If you attach this to the OnAfterCellPaint event of a TVirtualStringTree it gives the desired results.</p> <pre><code>(* DrawHTML - Draws text on a canvas using tags based on a simple subset of HTML/CSS &lt;B&gt; - Bold e.g. &lt;B&gt;This is bold&lt;/B&gt; &lt;I&gt; - Italic e.g. &lt;I&gt;This is italic&lt;/I&gt; &lt;U&gt; - Underline e.g. &lt;U&gt;This is underlined&lt;/U&gt; &lt;font-color=x&gt; Font colour e.g. &lt;font-color=clRed&gt;Delphi red&lt;/font-color&gt; &lt;font-color=#FFFFFF&gt;Web white&lt;/font-color&gt; &lt;font-color=$000000&gt;Hex black&lt;/font-color&gt; &lt;font-size=x&gt; Font size e.g. &lt;font-size=30&gt;This is some big text&lt;/font-size&gt; &lt;font-family&gt; Font family e.g. &lt;font-family=Arial&gt;This is arial&lt;/font-family&gt; *) procedure TfrmSNMPMIBBrowser.DrawHTML(const ARect: TRect; const ACanvas: TCanvas; const Text: String); function CloseTag(const ATag: String): String; begin Result := concat('/', ATag); end; function GetTagValue(const ATag: String): String; var p: Integer; begin p := pos('=', ATag); if p = 0 then Result := '' else Result := copy(ATag, p + 1, MaxInt); end; function ColorCodeToColor(const Value: String): TColor; var HexValue: String; begin Result := 0; if Value &lt;&gt; '' then begin if (length(Value) &gt;= 2) and (copy(Uppercase(Value), 1, 2) = 'CL') then begin // Delphi colour Result := StringToColor(Value); end else if Value[1] = '#' then begin // Web colour HexValue := copy(Value, 2, 6); Result := RGB(StrToInt('$'+Copy(HexValue, 1, 2)), StrToInt('$'+Copy(HexValue, 3, 2)), StrToInt('$'+Copy(HexValue, 5, 2))); end else // Hex or decimal colour Result := StrToIntDef(Value, 0); end; end; const TagBold = 'B'; TagItalic = 'I'; TagUnderline = 'U'; TagBreak = 'BR'; TagFontSize = 'FONT-SIZE'; TagFontFamily = 'FONT-FAMILY'; TagFontColour = 'FONT-COLOR'; var x, y, idx, CharWidth, MaxCharHeight: Integer; CurrChar: Char; Tag, TagValue: String; PreviousFontColor: TColor; PreviousFontFamily: String; PreviousFontSize: Integer; begin // Start - required if used with TVirtualStringTree ACanvas.Font.Size := Canvas.Font.Size; ACanvas.Font.Name := Canvas.Font.Name; ACanvas.Font.Color := Canvas.Font.Color; ACanvas.Font.Style := Canvas.Font.Style; // End PreviousFontColor := ACanvas.Font.Color; PreviousFontFamily := ACanvas.Font.Name; PreviousFontSize := ACanvas.Font.Size; x := ARect.Left; y := ARect.Top; idx := 1; MaxCharHeight := ACanvas.TextHeight('Ag'); While idx &lt;= length(Text) do begin CurrChar := Text[idx]; // Is this a tag? if CurrChar = '&lt;' then begin Tag := ''; inc(idx); // Find the end of then tag while (Text[idx] &lt;&gt; '&gt;') and (idx &lt;= length(Text)) do begin Tag := concat(Tag, UpperCase(Text[idx])); inc(idx); end; /////////////////////////////////////////////////// // Simple tags /////////////////////////////////////////////////// if Tag = TagBold then ACanvas.Font.Style := ACanvas.Font.Style + [fsBold] else if Tag = TagItalic then ACanvas.Font.Style := ACanvas.Font.Style + [fsItalic] else if Tag = TagUnderline then ACanvas.Font.Style := ACanvas.Font.Style + [fsUnderline] else if Tag = TagBreak then begin x := ARect.Left; inc(y, MaxCharHeight); end else /////////////////////////////////////////////////// // Closing tags /////////////////////////////////////////////////// if Tag = CloseTag(TagBold) then ACanvas.Font.Style := ACanvas.Font.Style - [fsBold] else if Tag = CloseTag(TagItalic) then ACanvas.Font.Style := ACanvas.Font.Style - [fsItalic] else if Tag = CloseTag(TagUnderline) then ACanvas.Font.Style := ACanvas.Font.Style - [fsUnderline] else if Tag = CloseTag(TagFontSize) then ACanvas.Font.Size := PreviousFontSize else if Tag = CloseTag(TagFontFamily) then ACanvas.Font.Name := PreviousFontFamily else if Tag = CloseTag(TagFontColour) then ACanvas.Font.Color := PreviousFontColor else /////////////////////////////////////////////////// // Tags with values /////////////////////////////////////////////////// begin // Get the tag value (everything after '=') TagValue := GetTagValue(Tag); if TagValue &lt;&gt; '' then begin // Remove the value from the tag Tag := copy(Tag, 1, pos('=', Tag) - 1); if Tag = TagFontSize then begin PreviousFontSize := ACanvas.Font.Size; ACanvas.Font.Size := StrToIntDef(TagValue, ACanvas.Font.Size); end else if Tag = TagFontFamily then begin PreviousFontFamily := ACanvas.Font.Name; ACanvas.Font.Name := TagValue; end; if Tag = TagFontColour then begin PreviousFontColor := ACanvas.Font.Color; ACanvas.Font.Color := ColorCodeToColor(TagValue); end; end; end; end else // Draw the character if it's not a ctrl char if CurrChar &gt;= #32 then begin CharWidth := ACanvas.TextWidth(CurrChar); if x + CharWidth &gt; ARect.Right then begin x := ARect.Left; inc(y, MaxCharHeight); end; if y + MaxCharHeight &lt; ARect.Bottom then begin ACanvas.Brush.Style := bsClear; ACanvas.TextOut(x, y, CurrChar); end; x := x + CharWidth; end; inc(idx); end; end; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload