QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2400|回复: 0
打印 上一主题 下一主题

Assign event handlers at runtime

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:24 |只看该作者 |正序浏览
|招呼Ta 关注Ta

Step 1: Identify out what type of handler you will be writing. OnClick event handlers are TNotifyEvent types. The OnIdle event of TApplication is of the type TIdleEvent, and it passes two arguments to its handler. The VCL help file can help you identify what type of handler you should create for a particular event. For OnIdle, the help file lists:

/ y% |5 Q# Z- V5 d) z) K4 d

__property TIdleEvent OnIdle;

# o- `2 w5 q( n; {! w; l" J( T 0 h* M- l3 o8 P% Z

Step 2: Determine what type of variable the function should return (almost always void), and what variables should be passed to the function. Unfortunately, the help file leaves this part out. For events that could have been created at design time (such as OnClick), you could create a dummy handler, and then cut and paste the function declarations. For handlers that cannot be created at design time, you must refer to the include files.

* ~& ~: z+ M" @) l

The TApplication handler types are listed in FORMS.HPP. You might want to open the file now and take a look. Find the typedef for TIdleEvent. It should look something like:

8 Q- K: d1 X7 n) U& f7 f+ a

: S. A* p5 J! A+ ltypedef void __fastcall (__closure *TIdleEvent)(System::TObject* Sender,3 T/ L) r( u& R( X" X4 i7 B8 w bool &Done);

: g- q6 _1 T0 ~* P

This means that an OnIdle function should look like this:

1 l' R' ^. D2 a* x$ E; j/ M, A

void __fastcall TForm1::AppIdle(System::TObject *Sender, bool &Done)

( e( p i+ r! I# H S( b% f % L/ F- M: t- q& R. o% j" K

Step 3: Add the function declaration to the class that is handling the event. For OnIdle it would look like this:

C; k3 J( Y; }3 M. z0 I- M Q5 i

class TForm1 : public TForm 1 h% F# Q- C! X' A{ ' C/ t3 [. o9 E8 r V' Q __published:! h; D% }+ @& s2 N e private: r4 A) A. W- ^ void __fastcall AppIdle(System::TObject *Sender, bool &Done);

; I Z2 J- F0 R' T

! ?2 L1 E$ Z+ R6 e/ d1 nStep 4: Code the function body.

2 \8 D5 m* g q* t0 Z

void __fastcall TForm1::AppIdle(System::TObject *Sender, bool &Done)( b6 u3 \$ Y0 n7 m { 1 t' H. d o1 B, U1 _% I# x- ^! n // function body here* |1 E" n# b |5 x1 Y/ g }

. w$ ]: F- c z : _0 l1 f0 x" R. |7 g

Step 5: And now, the step you have all been waiting for. To associate your handler function to a component's event, you must assign the function name to the handler. The most convenient place to do this is inside the constructor of the class that contains the handler function.

+ \8 S3 x3 G6 K. K0 V: S

__fastcall TForm1::TForm1(TComponent* Owner) 9 t9 |$ L+ O3 ?) m : TForm(Owner)+ E- } P5 N6 X. I+ l8 p6 q# n {! q* L0 I/ i$ q7 d9 b' x$ D+ S D Application->OnIdle = AppIdle;9 Q2 |& u. H7 Y }

6 e. Z" ?8 E b6 M

Notice that there is nothing fancy here. Just assign the function name to the handler. Do not assign the address of the function to the handler. / i1 b# ~! u5 R" F* U% j0 l% ~

zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-18 19:39 , Processed in 0.460610 second(s), 53 queries .

回顶部