捕获vcl控件的离开消息

class TEdit1:public TEdit{
public:
    __fastcall TEdit1(TComponent* Owner):TEdit(Owner){
    }

    void __fastcall WndProc(TMessage& msg){
        if( msg.Msg == WM_LBUTTONDOWN ){
            Form1->Memo1->Lines->Add("WM_LBUTTONDOWN");
            Application->OnMessage = PreProcessMsg;
        }
        TEdit::WndProc(msg);
    }

    void __fastcall PreProcessMsg(tagMSG &Msg, bool &Handled){
    switch(Msg.message ){
        case WM_LBUTTONDOWN:{
            if( Msg.hwnd == Handle )
                return;
            else{
                Form1->Memo1->Lines->Add("Leave");
                //to something
                Application->OnMessage = NULL;
            }
            return;
        }
    }
}
}; 

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    TEdit1* edit = new TEdit1(this);
    edit->Parent = this;
}

 

//说明:

     程序逻辑:

         (1)当单击进入Edit控件时,设置Application的OnMessage函数指针,使TEdit控件能够处理任何该应用程序的消息

       Application->OnMessage = PreProcessMsg; 

       (2)在Edit控件的PreProcessMsg方法中检测WM_LBUTTONDOWN消息,若消息的发送者为自己,则不作处理,否则作离开处理

 

你可能感兴趣的:(捕获vcl控件的离开消息)