码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

把普通方法赋值给事件或者类中的方法

 

--开发环境:D7

 

 

 

------------Unit1--开始-------------

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure MyFormbuttonClick(Sender:TObject);
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses
Unit2;
{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
MybuttonClick(Sender);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
vMybuttonClick:TNotifyEvent;
begin
TMethod(vMybuttonClick).Code:=@MybuttonClick ; //Unit2中的MybuttonClick
Button1.OnClick:=vMybuttonClick;
//Button1.OnClick:=MyFormbuttonClick ; //MyFormbuttonClick可以直接复制
end;

procedure TForm1.MyFormbuttonClick(Sender: TObject);
begin
MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);
end;

end.

 

------------Unit1--结束--------------

--------------Form1开始

object Form1: TForm1
Left = 745
Top = 544
Width = 203
Height = 156
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 48
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
end
object Button2: TButton
Left = 48
Top = 80
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
end

----------------------Form1结束

 

 

-------------------------------Unit2开始

unit Unit2;

interface

uses
Windows;
procedure MybuttonClick(Sender:TObject);
implementation


procedure MybuttonClick(Sender:TObject);
begin
MessageBox(0,PChar('AAA'),PChar('提示'),MB_OK);

end;
end.

-------------------------------Unit2结束

posted on 2021-05-15 16:17  码农的笔记  阅读(395)  评论(0编辑  收藏  举报