五、非标准消息
1.通知消息(Notification message)
此消息只发生在一些标准windows控件上
当一个窗体的子控件发生了一些事情后,他通知给其父窗体的消息
包括:按钮、编辑框、列表空、label等..
如:BN_CLICKED 单击按钮
2.自定义消息
相比通过改变对象成员的优点:可以不用知道接收者的确切类型,只要知道其窗口句柄;可以广播给多个接受者。
一般有两种方式:直接定义,WM_USER + XXX 或 WM_APP+XXX 分别为0x0400和0x8000
或调用API函数RegisterWindowMessage()向系统注册一个
3.发送自定义消息
向特定窗体发送消息:
TControl::Perform() 由C++ builder提供
SendMessage() 和 PostMessage() API函数
发送广播消息
TWinControl::Broadcast() 由C++ builder提供
BroadcastSystemMessage() API函数
Perform() 适用于在同一应用程序的不同窗体和控件消息的传递。
SendMessage() 把消息直接发送给窗口函数(不入队列),等消息被处理后才返回。
PostMessage() 只是单纯吧消息送到消息队列中,就立刻返回。
Boradcast() 只能向C++ builder应用程序中的指定窗体上的所有子控件广播消息,无法向其他应用程序广播。
BroadcastSystemMessage() 可以向任意的应用程序或者组件广播消息。
代码
//窗体1(四个按钮)
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Unit2.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#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::Button1Click(TObject *Sender)
{
Form2->Perform(WM_CLOSE,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
SendMessage(Form2->Handle,WM_CLOSE,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
PostMessage(Form2->Handle,WM_CLOSE,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//向窗体Form2的子控件广播WM_LBUTTONDOWN消息
TMessage M;
M.Msg = WM_LBUTTONDOWN;
Form2->Broadcast(&M);
}
//---------------------------------------------------------------------------
//窗体2(两个panel)
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
TPanel *Panel1;
TPanel *Panel2;
void __fastcall Panel1MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Panel2MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Panel1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Panel1->Caption = "Down";
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Panel2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Panel2->Caption = "Down";
}
//---------------------------------------------------------------------------