對于C++ Builder的程序員來說,VCL以其靈活、高效的特點令人喜愛。因為VCL是在Windows API的基礎上進行了封裝,同時舍棄了一些不常用的功能,所以,VCL在功能上是Windows API 的子集。VCL提供了對大多數Windows消息的處理機制,但是對于沒有處理的Windows消息呢,在需要是如何捕獲呢?C++ Builder采用了消息映射標機制,通過消息映射表將特定的Windows消息于代碼中的函數聯系起來,當窗口捕獲到消息時就會調用這個函數。 C++ Builder消息映射表定義形式如下: BEGIN_MESSAGE_MAP MESSAGE_HANDLER(<message> , <message structure> , <message handler>) END_MESSAGE_MAP(ClassName) 其中: BEGIN_MESSAGE_MAP:消息映射定義起始語句 MESSAGE_HANDLER:消息處理定義 END_MESSAGE_MAP:消息映射定義結束語句 ClassName:是要接受消息的類名 message:是要截獲的Windows消息 message handler:消息處理函數名稱 message structure:是用于傳遞給VCL消息的結構名稱,此結構里包含有處理消息時所需的全部參數,不同的消息所對應的消息結構是不同的。 由于每一個VCL控件(無論是窗體還是按鈕)都可以獨立的接收Windows消息,并且進行獨立的響應,所以一定要注意消息定一種的ClassName參數。 現在舉例說明消息定義、傳遞的應用。現在一個窗體FormMain,和2個TperformanceGraph控件(不能響應鼠標事件),現在我要對2個TperformanceGraph控件定義鼠標單擊事件,對FormMain也重定義鼠標單擊事件,過程如下(假定工程文件為Message.bpr,程序文件為main.cpp、main.h): 源代碼如下: //----main.h-------------------------------------------------------------- #ifndef mainH #define mainH //---------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include "PERFGRAP.h" //----------------------------------------------------------- class TFormMain : public TForm { __published: // IDE-managed Components //----2個標準TperformanceGraph控件 TPerformanceGraph *PerformanceGraph1; TPerformanceGraph *PerformanceGraph2; TEdit *Edit2; TEdit *Edit1; void __fastcall FormCreate(TObject *Sender); private: // User declarations //----自定義的消息處理函數,其中MESSAGE可以不寫 MESSAGE void __fastcall LButtonDown(TMessage &message); //----用戶定義的函數(處理消息,具體使用見.cpp文件) void __fastcall MyWndProc1(TMessage &message); void __fastcall MyWndProc2(TMessage &message); //----函數指針,用來保存2個TperformanceGraph控件的消息處理函數的指針 TWndMethod OldWndProc1 , OldWndProc2; public: // User declarations __fastcall TFormMain(TComponent* Owner); //----窗體的消息定義 BEGIN_MESSAGE_MAP MESSAGE_HANDLER(WM_LBUTTONDOWN , TMessage , LButtonDown) END_MESSAGE_MAP(TForm) }; //--------------------------------------------------------------------------- extern PACKAGE TFormMain *FormMain; //--------------------------------------------------------------------------- #endif
//----main.cpp------------------------------------------------------------ #include <vcl.h> #pragma hdrstop #include "main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "PERFGRAP" #pragma resource "*.dfm" TFormMain *FormMain; //--------------------------------------------------------------------------- __fastcall TFormMain::TFormMain(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- void __fastcall TFormMain::LButtonDown(TMessage &message) { //----如果鼠標在窗體內(控件外)被單擊,則此事件被激活 if(GetAsyncKeyState(VK_LBUTTON) < 0) { Application->MessageBoxA("Form Get Messsge" , "Message" , MB_OK); //----向Edit2發送鼠標消息,則Edit2將產生單擊獲得焦點的現象 SendMessage(FormMain->Edit2->Handle , message.Msg , message.WParam , message.LParam); } } //--------------------------------------------------------------------------- void __fastcall TFormMain::FormCreate(TObject *Sender) { //----替換2個TperformanceGraph控件的消息處理函數句柄 OldWndProc1 = PerformanceGraph1->WindowProc; OldWndProc2 = PerformanceGraph2->WindowProc; PerformanceGraph1->WindowProc = MyWndProc1; PerformanceGraph2->WindowProc = MyWndProc2; } //--------------------------------------------------------------------------- void __fastcall TFormMain::MyWndProc1(TMessage &message) { if (message.Msg == WM_LBUTTONDOWN) //----如果消息是“鼠標單擊消息”,則顯示信息 ShowMessage("PerformanceGraph1 Get Message "); else //----如果消息是其他消息,則交給控件原來的處理 OldWndProc1(message); } //--------------------------------------------------------------------------- void __fastcall TFormMain::MyWndProc2(TMessage &message) { if (message.Msg == WM_LBUTTONDOWN) ShowMessage("PerformanceGraph 2 get Message"); else OldWndProc2(message); } 如果在TFormMain::FormCreate()函數中加入以下兩句語句: OldWndProc3 = FormMain->WindowProc; FormMain->WindowProc = MyWndProc3; 再添加函數: void __fastcall TFormMain::MyWndProc3(TMessage &message) { if (message.Msg == WM_LBUTTONDOWN) //----如果消息是“鼠標單擊消息”,則顯示信息 ShowMessage(“FormMain Get the Message "); else //----如果消息是其他消息,則交給控件原來的處理 OldWndProc1(message); } 則把FormMain的鼠標消息響應函數進行了改裝,此時窗體就會截獲鼠標消息,而2個TperformanceGraph控件將不會得到消息。這是因為窗體將先于控件得到消息。 從上面的例子可以看出: 1、 窗體將先于控件得到消息; 2、 對于響應某一消息的控件,可以用SendMessage想起發送消息; 3、 對于不響應某一消息的控件,可以用重載其WindowProc屬性獲得你想要的效果。 實際上,掌握了利用Windows的消息傳遞機制才是掌握C++ Builder的第二階段,任重而道遠。
|