RxDBGrid实现高亮显示指定字符,效果如下:
![]()

方法:
用OnDrawColumnCell自绘:
直接上代码(在“心有阳光”的代码基础完善而来的):
procedure FindAllOccurrences(Text, SearchStr: string; var Positions: TStringList); var PosIndex, SearchLength: Integer; function str(l:integer):string; var i:integer; begin result:=''; for i:=1 to l do result:=result+' '; end; begin SearchLength := Length(SearchStr); PosIndex := Pos(SearchStr, Text); while PosIndex > 0 do begin Positions.Add((PosIndex).ToString); Text:=stringReplace(Text,SearchStr,str(SearchLength),[]); PosIndex := Pos(SearchStr, Text); end; end; procedure TRxDBGridMainForm.RxDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); Var HlRect:TRect; tmpPosition: Integer; HlText, DisplayText: String; offset: Integer; Positions: TStringList; i,pos1: Integer; begin HlText:=edit1.text; if (Sender as TRxDBGrid).Font.Size=6 then (Sender as TRxDBGrid).Font.Size:=6; DisplayText := Column.Field.DisplayText; Positions := TStringList.Create; try FindAllOccurrences(DisplayText,HlText, Positions); if Positions.Count=0 then (Sender as TRxDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State) // 默认绘制 else for i := 0 to Positions.Count - 1 do begin tmpPosition:=Positions[i].ToInteger; //tmpPosition := Pos(AnsiLowerCase(HlText), AnsiLowerCase(DisplayText)); If tmpPosition > 0 Then Begin //Draw the text before the highlighted part Case Column.Alignment Of taLeftJustify: HlRect.Left := Rect.Left + (Sender as TRxDBGrid).Canvas.TextWidth(Copy(DisplayText, 1, tmpPosition - 1)) + 2; taRightJustify: Begin HlRect.Left := (Rect.Right - (Sender as TRxDBGrid).Canvas.TextWidth(DisplayText) ) + (Sender as TRxDBGrid).Canvas.TextWidth(Copy(DisplayText, 1, tmpPosition-1)); End; taCenter: Begin Offset := ((Rect.Right - Rect.Left) DIV 2) - ((Sender as TRxDBGrid).Canvas.TextWidth(DisplayText) DIV 2) + 2; HlRect.Left := (Rect.Right - (Sender as TRxDBGrid).Canvas.TextWidth(DisplayText) - offset) + (Sender as TRxDBGrid).Canvas.TextWidth(Copy(DisplayText, 1, tmpPosition - 1)); End; End; HlRect.Top := Rect.Top + 2; HlRect.Right := HlRect.Left + (Sender as TRxDBGrid).Canvas.TextWidth(Copy(DisplayText, tmpPosition, Length(HlText))) + 1; HlRect.Bottom := Rect.Bottom - 2; // Check for limit of the cell If HlRect.Right > Rect.Right Then HlRect.Right := Rect.Right; // Draw the text before the highlight (Sender as TRxDBGrid).Canvas.Font.Color := clBlack; if i=0 then (Sender as TRxDBGrid).Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, DisplayText); // Draw the highlighted part (Sender as TRxDBGrid).Canvas.Brush.Color := clYellow; (Sender as TRxDBGrid).Canvas.Font.Color := clRed; (Sender as TRxDBGrid).Canvas.FillRect(HlRect); (Sender as TRxDBGrid).Canvas.TextRect(HlRect, HlRect.Left , HlRect.Top , HlText); End else begin (Sender as TRxDBGrid).Canvas.Brush.Color := TColor(clGradientActiveCaption); (Sender as TRxDBGrid).Canvas.Font.Color := TColor(clBlack); (Sender as TRxDBGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State); // 默认绘制 end; end; finally Positions.Free; end; end;

浙公网安备 33010602011771号