Colour for selected character (or cursor placed)

General TRichView support forum. Please post your questions here
Post Reply
jota
Posts: 48
Joined: Fri Sep 16, 2011 10:56 am

Colour for selected character (or cursor placed)

Post by jota »

Hi

For a TDBRichViewEdit:

Is following code valid to get the color of the current character? Is there an easier way?

Code: Select all

procedure TSDIAppForm.EditorNotasClick(Sender: TObject);

var
  tfi:tfontinfos;
  i:tcolor;

begin
  tfi := estiloeditornotas.TextStyles;
  i := tfi.Items[editornotas.CurItemNo].Color;

end;
Best regards
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

No, this code is wrong. CurItemNo is an index of an item in a document, not an index of a text style.

The resulting code depends on what "current character" mean.

The following code returns the color of the current text style:

Code: Select all

procedure TSDIAppForm.EditorNotasClick(Sender: TObject); 

var 
  tfi:tfontinfos; 
  i:tcolor; 

begin 
  tfi := estiloeditornotas.TextStyles; 
  i := tfi.Items[editornotas.[color=red]CurTextStyleNo[/color]].Color; 

end; 
But a current text style does not necessary equal to the style of text at the caret position. A current text style is a text style that will be applied to new inserted characters.

If you need to know a color of text of the current text item, you can use this code:

Code: Select all

procedure TSDIAppForm.EditorNotasClick(Sender: TObject); 

var 
  i:tcolor; 
  StyleNo, TextStyleNo: Integer; 
begin 
  StyleNo := estiloeditornotas.CurItemStyle;
  if StyleNo>=0 then begin
    // this is a text item
    TextStyleNo := StyleNo
  else if estiloeditornotas.CurrentItem.AssociatedTextStyleNo>=0 then
    // this is a tab or a label item
    TextStyleNo := estiloeditornotas.CurrentItem.AssociatedTextStyleNo
  else
    // non-text item like a picture or a table
    TextStyleNo := -1;
  if TextStyleNo>=0 then
    i := estiloeditornotas.TextStyles[TextStyleNo].Color
  else
    i := clNone;
end; 
Post Reply