Recent

Author Topic: Does 'TMemo' have no OnPrepareCanvas / OnPaint / OnDraw-Event or so?  (Read 857 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 803
I wanted to draw some lines of a TMemo in a different color. I hoped to find an Event like OnPrepareCanvas / OnPaint / OnDraw or something like that, which I could use to manipulate the color, before a certain line is drawed. But I could not find some Event like this... does nothing like this exist?

I'm on Linux with Lazarus 2.2.4. Thanks in advance.

jamie

  • Hero Member
  • *****
  • Posts: 6528
Re: Does 'TMemo' have no OnPrepareCanvas / OnPaint / OnDraw-Event or so?
« Reply #1 on: July 03, 2024, 10:41:37 pm »
Use a different component.

TListBox for example has an owner draw, you can color each line as you wish.
The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 1187
Best regards / Pozdrawiam
paweld

indydev

  • Full Member
  • ***
  • Posts: 114
Re: Does 'TMemo' have no OnPrepareCanvas / OnPaint / OnDraw-Event or so?
« Reply #3 on: July 04, 2024, 12:19:18 am »
Could you use Richmemo? Here's what I am thinking to color specific lines:

Use the TRichMemo.SelStart and TRichMemo.SelLength properties to select the specific line.
Set the TRichMemo.SelAttributes.Color property to change the color of the selected text.
Create a procedure to color a line and apply it in the onChange event.

Here's an example:

Code: Pascal  [Select][+][-]
  1. uses
  2.   StrUtils  //  ...and your other units
  3.  
  4. procedure ColorLine(RichMemo: TRichMemo; LineIndex: Integer; Color: TColor);
  5. var
  6.   StartPos, EndPos, LineCount: Integer;
  7.   Text: string;
  8. begin
  9.   LineCount := RichMemo.Lines.Count;
  10.  
  11.   // Check if the requested line exists
  12.   if (LineIndex < 0) or (LineIndex >= LineCount) then
  13.     Exit;
  14.  
  15.   // Get the full text
  16.   Text := RichMemo.Text;
  17.  
  18.   // Find the start position of the line
  19.   StartPos := 1;
  20.   for var i := 0 to LineIndex - 1 do
  21.     StartPos := PosEx(LineEnding, Text, StartPos) + Length(LineEnding);
  22.  
  23.   // Find the end position of the line
  24.   if LineIndex = LineCount - 1 then
  25.     EndPos := Length(Text) + 1
  26.   else
  27.     EndPos := PosEx(LineEnding, Text, StartPos);
  28.  
  29.   // Select the line
  30.   RichMemo.SelStart := StartPos - 1; // Adjusting for 0-based index
  31.   RichMemo.SelLength := EndPos - StartPos;
  32.  
  33.   // Set the color
  34.   RichMemo.SelAttributes.Color := Color;
  35.  
  36.   // Clear the selection
  37.   RichMemo.SelLength := 0;
  38. end;

You can then use this procedure to color a specific line in the onChange event:

Code: Pascal  [Select][+][-]
  1. ColorLine(RichMemo1, 8, clRed); // Colors the 9th line (index 8) red

This should allow you to color entire lines. If you need more granular control (e.g., coloring parts of lines), you would need to adjust the selection range.
« Last Edit: July 05, 2024, 08:10:44 am by indydev »

Hartmut

  • Hero Member
  • *****
  • Posts: 803
Re: Does 'TMemo' have no OnPrepareCanvas / OnPaint / OnDraw-Event or so?
« Reply #4 on: July 04, 2024, 06:58:50 pm »
Thanks a lot to all for your answers. Seems that 'TMemo' does not have an appropriate Event. I will investigate your suggestions to find the best fitting solution for me.

 

TinyPortal © 2005-2018