前言
Delphi拥有高度的可扩展性和大量的第三方控件的支持,当控件面板上找不到合适控件时,不妨自己开发一个。
一、控件编写
尝试添加一个自定义面板控件TMyPanel,从TCustomPanel派生。
- 选择File-> New-> Other,选择Component
- 选择要集成的控件
- 填写自定义控件名称、控件在控制面板上的位置以及保存的文件名称
- 编写自定义控件,保存为MyPanel.pas
unit MyPanel;
interface
uses
SysUtils, Classes, Controls, ExtCtrls;
type
TMyPanel = class(TCustomPanel)
private
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyPanel]);
end;
{ TMyPanel }
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
end;
destructor TMyPanel.Destroy;
begin
inherited;
end;
end.
- 新建Package包,File-> New-> Package,保存工程为MyPanel
- 把MyPanel.pas文件拷贝到工程目录下并且添加到包内
至此,自定义控件就编写完成
二、自定义控件安装
- 打开自定义控件工程,选择MyPanel.bpl右键安装
- 安装完成提示新控件注册完成
- 控件查看,控件控制面板对应分组下可以找到安装完成的新控件
- 卸载控件,在MyPanel工程下选择MyPanel.bpl右键Uninstall