Solved

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

Posted on 1998-03-13
7
633 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

17 Experts available now in Live!

Get 1:1 Help Now