delphi 使用TImage绘制的问题,我已经在TImage上绘制了,但它没有被绘制

e7arh2l6  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(168)

我已经创建了一个示例组件下面。有3个TPanel s包含1个TImage。我希望图像按照代码中的指示绘制自己。这不会发生。
如果构建此组件并将其放到窗体上,则不会看到任何绘画,并且在执行时程序也不会显示任何绘画。
我在Windows 11机器上使用 Delphi 10.4。
我不知道我做错了什么。我尝试使用Paint方法,但这导致了一个循环错误。我只是从创建的组件的绘画命令中复制了相同的细节到Paint。不是一个好结果。

unit Schedule;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Controls,
  Vcl.ExtCtrls, Vcl.Graphics, Vcl.StdCtrls, Vcl.Forms;

type
  TEBSSchedule = class(TCustomControl)
  private
    Panel1, Panel2, Panel3: TPanel;
    Image1, Image2, Image3: TImage;
    ScrollBar1, ScrollBar2: TScrollBar;

    procedure Image3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image3MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Image3MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

    procedure CreatePanels;
    procedure CreateScrollBars;
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

Procedure Register;

implementation

constructor TEBSSchedule.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 400;
  Height := 300;

  CreatePanels;
  CreateScrollBars;
end;

destructor TEBSSchedule.Destroy;
begin
  if Assigned(Image1) then Image1.Free;
  if Assigned(Image2) then Image2.Free;
  if Assigned(Image3) then Image3.Free;
  if Assigned(Panel1) then Panel1.Free;
  if Assigned(Panel2) then Panel2.Free;
  if Assigned(Panel3) then Panel3.Free;
  if Assigned(ScrollBar1) then ScrollBar1.Free;
  if Assigned(ScrollBar2) then ScrollBar2.Free;

  inherited Destroy;
end;

procedure TEBSSchedule.CreatePanels;
var
  T: TRect;
begin
  T.Left := 0;
  T.Top := 0;
  Panel1 := TPanel.Create(Self);
  Panel1.Parent := Self;
  Panel1.SetBounds(100,0,Self.Width-100,25);
  Panel1.Anchors := [akLeft,akTop,akRight];
  Image1 := TImage.Create(Panel1);
  Image1.Parent := Panel1;
  Image1.Align := alClient;
  Image1.Picture.Bitmap.Canvas.Brush.Color := clBlue;
  T.Right := Panel1.Width;
  T.Bottom := Panel1.Height;
  Image1.Picture.Bitmap.Canvas.FillRect(T);

  Panel2 := TPanel.Create(Self);
  Panel2.Parent := Self;
  Panel2.SetBounds(0,Panel1.Height,100,Self.Height-25);
  Panel2.Anchors := [akLeft,akTop,akBottom];
  Image2 := TImage.Create(Panel2);
  Image2.Parent := Panel2;
  Image2.Align := alClient;
  Image2.Picture.Bitmap.Canvas.Brush.Color := clBlue;
  T.Right := Panel2.Width;
  T.Bottom := Panel2.Height;
  Image2.Picture.Bitmap.Canvas.FillRect(T);

   Panel3 := TPanel.Create(Self);
  Panel3.Parent := Self;
  Panel3.SetBounds(Panel2.Width,Panel1.Height,
  Self.Width-Panel2.Width,Self.Height-Panel1.Height);
  Panel3.Anchors := [akLeft,akTop,akRight,akBottom];
  Image3 := TImage.Create(Panel3);
  Image3.Parent := Panel3;
  Image3.Align := alClient;
  Image3.Picture.Bitmap.Canvas.Brush.Color := clYellow;
  T.Right := Panel3.Width;
  T.Bottom := Panel3.Height;
  Image3.Picture.Bitmap.Canvas.FillRect(T);
  Image3.OnMouseDown := Image3MouseDown;
  Image3.OnMouseMove := Image3MouseMove;
  Image3.OnMouseUp := Image3MouseUp;
end;

procedure TEBSSchedule.CreateScrollBars;
begin
  ScrollBar1 := TScrollBar.Create(Self);
  ScrollBar1.Parent := Self;
  ScrollBar1.Kind := sbHorizontal;
  ScrollBar1.Align := alBottom;

  ScrollBar2 := TScrollBar.Create(Self);
  ScrollBar2.Parent := Self;
  ScrollBar2.Kind := sbVertical;
  ScrollBar2.Align := alRight;
end;

procedure TEBSSchedule.Paint;
begin
  inherited Paint;
end;

procedure TEBSSchedule.Image3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
end;

procedure TEBSSchedule.Image3MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
end;

procedure TEBSSchedule.Image3MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
end;

procedure Register;
begin
  RegisterComponents('EBSH', [TEBSSchedule]);
end;

end.

字符串

pvabu6sv

pvabu6sv1#

你没有看到任何画,因为没有什么可画的。
当您第一次访问TImage.Picture.Bitmap属性时,TImage中还没有加载任何图形,因此Bitmap属性getter创建了一个TBitmap对象,尺寸为0x0。这是(某种程度上)记录的行为(尽管措辞有点误导1):
https://docwiki.embarcadero.com/Libraries/en/Vcl.Graphics.TPicture.Bitmap
当图片对象包含位图时,使用Bitmap引用图片对象。如果当图片包含图元文件或图标图形时引用Bitmap,则不会转换图形(Types of Graphic Objects)。相反,图片的原始内容将被丢弃**,Bitmap将返回新的空白位图**。
1:文档 * 真正的意思 * 更像是这样:
当图片对象包含位图时,使用Bitmap引用图片对象。如果在图片不包含位图图形时引用Bitmap,则不会转换任何现有图形(Types of Graphic Objects)。相反,图片的原始内容将被丢弃**,Bitmap将返回一个新的、没有大小的空白位图**。
因此,您在TBitmap的可用绘图区域边界之外的Bitmap.Canvas上进行绘图。
要解决这个问题,您需要调整TBitmap对象的大小,然后才能在其上绘制任何内容,例如:

Image1.Picture.Bitmap.SetSize(Image1.Width, Image1.Height); // <-- ADD THIS!
Image1.Picture.Bitmap.Canvas.Brush.Color := clBlue;

字符串

相关问题