如何捕获窗体下面的控件的一些消息?
比如我现在要捕获窗体下面的一个button的一些消息,我在
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1->WindowProc = buttonWndProc;
}
void __fastcall TForm1::buttonWndProc(Messages::TMessage &Message)
{
//这里进行处理
............
TForm::WndProc(Message);
}
当时这样最终不行,请问如何捕获对该button的一些操作?
问题点数:50、回复次数:4Top
1 楼onestation(新手)回复于 2005-06-01 13:47:41 得分 0
你把TButton的消息全部传给了TForm处理当然不行啦!
Top
2 楼yanhawk(yanhawk)回复于 2005-06-01 15:11:21 得分 0
确实是这个问题,但是如果 TForm::WndProc(Message);改为 TButton::WndProc(Message);很显然又会报错。Top
3 楼onestation(新手)回复于 2005-06-02 15:44:20 得分 10
你可以自己继承一个自己的TButton,再重载WndProc函数,在主窗口中动态创建自己定义的Button
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <StdCtrls.hpp>
//---------------------------------------------------------------------------
class TMyButton : public TButton
{
private: // User declarations
public: // User declarations
void __fastcall WndProc(Messages::TMessage &Message);
__fastcall TMyButton(TComponent* Owner);
__fastcall ~TMyButton(void);
};
#endif
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit2.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TMyButton::TMyButton(TComponent* Owner): TButton(Owner)
{}
__fastcall TMyButton::~TMyButton(void)
{
}
void __fastcall TMyButton::WndProc(Messages::TMessage &Message)
{
if( Application->MainForm )
{
TCanvas * pCanvas = Application->MainForm->Canvas;
pCanvas->TextOutA(100,100,IntToStr(Message.Msg));
}
TButtonControl::WndProc(Message);
}
//------------------------------------------------------------------------------
//主窗口
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
if( Button1 ) delete Button1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button1 = new TMyButton(NULL);
Button1->Parent = Form1;
Button1->Show();
}
//---------------------------------------------------------------------------
Top
4 楼COKING(天晴)回复于 2005-06-02 15:53:31 得分 40
OLDWndProc= Button1->WindowProc;
Button1->WindowProc = buttonWndProc;
void __fastcall TForm1::buttonWndProc(Messages::TMessage &Message)
{
//这里进行处理
............
OLDWndProc(Message);
}Top




