⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq17.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Send and respond to user defined messages.</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>







<H3>

Send and respond to user defined messages.

</H3>



<P>

If you don't know how to respond to events, see the FAQ titled

<A TARGET=_top HREF="faq16.htm">Respond to messages sent to the application</A>.

<P>

<B>Step 1:</B> Add a <TT>#define</TT> for a user message ID to the header file of the class

that needs to respond to the message.

</P>

<pre>

    <font color="green">#include &lt;vcl\Classes.hpp></font>

    <font color="green">#include &lt;vcl\Controls.hpp></font>

    <font color="green">#include &lt;vcl\StdCtrls.hpp></font>

    <font color="green">#include &lt;vcl\Forms.hpp></font>



    <font color="navy">// user messages are WM_USER + some value</font>

    <font color="green">#define  UM_SPECIALMESSAGE (WM_USER + 1001)</font>

</pre>

<P>

<B>Step 2:</B> Stay in the same header file and add a message map and a function declaration

to the class that will receive the custom message.

</P>

<pre>

    <b>private</b><b>:</b>     <font color="navy">// User declarations</font>

        <b>void</b> <b>__fastcall</b> UMSpecialMessage<b>(</b>TMessage <b>&</b>Message<b>)</b><b>;</b>

    <b>public</b><b>:</b>      <font color="navy">// User declarations</font>

        <b>__fastcall</b> TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b><b>;</b>



    BEGIN_MESSAGE_MAP

        MESSAGE_HANDLER<b>(</b>UM_SPECIALMESSAGE<b>,</b>TMessage<b>,</b>UMSpecialMessage<b>)</b>

    END_MESSAGE_MAP<b>(</b>TForm<b>)</b>

</pre>

<P>

<B>Step 3:</B> Code the response function. This function just beeps to let you know that it

ran.

</P>

<pre>

      <b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>UMSpecialMessage<b>(</b>TMessage <b>&</b>Message<b>)</b>

      <b>{</b>

          MessageBeep<b>(</b>MB_ICONEXCLAMATION<b>)</b><b>;</b>

      <b>}</b>

</pre>

<P>

<B>Step 4:</B> Somebody needs to send a message. You would do this using <TT>SendMessage</TT>,

<TT>PostMessage</TT>, or <TT>Perform</TT>.

</P>

<pre>

      <font color="navy">// SendMessage and Perform send the message directly to the window, and</font>

      <font color="navy">// the handler will run before the call returns. PostMessage adds the</font>

      <font color="navy">// message to the window's message queue and won't be handled until the</font>

      <font color="navy">// application fetches the message from the queue.</font>

      SendMessage<b>(</b>Form1<b>-></b>Handle<b>,</b> UM_SPECIALMESSAGE<b>,</b> <font color="navy">/*WPARAM*/</font> <font color="blue">0</font><b>,</b> <font color="navy">/*LPARAM*/</font> <font color="blue">0</font><b>)</b><b>;</b>

      PostMessage<b>(</b>Form1<b>-></b>Handle<b>,</b> UM_SPECIALMESSAGE<b>,</b> <font color="navy">/*WPARAM*/</font> <font color="blue">0</font><b>,</b> <font color="navy">/*LPARAM*/</font> <font color="blue">0</font><b>)</b><b>;</b>

      Perform<b>(</b>UM_SPECIALMESSAGE<b>,</b> <font color="navy">/* WPARAM */</font> <font color="blue">0</font><b>,</b> <font color="navy">/* LPARAM */</font> <font color="blue">0</font><b>)</b><b>;</b>

</pre>





</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -