Solved

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

Posted on 1998-03-13
7
652 Views
Last Modified: 2013-11-22
Is it possible to change a line color of a RichEdit without selection?
0
Comment
Question by:raunol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

740 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