Forum > RichMemo

[solved] RichMemo and CHARFORMAT2 - highlight text

(1/4) > >>

Nicole:
I received an answer, which I very appreciated:
https://forum.lazarus.freepascal.org/index.php/topic,60019.msg448099.html#msg448099

However it does not work for me, because
CHARFORMAT2
is not found.
In which unit may it be?

I use Richmemo, NOT lzRichEdit.
One link says, it would be struct.inc, but this did not work neither.

This is my new unit:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit unit_RichMemo; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, graphics,  Windows, RichMemo, RichMemoUtils;   type    TmyRichMemo = class(TObject)      procedure RichMemoHighlightRange(R: TRichMemo; ASelStart, ASelLength: integer; AColor: TColor);      procedure RichMemoHighlightReset(R: TRichMemo);      procedure RichMemoHighlightWord(R: TRichMemo; AWord: string; AColor: TColor);      end; Var myRichMemo: TRichMemo; implementation//uses struct.inc; procedure TmyRichMemo.RichMemoHighlightRange(R: TRichMemo; ASelStart, ASelLength: integer; AColor: TColor);var  Format: CHARFORMAT2;  // ist in struct.inc  OldSelStart, OldSelLength: integer;begin  R.Lines.BeginUpdate;  try     OldSelStart := R.SelStart;    OldSelLength := R.SelLength;     R.SelStart := ASelStart;    R.SelLength := ASelLength;     FillChar(Format, SizeOf(Format), 0);    Format.cbSize := SizeOf(Format);    SendMessage(R.Handle, EM_GETCHARFORMAT, WPARAM(True), LPARAM(@Format));    Format.dwMask := CFM_BACKCOLOR;    if AColor = 0 then    begin      Format.dwEffects := CFE_AUTOBACKCOLOR;    end    else    begin      Format.dwEffects := 0;      Format.crBackColor := ColorToRGB(AColor);    end;    SendMessage(R.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@Format));   finally    R.SelStart := OldSelStart;    R.SelLength := OldSelLength;    R.Lines.EndUpdate;  end; end; procedure TmyRichMemo.RichMemoHighlightReset(R: TRichMemo);begin  RichMemoHighlightRange(R, 0, R.GetTextLen, 0);end; procedure TmyRichMemo.RichMemoHighlightWord(R: TRichMemo; AWord: string; AColor: TColor);var  Start, Found: integer;begin  Start := 0;  Found := R.Search(AWord, Start, R.GetTextLen, []);   while (Found <> -1) do  begin    RichMemoHighLightRange(R, Found, Length(AWord), AColor);    Start := Found + 1;    Found := R.Search(AWord, Start, R.GetTextLen, []);  end; end;////procedure TForm1.FormCreate(Sender: TObject);//begin//  LoadRtfFile(RichMemo1, 'c:\temp\test.rtf');//end;////procedure TForm1.Button1Click(Sender: TObject);//begin//  RichMemoHighlightReset(RichMemo1);//end;////procedure TForm1.Button2Click(Sender: TObject);//begin//  RichMemoHighlightWord(RichMemo1, 'Lorem', clYellow);//  RichMemoHighlightWord(RichMemo1, 'Ipsum', clMoneyGreen);//end; end. 

rvk:
Didn't I put the uses in my answer ???
Yes I did  ;)


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Windows, RichEdit, RichMemoUtils;
(One of those has the CHARFORMAT2 in it, I think the RichEdit.)

You could have asked the question in that topic  :D

skalogryz:
O_o
CFE_AUTOBACKCOLOR and CFE_AUTOCOLOR is something i was looking for!

SetRangeParams however should work as well, with tmm_BackColor used.

Nicole:
Thank you for the answers.
Yes, this SHOULD be part of RichEdit, never the less, the compiler does not accept CHARFORMAT2

about
RiCFE_AUTOBACKCOLOR and CFE_AUTOCOLOR
tmm_BackColor

Can you please give me an example how to use?
I post a syntax, which is accepted for Color.
RichMemo1.SetRangeColor(RichMemo1.SelStart,RichMemo1.SelLength, clRed);

How would a line look alike for the mentioned parameters?
The above is only for color and I found Range parameter for underline etc.

rvk:

--- Quote from: Nicole on August 03, 2022, 09:46:50 am ---Yes, this SHOULD be part of RichEdit, never the less, the compiler does not accept CHARFORMAT2

--- End quote ---
I just checked. It is in RichEdit. And my code is still compilable for me.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     CHARFORMAT2 = CHARFORMAT2A;
So I'm not sure what you mean by "the compiler does not accept CHARFORMAT2".
In that case you have done something wrong.

Show the exact error message (you can right-click it and choose copy) and code.

PS. My code worked perfectly (I also showed you a screen shot).
New Project, drop two buttons on the form and a TRichMemo.
Double click the buttons and a piece of empty form to create the Button1Click, Button2Click and FormCreate.
After that copy and paste my code from the other topic over the created functions and also include the uses line.
Create a c:\temp\test.rtf and run the project.
It should work without errors.

Navigation

[0] Message Index

[#] Next page

Go to full version