简体   繁体   English

Delphi创建带有图像的按钮

[英]Delphi create button with image

I have problem with creating button with Image and Label inside it. 我在创建带有图像和标签的按钮时遇到问题。 Here my code: 这是我的代码:

Class: 类:

  type
  Folder = class(TButton)
    AName:TLabel;
    AImage:TImage;
    constructor Create(Nme:String;Path:String;Handle:TForm);
  end;

Constructor: 构造函数:

constructor Folder.Create(Nme:String;Path:String;Handle:TForm);
begin
  AImage:=Timage.Create(Self);
  AName:=TLabel.Create(Self);
  AImage.Parent:=Self;
  AName.Parent:=Self;
  AName.Caption:=Nme;
  AImage.Picture.LoadFromFile(Path);
end;`

And event where I create this button: 还有创建此按钮的事件:

procedure TForm3.Button1Click(Sender: TObject);
  var Fld:Folder;
  begin
  Fld:=Folder.Create('It','D:\image.bmp',Form3);
  Fld.Parent:=Form3;
  Fld.Width:=100;
  Fld.Height:=100;
end;

But when I'm creating this button it causes acess violation!What I must to do with it? 但是当我创建此按钮时,它会导致访问冲突!我该怎么办?

Problem: 问题:

The problem is that you have declared a customized version of constructor, but you are not calling the parent constructor of TButton class. 问题是您已经声明了构造函数的自定义版本,但是您没有调用TButton类的父构造函数。

You need to change the constructor like this: 您需要像这样更改构造函数:

constructor Folder.Create(Nme: String; Path: String; Handle: TForm);
begin
  inherited Create(Handle);     // <- Add this line
  AImage := TImage.Create(Self);
  AName := TLabel.Create(Self);
  AImage.Parent := Self;
  AName.Parent := Self;
  AName.Caption := Nme;
  AImage.Picture.LoadFromFile(Path);
end;

General advice: 一般建议:

You need to learn how to debug such problems yourself. 您需要学习如何自己调试此类问题。

Put a breakpoint on line Fld:=Folder.Create('It','D:\\image.bmp',Form3); Fld:=Folder.Create('It','D:\\image.bmp',Form3);上放置一个断点 and use Step Over F8 / Trace Into F7 from Run menu to check your code line by line. 并使用Run菜单中的“ Step Over F8 / Trace Into F7 Run逐行检查代码。

You will see that once you reach the line AImage.Parent:=Self; 您将看到到达行AImage.Parent:=Self; the exception occurs. 发生异常。 This is because Self , which points to your Folder object, was not initialized correctly, and is not a proper TButton descendant. 这是因为指向您的Folder对象的Self未正确初始化,并且不是正确的TButton后代。

You need to learn how to do that to progress any further with Delphi, and you will very soon be able to solve such problems yourself. 您需要学习如何做到这一点,以进一步使用Delphi进行开发,并且您很快就能自己解决此类问题。


Also if you need to write a custom component for Delphi, invest some time learning more about the way components work and are being used. 另外,如果您需要为Delphi编写自定义组件,请花一些时间来了解有关组件工作方式和使用方式的更多信息。 I would recommend the following guides on component writing: 我将建议以下有关组件编写的指南:


Also consult a guide on Delphi Coding Style . 另请参阅有关Delphi编码样式的指南。

At first glance: 乍一看:

  • Class names should begin with T 类名应以T开头
  • Class fields should begin with F instead of A 类字段应以F而不是A开头
  • constructor should be in public section and fields in private or protected constructor应位于公共部分,字段应为privateprotected
  • You should use spaces around parameters, after variables in declarations and around operators 您应该在参数周围,声明中的变量之后和运算符周围使用空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM