Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Delphi VCL highlights word in visual components programmatically

Digital solution partner

Delphi VCL highlights word in visual components programmatically

Highlights word in a RichView component programmatically:

 

uses Winapi.RichEdit;

procedure SetTextColor(oRichEdit : TRichEdit; sText : String; rAttributes : TTextAttributes);
var
          iPos : Integer;
          iLen : Integer;

          Format: CHARFORMAT2;
begin
          FillChar(Format, SizeOf(Format), 0);
          Format.cbSize := SizeOf(Format);
          Format.dwMask := CFM_BACKCOLOR;
          Format.crBackColor := rAttributes.BackColor;

          iPos := 0;
          iLen := Length(oRichEdit.Lines.Text) ;
          iPos := oRichEdit.FindText(sText, iPos, iLen, []);

          while iPos > -1 do begin
                oRichEdit.SelStart  := iPos;
                oRichEdit.SelLength := Length(sText) ;
                oRichEdit.SelAttributes.Color := rAttributes.Font.Color;
                oRichEdit.SelAttributes.Size  := rAttributes.Font.Size;
                oRichEdit.SelAttributes.Style := rAttributes.Font.Style;
                oRichEdit.SelAttributes.Name  := rAttributes.Font.Name;

                oRichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));

                iPos := oRichEdit.FindText(sText,iPos + Length(sText),iLen, []) ;
          end;
end;

How to highlitgh full cell in a DBGrid:

procedure TfrmRuleCreator.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  mstr: String;
  I : Integer;
  currValue: string;
begin
  currValue := Column.Field.AsString.ToUpper().Trim;
  if (Length(currValue) > 3)and(Column.Field.FieldName.ToUpper <> 'NOTE') then
  Begin
    If FDMemTable1.FieldByName('NOTE').AsString.ToUpper.IndexOf(currValue) <> -1
    then
    Begin
      DBGrid1.Canvas.Brush.Color := clYellow;
    End
  End;

  if Column.Field.FieldName.ToUpper = 'NOTE' then
   DBGrid1.Canvas.Brush.Color := $00D7FFD7;


  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;