有没有办法在运行时调用 StringGrid OnCellDraw
Is there a way to call a StringGrid OnCellDraw during runtime
我有一个程序可以跟踪一年中预订的天数。为了显示这个,我有一个 StringGrid,我使用 Colors 来显示预订的天数。预订的天数存储在 ar2Booking 中,它是一个二维数组,分别包含日期和月份。
procedure TfrmClient.stgYearPlan1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
k, iMonth, iDay : Integer;
begin
for k := 1 to 31 do
stgYearPlan1.Cells[k,0] := IntToStr(k);
for k := 1 to 12 do
stgYearPlan1.Cells[0,k] := ShortMonthNames[k];
for iDay := 1 to 31 do
for iMonth := 1 to 12 do
begin
if ar2Booking[iDay,iMonth] = 'Y' then
begin
if (ACol = iDay) and (ARow = iMonth) then
begin
stgYearPlan1.Canvas.Brush.Color := clBlack;
stgYearPlan1.Canvas.FillRect(Rect);
stgYearPlan1.Canvas.TextOut(Rect.Left,Rect.Top,stgYearPlan1.Cells[ACol, ARow]);
end;
end;
if ar2Booking[iDay,iMonth] = 'D' then
begin
if (ACol = iDay) and (ARow = iMonth) then
begin
stgYearPlan1.Canvas.Brush.Color := clSilver;
stgYearPlan1.Canvas.FillRect(Rect);
stgYearPlan1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,stgYearPlan1.Cells[ACol, ARow]);
end;
end;
end;
end;
然后我想在 运行 时间内单击允许用户预订日期的按钮。然后我希望他们 select 的日期反映在 StringGrid 中。如果我更新数组,我如何才能再次 运行 OnCellDraw 以反映新的预订日期?
谢谢
通常,您会使控件的一部分无效,从而导致使用下一个 windows 绘制消息重新绘制它。 TStringGrid 执行此操作的方法受到保护,因此您需要使用黑客 class 来访问它们。
// -- add to the type section
type
TStringGridCracker = class(TStringGrid);
procedure TForm1.Button1Click(Sender: TObject);
begin
TStringGridCracker(StringGrid1).InvalidateCell(1,2);
end;
我在一位朋友向我展示后发现,StringGrid.Redraw 程序可以完成我所需要的。谢谢大家
我有一个程序可以跟踪一年中预订的天数。为了显示这个,我有一个 StringGrid,我使用 Colors 来显示预订的天数。预订的天数存储在 ar2Booking 中,它是一个二维数组,分别包含日期和月份。
procedure TfrmClient.stgYearPlan1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
k, iMonth, iDay : Integer;
begin
for k := 1 to 31 do
stgYearPlan1.Cells[k,0] := IntToStr(k);
for k := 1 to 12 do
stgYearPlan1.Cells[0,k] := ShortMonthNames[k];
for iDay := 1 to 31 do
for iMonth := 1 to 12 do
begin
if ar2Booking[iDay,iMonth] = 'Y' then
begin
if (ACol = iDay) and (ARow = iMonth) then
begin
stgYearPlan1.Canvas.Brush.Color := clBlack;
stgYearPlan1.Canvas.FillRect(Rect);
stgYearPlan1.Canvas.TextOut(Rect.Left,Rect.Top,stgYearPlan1.Cells[ACol, ARow]);
end;
end;
if ar2Booking[iDay,iMonth] = 'D' then
begin
if (ACol = iDay) and (ARow = iMonth) then
begin
stgYearPlan1.Canvas.Brush.Color := clSilver;
stgYearPlan1.Canvas.FillRect(Rect);
stgYearPlan1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,stgYearPlan1.Cells[ACol, ARow]);
end;
end;
end;
end;
然后我想在 运行 时间内单击允许用户预订日期的按钮。然后我希望他们 select 的日期反映在 StringGrid 中。如果我更新数组,我如何才能再次 运行 OnCellDraw 以反映新的预订日期?
谢谢
通常,您会使控件的一部分无效,从而导致使用下一个 windows 绘制消息重新绘制它。 TStringGrid 执行此操作的方法受到保护,因此您需要使用黑客 class 来访问它们。
// -- add to the type section
type
TStringGridCracker = class(TStringGrid);
procedure TForm1.Button1Click(Sender: TObject);
begin
TStringGridCracker(StringGrid1).InvalidateCell(1,2);
end;
我在一位朋友向我展示后发现,StringGrid.Redraw 程序可以完成我所需要的。谢谢大家