Solved

Changing color of font automatically

Posted on 1997-07-20
1
233 Views
Last Modified: 2013-12-03
Is it possible when loading a *.rtf document in richedit, which contains any black font to automatically convert to white font at loading.
0
Comment
Question by:hellfire052497
1 Comment
 
LVL 4

Accepted Solution

by:
erajoj earned 40 total points
ID: 1339317
This works but it isn't the most beautiful piece of code
I have come up with. It is not very fast but it does the job.
This is quite a complicated problem and it's really worth
more than 40pts, but this solution is ;^):

procedure TfrmMain.LoadRTFBlackToWhite(FileName: string);
const
  BlackAttr = $80000008; // don't ask me why, ought to be $0
var
  Index1, Index2 : Integer;
  First, Last    : Cardinal;
begin
  RichEdit1.Hide;
  RichEdit1.Lines.LoadFromFile(FileName);
  SendMessage(RichEdit1.Handle, EM_SETSEL, 0, -1); // Set cursor at last position
  SendMessage(RichEdit1.Handle, EM_GETSEL, Integer(@First), Integer(@Last)); // Get gursor position
  Index1  := 0; // First char
  repeat  // Yes it's true, scan through every char. Might be slow on large files!
    SendMessage(RichEdit1.Handle, EM_SETSEL, Index1, Index1+1); // Select char
    with RichEdit1.SelAttributes do
    if (Color=BlackAttr) or (Color=clBlack) then begin
      Index2 := Index1;
      repeat // Find end of color paragraph
        SendMessage(RichEdit1.Handle, EM_SETSEL, Index2, Index2+1);
        Inc(Index2);
      until ((Color<>BlackAttr) and (Color<>clBlack)) or (Index2>=Last);
      SendMessage(RichEdit1.Handle, EM_SETSEL, Index1, Index2-1);
      RichEdit1.SelAttributes.Color := clWhite; // replacement
      Index1 := Index2-1;
    end;
    Inc(Index1);
  until (Index1>=Last);
  SendMessage(RichEdit1.Handle, EM_SETSEL, -1, 0); // Select nothing and put cursor last
  RichEdit1.Show;
end;

/// John
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question