突出显示DBGrid中的特定文本 [英] Highlight specific text in DBGrid

查看:51
本文介绍了突出显示DBGrid中的特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行查询并在dbgrid中显示返回的数据.

I'm performing a query and displaying returned data in dbgrid.

我想突出显示符合搜索条件的项目.像这样:

I would like to highlight the items matching the search criteria. Something like:

搜索依据:"测试"

在DBGrid中,返回的数据将为.

In DBGrid, returned data would be .

ID     Return
1      This is a **test**
2      **Test**ing

毫无疑问,这里的目标是查询数据.但是如何突出显示DBGrid中的特定文本?

The goal here is no doubt about querying data. But how to highlight specific text in DBGrid?

重要提示:仅突出显示文本的特定部分.

Important: only the specific part of the text should be highlighted.

注意:所提供的信息是为了使之清晰,并不完全符合现实.

NOTE: The information presented is to make it clear, not corresponding exactly to reality.

推荐答案

此过程在DbGrid中突出显示"FilterText"

This procedure highlight `FilterText' in DbGrid

procedure HighlightCellText(AGrid :TDbGrid; const ARect : TRect; AColumn : TColumn;  FilterText : string; AState:TGridDrawState ;
  BkColor : TColor = clYellow; SelectedBkColor : TColor = clGray);
var
  HlRect : TRect;
  Position : Integer;
  HlText, FilterColName,DisplayText: string;
  i, offset : Integer;
begin
   DisplayText := Acolumn.Field.AsString;
   Position := Pos(AnsiLowerCase(FilterText), AnsiLowerCase(DisplayText){  AnsiLowerCase(AColumn.DisplayText)});
   if Position > 0 then
   begin
     // set highlight area
     case AColumn.Alignment of
       taLeftJustify:  HlRect.Left := ARect.Left + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1)) + 1;
       taRightJustify: begin
         Offset := AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 1;
         HlRect.Left :=  (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)-offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1));
       end;
       taCenter: begin
         Offset := ((ARect.Right - ARect.Left) div 2) - (AGrid.Canvas.TextWidth(DisplayText) div 2) - (AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 2);

         HlRect.Left := (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)- offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1));
       end;
     end;

     HlRect.Top := ARect.Top + 1;
     HlRect.Right := HlRect.Left +AGrid.Canvas.TextWidth(Copy(DisplayText, Position, Length(FilterText))) + 1 ;
     HlRect.Bottom := ARect.Bottom - 1;

     //check for  limit of the cell
     if HlRect.Right > ARect.Right then
       HlRect.Right := ARect.Right;

     // setup the color and draw the rectangle in a width of the matching text
     if gdSelected in AState then
       AGrid.Canvas.Brush.Color := SelectedBkColor
     else
       AGrid.Canvas.Brush.Color := BkColor;

     AGrid.Canvas.FillRect(HlRect);

     HlText := Copy(DisplayText,Position, Length(FilterText));
     AGrid.Canvas.TextRect(HlRect,HlRect.Left + 1,HlRect.Top + 1, HlText);
   end;
end;

在DbGrid.OnDrawColumnCell事件中使用它:

Use it in DbGrid.OnDrawColumnCell event :

例如,突出显示文本为"ro".

For example highlight text is "ro".

procedure TForm6.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
 begin
   HighlightCellText(TDBGrid(Sender),Rect, Column,'ro',State);
end;

结果:

小样演示

这篇关于突出显示DBGrid中的特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆