gpt4 book ai didi

delphi - 如何使用按钮单击在绘图网格中绘制选定的单元格

转载 作者:行者123 更新时间:2023-12-03 19:42:06 24 4
gpt4 key购买 nike

使用鼠标选择单元格并单击按钮后,如何在 Delphi drawgrid 单元格内绘制。按下按钮后要绘制的单元格。

最佳答案

将绘图信息存储在单独的容器中,例如与网格中的单元格具有相同项数的数组,然后使用网格的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.

关于delphi - 如何使用按钮单击在绘图网格中绘制选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com