delphi 技巧 以相同类名派生一个子类


本文整理自网络,侵删。

 delphi 技巧 以相同类名派生一个子类
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type

  // 以相同类名派生一个子类
  TPanel = class(ExtCtrls.TPanel)
  private
    FOnPaint: TNotifyEvent;
  protected
    // 重载一个方法
    procedure Paint; override;
  public
    // 新定义一个事件
    property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
  end;

  TForm1 = class(TForm)
    pnl1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure OnPnlPaint(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TPanel }

procedure TPanel.Paint;
begin
  inherited;
  if Assigned(FOnPaint) then
    FOnPaint(Self);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 新增加的事件只能在运行期动态关联
  pnl1.OnPaint := OnPnlPaint;
end;

procedure TForm1.OnPnlPaint(Sender: TObject);
begin
  // 这一句是可以执行到的
  pnl1.Canvas.TextOut(10, 10, 'Test');
end;

end.

相关阅读 >>

Delphi 文件的操作:重命名、复制、移动、删除

Delphi 快速远程屏幕传输api版

Delphi批量删除同类文件(带通配符)

Delphi2010中使用pchar时e2010 incompatible types: 'char' and 'ansichar' 错误的处理

Delphi cxgrid:动态设计统计功能

Delphi 的运算符列表

winapi 字符及字符串函数(5): ischaralpha - 是否是个字母

Delphi 字符串分割

Delphi第三方控件通用安装方法

模拟鼠标点击 查找窗口标题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...