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 プロシージャをオーバーライドして、描画されたフォーカス rect を削除できます。

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 に答える