Solved

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

Posted on 1998-03-13
7
644 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 238
Delphi selector screen 2 84
Print Graphic and Text to Epson TM-T88v 12 386
Wincontrol not (correctly) drawn 15 41
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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