Delphi拾遗(8) 类事件
类的事件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyEvent = procedure of object; //不带参数的过程
TMyEventExt = procedure(AName: string) of object; //带参数的过程
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyBase = class
private
FName : string;
FAge : Integer;
FOnEvent: TMyEvent; //定义 TMyEvent 类型事件
FOnEventExt: TMyEventExt;
procedure SetAge(const AValue: Integer);
public
//创建类时进行相应的一些初始化工作
constructor Create;
procedure SetEvent1;
procedure SetEvent2;
procedure SetEventExt1(ATmp: string);
//Name属性 不可更改
property Name: string read FName write FName;
//Age属性 可以更改
property Age: Integer read FAge write SetAge;
//关联事件 发布 TMyEvent 类型事件
property OnEvent: TMyEvent read FOnEvent write FOnEvent;
property OnEventExt: TMyEventExt read FOnEventExt write FOnEventExt;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyBase }
constructor TMyBase.Create;
begin
FName := 'hehf';
FAge := 99; //不赋值时默认为0
FOnEvent := SetEvent1;
FOnEventExt := SetEventExt1; //这时不能带参数
end;
procedure TMyBase.SetAge(const AValue: Integer);
begin
if (AValue > 0) and (AValue < 130) then
FAge := AValue
else
FAge := -1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TmpMy: TMyBase;
begin
TmpMy := TMyBase.Create;
ShowMessage(IntToStr(TmpMy.Age)); // 99
TmpMy.Age := 100;
ShowMessage(IntToStr(TmpMy.Age)); // 100
TmpMy.OnEvent; //触发关联事件
TmpMy.Free;
end;
procedure TMyBase.SetEvent1;
begin
ShowMessage('Event 1'
相关文档:
1、首先将delphi中Controls单元提取
2、修改Controls单元中如下部分:
procedure TWinControl.CreateParams(var Params: TCreateParams);
begin
FillChar(Params, SizeOf(Params), 0);
with Params do
begin
Caption := FText;
Style := WS_CHILD or WS_CLIPSIBLINGS;
&nbs ......
屏幕的分辨率用这个
x=GetSystemMetrics(SM_CXSCREEN)
y=GetSystemMetrics(SM_CYSCREEN)
同上。
.而且获得屏幕上的像素好像应该使用
screen.pixelsperinch函数
int GetDeviceCaps(
  ......
Day 开头的函数
●
Unit
DateUtils
function DateOf(const Avalue: TDateTime): TDateTime;
描述
使用 DateOf 函数用来把一个 TDateTime 类型的变量转变成一个
只带有日期的 TDateTime 类型变量。
例如:
showmessage(DateTimetostr(dateof(now())));
你得到的是 2003/03/19
而 showmessage(DateTime ......
枚举类型
Pascal程序不仅用于数值处理,还更广泛地用于处理非数值的数据。例如,性别、月份、星期几、颜色、单位名、学历、职业等。
1、枚举类型的定义
格式: type 枚举类型标识符=(标识符1,标识符2,…,标识符n)
2、枚举类型数据特点
① 枚举元素只能是标识符;
例如,下列类型定义是合法的:
type ......
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
......