Solved

Is it possible to change a line color of a RichEdit without selection?

Posted on 1998-03-13
7
635 Views
Last Modified: 2013-11-22
Is it possible to change a line color of a RichEdit without selection?
0
Comment
Question by:raunol
  • 4
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 0 total points
ID: 1360471
This should do it.

Selection is needed to color it, after the coloring we don't need selection any more. Hope this is enough.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ColorGrd, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    ColorGrid1: TColorGrid;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

  var
    iStart, iEnd: longint;
    iSelStart, iSelEnd: longint;
    iOldCount: longint;

  begin
  with RichEdit1 do
    begin
    Lines.BeginUpdate;
    try
      // get current position
      SendMessage( Handle, EM_GETSEL, LongInt( @iStart ), LongInt( @iEnd ) );
      // find out how many lines we have before we add this text
      iOldCount := Lines.Count;
      // add the text
      Lines.Add( Edit1.Text );
      // select newly added text
      // The start index will always be the first line after the old
      // count (line count is not zero-based, so we don't have to add 1)
      iSelStart := SendMessage( Handle, EM_LINEINDEX, iOldCount, 0 );
      // The length of our selection will always be the position we start
      // from plus the length of the text that was added.
      iSelEnd := iSelStart + Length( Edit1.Text );
      SendMessage( Handle, EM_SETSEL, iSelStart, iSelEnd );
      // change the color of the text
      with SelAttributes do
        Color := ColorGrid1.ForegroundColor;
      // return to saved position
      SendMessage( Handle, EM_SETSEL, iStart, iEnd );
    finally
      Lines.EndUpdate;
      end;
    end;
  end;

end.

Regards, Zif.
0
 

Author Comment

by:raunol
ID: 1360472
Hi,

Good idea! Thank You very much, Zif.

It works just like I want, a selection not
flashes during colouring. I feel that Perform
function is rather nicer code to use than Sendmessage,
like this:

//colouring of the current line
Procedure TForm1.Button1Click(Sender: TObject);
var
    iStart, iEnd: longint;
    iSelStart, iSelEnd: longint;
    CurrentLine:Longint;
Begin
Whit RichEdit1 do
Begin
               Lines.BeginUpdate;  
      Perform(EM_GETSEL, LongInt( @iStart ), LongInt( @iEnd ));
               //Get the current line
      CurrentLine:=Perform(EM_LINEFROMCHAR,SelStart,0);
      //Starpos in the begin of the line
            iSelStart:= Perform(EM_LINEINDEX,CurrentLine, 0 );
                 iSelEnd := iSelStart+Length(Lines[CurrentLine]);
            Perform(EM_SETSEL, iSelStart, iSelEnd );
            // change the color of the text
            SelAttributes.Color := clRed;
            // return to saved position
            Perform(EM_SETSEL, iStart, iEnd );
            Lines.EndUpdate;
      Setfocus;
End;


- Rauno


0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360473
Yep Rauno, it's nicer with perform. regards, ZiF.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:raunol
ID: 1360474
Hi, Zif

Thanks for Your comment. Dont be sorry, but
in fact, I was glad too early. The selection
still flashes when to colour a line which is
longer. So Your answer wasn't perfect for my problem:
How to colour line without selection.

Anyway You gave a good advice using of api
functions.


- Rauno



0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360475
Sorry, it doesn't works. I'll look for a solution.
0
 

Author Comment

by:raunol
ID: 1360476
Hi, Zif

Now, I found a one solution my self.

First we must add "RichEdit" -unit to USES section

Then it's possible to use Sendmessage or
perform function:
RichEdit1.Perform(EM_HIDESELECTION,1,0)
And voila! No selection flashes during colouring.

- Rauno

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360477
Hi Rauno,

Great solution! Thanks for letting me know of this!

Regards Zif.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now