我正在運行Lazarus v0.9.30(32位編譯器)。如何切換單元格顏色和文本在TStringGrid開和關
我有一個TForm標準的TStringGrid就可以了。網格具有以下屬性集。行數= 5,列數= 5,FixedCols = 0,FixedRows = 0
我Google了一些code是教我如何更改單元格顏色和一些文本添加到該小區當用戶點擊一個TStringGrid細胞。所有工作正常,我已經稍微擴展它,以在GridClick事件上打開和關閉顏色/文本。
我的問題更多的是爲了更好地理解代碼中某些元素背後的目的。
有一系列Foregroud(FG)和Background(BG)TColor對象。他們是否在那裏存儲在GridClick事件中設置的單元格顏色屬性,因此如果由於某種原因需要再次觸發DrawCell事件,單元格可能會重繪自己?你可以避免使用TColors數組,只需根據需要設置DrawCell事件中的顏色/文本?
如果需要使用數組,我會假設,尺寸必須與Grid.ColCount匹配和Grid.RowCount(即通過在Form.Create的SetLength呼叫建立)
有沒有一種方法來檢測您要點擊字符串網格的5 x 5個單元格之外(即在空白處),從而阻止GridClick調用DrawCell事件。如果設置AllowOutboundEvents
到False
,則OnClick
活動將只有當你點擊一個特定的細胞燃燒,在你點擊你無論總是得到行和上校
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.
不知道是什麼你的意思是使用TCustomStringGrid對象?代碼的作者有多維數組來存儲TColors .....如何使用TCustomStringGrid幫助我? – user1174918 2012-03-14 04:51:44
對不起,誤導你。修復並添加了一些其他提示和示例。 – TLama 2012-03-14 09:45:12
好的....我看到你在做什麼.....我打算加載一個對象的每個細胞....我只需要添加TColor屬性,因爲我有一個屬性,就像'StateYes' 。一個問題:您沒有使用網格DrawCell事件來繪製單元格,就像在這個答案中一樣? [鏈接](http://stackoverflow.com/questions/6701462/delphi-how-can-i-change-color-of-a-cell-in-string-grid) – user1174918 2012-03-14 12:37:47