C++ builder消息的运用


  一、TApplication的OnMessage事件
  OnMessage事件只处理消息队列中的消息,SendMessage()发送的消息不会被截获。

  任何窗体接收到一个windows消息,都会触发一次此事件

  函数原型:

  typedef void __fastcall (__closure *TMessageEvent) (tagMSG &Msg,bool &Handled);

  Msg表示被截获的消息,Handled表示是否处理完成,为TRUE则防止被再次处理,设为FALSE则可以继续处理。

  代码

  //计算程序响应的消息数

  //---------------------------------------------------------------------------

  #ifndef Msg_testH

  #define Msg_testH

  //---------------------------------------------------------------------------

  #include <Classes.hpp>

  #include <Controls.hpp>

  #include <StdCtrls.hpp>

  #include <Forms.hpp>

  //---------------------------------------------------------------------------

  class TForm1 : public TForm

  {

  __published:    // IDE-managed Components

  TLabel *Label1;

  void __fastcall FormCreate(TObject *Sender);

  private:    // User declarations

  //自定义消息响应函数

  void __fastcall AppMessage(tagMSG &MSG,bool &Handled);

  public:        // User declarations

  __fastcall TForm1(TComponent* Owner);

  int Num;

  };

  //---------------------------------------------------------------------------

  extern PACKAGE TForm1 *Form1;

  //---------------------------------------------------------------------------

  #endif

  //---------------------------------------------------------------------------

  #include <vcl.h>

  #pragma hdrstop

  #include "Msg_test.h"

  //---------------------------------------------------------------------------

  #pragma package(smart_init)

  #pragma resource "*.dfm"

  TForm1 *Form1;

  //---------------------------------------------------------------------------

  __fastcall TForm1::TForm1(TComponent* Owner)

  : TForm(Owner)

  {

  Num = 0;

  }

  //---------------------------------------------------------------------------

  void __fastcall TForm1::FormCreate(TObject *Sender)

  {

  //将自定义函数与OnMessage事件联系起来

  Application->OnMessage = AppMessage;

  }

  //---------------------------------------------------------------------------

  void __fastcall TForm1::AppMessage(tagMSG &MSG,bool &Handled)

  {

  Num++;

  Label1->Caption = AnsiString(Num);

  Handled = false;

  }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 下一页

相关内容