在C++BUILDER中如何使用自訂訊息和編寫自訂訊息的訊息處理函數
---------------------------------------------------------------
unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#define WM_MYMESSAGE WM_APP+1 //自訂訊息ID
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
void __fastcall CMMessage(TMessage &msg); //訊息接收處理函數
public: // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_MYMESSAGE,TMessage,CMMessage)
//決定WM_MYMESSAGE訊息交給CMMessage 函數處理
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
unit.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CMMessage(TMessage &msg)
{
if(msg.WParam == 0){
ShowMessage("haha");
}else
if(msg.WParam == 1){
ShowMessage("xixi");
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SendMessage(Handle,WM_MYMESSAGE,1,0); //發送訊息
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
SendMessage(Handle,WM_MYMESSAGE,0,0);//發送訊息
}
//---------------------------------------------------------------------------