Solved

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

Posted on 1998-03-13
7
640 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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