【问题标题】:Is there a way to call a StringGrid OnCellDraw during runtime有没有办法在运行时调用 StringGrid OnCellDraw
【发布时间】:2020-07-09 13:11:58
【问题描述】:

我有一个程序可以跟踪一年中的预订天数。为了显示这一点,我有一个 StringGrid,我使用颜色来显示预订的天数。预订的日期存储在 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;

然后我想在运行时单击允许用户预订日期的按钮。然后,我希望他们选择的日期反映在 StringGrid 中。如果我更新数组,我将如何再次运行 OnCellDraw 以反映新的预订日期?

谢谢

【问题讨论】:

  • 您的 DrawCell 看起来不仅仅是绘制一个单元格。
  • OnDrawCell 事件由网格调用,分别针对每个单元格。您应该只绘制由AColARow 指示的单元格,使用这些单元格Rect,这就是它们作为参数提供的原因。目前,您花费了大量的额外时间,可能会在每个单元格中绘制 12 X 31 次for iDayfor iMonth)。此外,由于TStringGrid 分配了内存来保存每个单元格的字符串,因此您只需在最初和更改时分配该内容一次,而不是每次绘制单元格时。查看此处的其他帖子,以获取有关如何使用 TStringGrid 的灵感。

标签: delphi delphi-2010


【解决方案1】:

通常,您会使控件的一部分无效,导致它被下一个窗口绘制消息重绘。执行此操作的 TStringGrid 方法受到保护,因此您需要使用破解程序类来访问它们。

// -- add to the type section 
type
  TStringGridCracker = class(TStringGrid);

procedure TForm1.Button1Click(Sender: TObject);
begin
   TStringGridCracker(StringGrid1).InvalidateCell(1,2);
end;

【讨论】:

    【解决方案2】:

    我在一位朋友向我展示后发现,StringGrid.Redraw 过程完成了我所需要的。谢谢大家

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2019-06-23
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多