マウスを使用してセルを選択し、ボタンをクリックして、Delphi ドローグリッド セル内をペイントするにはどうすればよいですか。ボタンを押した後に塗りつぶされるセル。
6546 次
1 に答える
4
グリッド内のセルと同じ数の項目を持つ配列などの別のコンテナーに描画情報を格納し、グリッドのOnDrawCell
イベントを使用して、コンテナーに現在格納されている情報を使用して、必要に応じてセルをペイントします。描画を更新するには、必要に応じてコンテナの内容を単純に更新してからInvalidate()
、グリッドを再描画してOnDrawCell
イベントが新しい情報を使用するようにします。
更新:例:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;
type
CellInfo = record
BkColor: TColor;
end;
TForm1 = class(TForm)
DrawGrid1: TDrawGrid;
Button1: TButton;
procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Cells: array of CellInfo;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Vcl.ExtCtrls;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
R: TGridRect;
Row, Col: Integer;
begin
R := DrawGrid1.Selection;
for Row := R.Top to r.Bottom do
begin
for Col := R.Left to R.Right do
begin
Cells[(Row * DrawGrid1.ColCount) + Col].BkColor := clBlue;
end;
end;
DrawGrid1.Invalidate;
end;
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
CellIndex: Integer;
begin
CellIndex := (ARow * DrawGrid1.ColCount) + ACol;
if gdFixed in State then
begin
DrawGrid1.Canvas.Brush.Color := DrawGrid1.FixedColor;
end
else if (State * [gdSelected, gdHotTrack]) <> [] then
begin
DrawGrid1.Canvas.Brush.Color := clHighlight;
end else
begin
DrawGrid1.Canvas.Brush.Color := Cells[CellIndex].BkColor;
end;
DrawGrid1.Canvas.FillRect(Rect);
if gdFixed in State then
Frame3D(DrawGrid1.Canvas, Rect, clHighlight, clBtnShadow, 1);
if gdFocused in State then
DrawGrid1.Canvas.DrawFocusRect(Rect);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
SetLength(Cells, DrawGrid1.RowCount * DrawGrid1.ColCount);
for I := Low(Cells) to High(Cells) do
begin
Cells[I].BkColor := DrawGrid1.Color;
end;
end;
end.
于 2013-06-18T19:14:55.033 に答える