【问题标题】:Draw TBitmaps in DrawGrid in Delphi在 Delphi 的 DrawGrid 中绘制 TBitmaps
【发布时间】:2014-05-24 20:59:53
【问题描述】:

我在 Delphi XE5 中有一个 8 x 16 DrawGrid,我想用我存储在 C:\Users\Sean Ewing\Documents\My Documents\Delphi Tutorials\Other\Math 中的九个图像随机填充它-O-Sphere\Win32\Debug\img。我目前正在尝试加载一张图片以确保我做得正确。这是我用来执行此操作的代码:

    procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
     Rect: TRect; State: TGridDrawState);
      var
        spherePlus: TBitmap;

      begin
        spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) + '\img\Sphere +1.bmp');
        grdPlayField.Canvas.Draw(0, 0, spherePlus);
      end;

代码编译良好,根据我在 Embarcadero wiki 中阅读的内容,这是正确的,但在加载 DrawGgrid 时,我在运行时遇到错误。我哪里做错了?

【问题讨论】:

  • 你忘记了spherePlus := TBitmap.Create。你买的是AV吧?当您提出问题时,不要忘记说明您遇到了什么错误,以及在哪里出错。
  • 只加载一次位图。不是每次你都要画画。

标签: delphi gridview bitmap


【解决方案1】:

您需要先创建位图才能使用它:

procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
 Rect: TRect; State: TGridDrawState);
  var
    spherePlus: TBitmap;
  begin
    spherePlus := TBitmap.Create;
    try
      spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) + 
          '\img\Sphere +1.bmp');
      grdPlayField.Canvas.Draw(0, 0, spherePlus);
    finally
      spherePlus.Free;
    end;
  end;

您应该注意的另一件事是,您在事件中收到的Rect 参数是需要绘制的区域,因此您需要使用Canvas.StretchDraw 并将该矩形传递给它。它对当前问题没有帮助,但当您进入下一步时,您将需要它。您可以使用 AColARow 参数来识别正在绘制的确切单元格,因此您可以使用该信息为列加载特定图像,或为列或行输出文本。

// Load specific image for the cell passed in ACol and ARow,
// and then draw it to the appropriate area using the Rect provided.
grdPlayField.Canvas.StretchDraw(Rect, spherePlus);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多