3

Lazarus v0.9.30(32ビットコンパイラ)を実行しています。

標準のTStringGridを備えたTFormがあります。グリッドには次のプロパティが設定されています。RowCount = 5、ColumnCount = 5、FixedCols = 0、FixedRows=0。

ユーザーがTStringGridセルをクリックしたときに、セルの色を変更してセルにテキストを追加する方法を示すコードをGoogleで検索しました。すべて正常に動作し、GridClickイベントで色/テキストのオンとオフを切り替えるために少し拡張しました。

私が持っている質問は、コードのいくつかの要素の背後にある目的をよりよく理解することです。

Foregroud(FG)およびBackground(BG)TColorオブジェクトの配列があります。それらはGridClickイベントに設定されたセルの色属性を格納するためにあります。したがって、何らかの理由でDrawCellイベントを再度トリガーする必要がある場合、セルはそれ自体を再描画できますか?TColorsの配列の使用を避け、必要に応じてDrawCellイベントの色/テキストを設定することはできますか?

配列を使用する必要がある場合は、ディメンションがGrid.ColCountおよびGrid.RowCountと一致する必要があると想定します(つまり、Form.CreateのSetLength呼び出しで設定します)。

stringgridの5x5セルの外側(つまり空白内)をクリックしていることを検出して、GridClickがDrawCellイベントを呼び出さないようにする方法はありますか?クリックした場所に関係なく、常に行と列の有効な値を取得します。

unit testunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, Menus, ComCtrls, Buttons, Grids, StdCtrls, Windows, Variants,
  LCLType;
type

  { TForm1 }

  TForm1 = class(TForm)
    Grid: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure GridClick(Sender: TObject);
    procedure GridDrawCell(Sender: TObject; aCol, aRow: Integer;
      aRect: TRect; aState: TGridDrawState);
  end; 

var
  Form1: TForm1; 

implementation

var
  FG: array of array of TColor;
  BG: array of array of TColor;

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  Col, Row: integer;
begin
  // Set the sizes of the arrays
  SetLength(FG, 5, 5);
  SetLength(BG, 5, 5);

  // Initialize with default colors
  for Col := 0 to Grid.ColCount - 1 do begin
    for Row := 0 to Grid.RowCount - 1 do begin
      FG[Col, Row] := clBlack;
      BG[Col, Row] := clWhite;
    end;
  end;
end;

procedure TForm1.GridDrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
var
  S: string;
begin
  S := Grid.Cells[ACol, ARow];

  // Fill rectangle with colour
  Grid.Canvas.Brush.Color := BG[ACol, ARow];
  Grid.Canvas.FillRect(aRect);

  // Next, draw the text in the rectangle
  Grid.Canvas.Font.Color := FG[ACol, ARow];
  Grid.Canvas.TextOut(aRect.Left + 22, aRect.Top + 2, S);
end;

procedure TForm1.GridClick(Sender: TObject);
var
  Col, Row: integer;
begin
  Col := Grid.Col;
  Row := Grid.Row;

  // Set the cell color and text to be displayed
  if (Grid.Cells[Col,Row] <> 'Yes') then
    begin
      BG[Col, Row] := rgb(131, 245, 44);
      FG[Col, Row] := RGB(0, 0, 0);
      Grid.Cells[Col, Row] := 'Yes'
    end {if}
  else
    begin
      BG[Col, Row] := rgb(255, 255, 255);
      FG[Col, Row] := RGB(255, 255, 255);
      Grid.Cells[Col, Row] := '';
    end; {else}
end;

end.
4

1 に答える 1

4

に設定するAllowOutboundEventsFalseOnClick空白をクリックしたときではなく、特定のセルをクリックしたときにのみイベントが発生します。したがって、このプロパティを使用すると、どこかをクリックしたときに常に有効なセル座標が取得されます。

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.AllowOutboundEvents := False;
  ...
end; 

もう1つのポイントは、テキストレンダリングを含むすべてをペイントする必要があるため、OnPrepareCanvasの代わりにイベントを使用する必要があるということです。レンダリングするセルごとにとを設定するだけです。OnDrawCellOnDrawCellOnPrepareCanvasBrush.ColorFont.Color

また、配列を使用する必要はありませんObjects。列で行ったのと同じように使用できますが、配列の色を維持することはできます。次の例ではObjects、を使用し、イベントの使用法も示していますが、OnPrepareCanvasこの例と質問の例では、固定セルを含むすべてのセルが色付けされていることに注意してください。

type
  TCellData = class(TObject)
  private
    FStateYes: Boolean;
    FForeground: TColor;
    FBackground: TColor;
  public
    property StateYes: Boolean read FStateYes write FStateYes;
    property Foreground: TColor read FForeground write FForeground;
    property Background: TColor read FBackground write FBackground;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Col, Row: Integer;
  CellData: TCellData;
begin
  for Col := 0 to StringGrid1.ColCount - 1 do
    for Row := 0 to StringGrid1.RowCount - 1 do
    begin
      CellData := TCellData.Create;
      CellData.StateYes := False;
      CellData.Foreground := clBlack;
      CellData.Background := clWhite;
      StringGrid1.Objects[Col, Row] := CellData;
    end;
  StringGrid1.AllowOutboundEvents := False;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  Col, Row: Integer;
begin
  for Col := 0 to StringGrid1.ColCount - 1 do
    for Row := 0 to StringGrid1.RowCount - 1 do
      StringGrid1.Objects[Col, Row].Free;
end;

procedure TForm1.StringGrid1Click(Sender: TObject);
var
  Col, Row: Integer;
  CellData: TCellData;
begin
  Col := StringGrid1.Col;
  Row := StringGrid1.Row;

  if StringGrid1.Objects[Col, Row] is TCellData then
  begin
    CellData := TCellData(StringGrid1.Objects[Col, Row]);
    if CellData.StateYes then
    begin
      StringGrid1.Cells[Col, Row] := '';
      CellData.StateYes := False;
      CellData.Foreground := RGB(255, 255, 255);
      CellData.Background := RGB(255, 255, 255);
    end
    else
    begin
      StringGrid1.Cells[Col, Row] := 'Yes';
      CellData.StateYes := True;
      CellData.Foreground := RGB(0, 0, 0);
      CellData.Background := RGB(131, 245, 44);
    end;
  end;
end;

procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
var
  CellData: TCellData;
begin
  if StringGrid1.Objects[ACol, ARow] is TCellData then
  begin
    CellData := TCellData(StringGrid1.Objects[ACol, ARow]);
    StringGrid1.Canvas.Brush.Color := CellData.Background;
    StringGrid1.Canvas.Font.Color := CellData.Foreground;
  end;
end;
于 2012-03-13T13:30:56.273 に答える