Page 1 of 1

Colour for selected character (or cursor placed)

Posted: Wed Apr 10, 2013 12:48 pm
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

Posted: Thu Apr 11, 2013 4:32 pm
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;