4

我试图隐藏 Delphi 在 StringGrid 中围绕当前选定单元格绘制的边框(焦点矩形)。我正在做所有者绘图以自定义字符串网格的外观。我已经设法摆脱了除了选择之外的所有东西。

我正在使用

 GR.Left := -1;
 GR.Top  := -1;
 GR.Right := -1;
 GR.Bottom := -1;
 StringGrid.Selection := GR;

但是如果你设置得非常快,就会出现错误(我在 onMouseMove 中运行了这个)。我的意思是它工作得很好,但是如果我足够快地调用这个特定的代码块,我会在 StringGrid 的渲染中得到一个异常(因此我不能只是尝试,除了它周围)。

关于如何可靠地解决这个问题的任何想法?

4

4 回答 4

3

您可以为 TStringgrid 使用插入器类并覆盖 Paint 过程以删除绘制的焦点矩形。

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TStringgrid=Class(Grids.TStringGrid)
  private
    FHideFocusRect: Boolean;
  protected
     Procedure Paint;override;
  public
     Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
  End;
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TStringgrid.Paint;
var
 L_Rect:Trect;
begin
  inherited;
   if HideFocusRect then
      begin
       L_Rect := CellRect(Col,Row);
       if DrawingStyle = gdsThemed then InflateRect(L_Rect,-1,-1);
       DrawFocusrect(Canvas.Handle,L_Rect)
      end;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
   StringGrid1.HideFocusRect := not StringGrid1.HideFocusRect;
end;

end.
于 2013-05-23T10:24:37.490 回答
1

OnDrawCell事件中添加

with Sender as TStringgrid do
begin
    if (gdSelected in State) then
    begin
        Canvas.Brush.Color := Color;
        Canvas.Font.Color := Font.Color;
        Canvas.TextRect(Rect, Rect.Left +2,Rect.Top +2, Cells[Col,Row]);
    end;
end;

OnSelectCell事件中添加

CanSelect := False

于 2015-03-31T01:28:29.497 回答
0

DefaultDrawing属性设置为 false 并onDrawCell事件绘制类似这样的文本(这也会交替行颜色和单元格中的中心文本):

procedure GridDrawCell(Sender: TObject; ACol,ARow: Integer; Rect: TRect; State: TGridDrawState);
var S: string;
        c:TColor;
        SavedAlign: word;
begin
  S := Grid.Cells[ACol, ARow];
  SavedAlign := SetTextAlign(Grid.Canvas.Handle,TA_CENTER); 
  if (ARow mod 2 = 0)
  then c:=clWhite
  else c:=$00E8E8E8;
  // Fill rectangle with colour
  Grid.Canvas.Brush.Color := c;
  Grid.Canvas.FillRect(Rect);
  // Next, draw the text in the rectangle
  if (ACol=1) or (ACol=3) or (ACol=5) then
  begin
    Grid.Canvas.Font.Color := $005F5F5F; 
  end
  else
  begin
    Grid.Canvas.Font.Color := $005F5F5F;
  end;
    Grid.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    SetTextAlign(Grid.Canvas.Handle, SavedAlign);
end;
于 2015-08-21T23:33:31.900 回答
0

内部 Stringrid 属性将 TabOrder 更改为 -1 并将 HitTest 更改为 False

于 2021-11-24T18:48:03.417 回答