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.