📄 faq1.htm
字号:
<HTML>
<HEAD>
<TITLE>Assign event handlers at runtime</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Assign event handlers at runtime?<BR>
</H3>
<P>
<B>Step 1:</B> Identify out what type of handler you will be writing. <TT>OnClick</TT> event
handlers are <TT>TNotifyEvent</TT> types. The <TT>OnIdle</TT> event of <TT>TApplication</TT> is
of the type <TT>TIdleEvent</TT>, 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
<TT>OnIdle</TT>, the help file lists:
</P>
<pre>
<b>__property</b> TIdleEvent OnIdle<b>;</b>
</pre>
<BR>
<P>
<B>Step 2:</B> Determine what type of variable the function should return (almost always
<TT>void</TT>), 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 <TT>OnClick</TT>), 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. </P>
<P>
The <TT>TApplication</TT> handler types are listed in FORMS.HPP. You might want to
open the file now and take a look. Find the <TT>typedef</TT> for <TT>TIdleEvent</TT>. It should
look something like:
<BR>
<pre>
<b>typedef</b> <b>void</b> <b>__fastcall</b> <b>(</b><b>__closure</b> <b>*</b>TIdleEvent<b>)</b><b>(</b>System<b>:</b><b>:</b>TObject<b>*</b> Sender<b>,</b>
<b>bool</b> <b>&</b>Done<b>)</b><b>;</b>
</pre>
<P>
This means that an <TT>OnIdle</TT> function should look like this:
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>AppIdle<b>(</b>System<b>:</b><b>:</b>TObject <b>*</b>Sender<b>,</b> <b>bool</b> <b>&</b>Done<b>)</b>
</pre>
<BR>
<P>
<B>Step 3:</B> Add the function declaration to the class that is handling the event. For
<TT>OnIdle</TT> it would look like this:</P>
<pre>
<b>class</b> TForm1 <b>:</b> <b>public</b> TForm
<b>{</b>
<b>__published</b><b>:</b>
<b>private</b><b>:</b>
<b>void</b> <b>__fastcall</b> AppIdle<b>(</b>System<b>:</b><b>:</b>TObject <b>*</b>Sender<b>,</b> <b>bool</b> <b>&</b>Done<b>)</b><b>;</b>
</pre>
<P>
<BR>
<B>Step 4:</B> Code the function body.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>AppIdle<b>(</b>System<b>:</b><b>:</b>TObject <b>*</b>Sender<b>,</b> <b>bool</b> <b>&</b>Done<b>)</b>
<b>{</b>
<font color="navy">// function body here</font>
<b>}</b>
</pre>
<BR>
<P>
<B>Step 5:</B> 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. </P>
<pre>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
Application<b>-></b>OnIdle <b>=</b> AppIdle<b>;</b>
<b>}</b>
</pre>
<P>
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.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -