Description
This example shows how to detect the following two events:
1- when the Mouse enters a control
2- when the Mouse leaves a control
First, we create the ControlName list with the names
of the classes of controls we are going to use
to trigger the above events.
Then, we override the TForm's WndProc() procedure with our Form1's
WndProc() function, so that we can intercept both the CM_MOUSEENTER and
the CM_MOUSELEAVE Windows messages.
If any of these two messages are received, then we process them by
displaying a message on a TLabel placed on Form1.
If any other Windows message is received, then, we call the
inherited WndProc() function
TForm::WndProc( Message );
which belongs to the TForm class,
to process it. TForm is the Parent class of our TForm1 class.
Also, notice that the TForm1's WndProc() was placed as a public
member function of TForm1 class.
...
public:
void __fastcall WndProc( Messages::TMessage &Message );
...
To run this project all you need to do is to
drop a TLabel, a TScrollBox, a TRichEdit, and a TImage
control on an empty Form.
Files
WndProcMouseEnterLeave.cpp
#include <vcl.h>
#pragma hdrstop
#include "WndProcMouseEnterLeave.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// I want to detect the events when the Mouse enters or leaves
// any of the controls types contained in the following list
String ControlName[10] = {"TScrollBox", "TRichEdit", "TImage"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = (TColor)0x654efd;
}
void Display(Messages::TMessage &Message, TControl* pcontrol,
String ClassName, TLabel* label)
{
if (pcontrol->ClassNameIs(ClassName))
{
if (Message.Msg == CM_MOUSEENTER)
{
label->Caption = ClassName + ": CM_MOUSEENTER Message";
}
else
{
label->Caption = ClassName + ": CM_MOUSELEAVE Message";
}
}
}
void __fastcall TForm1::WndProc( Messages::TMessage &Message )
{
//Detect when the mouse enters or leaves a given component
//
if( (Message.Msg == CM_MOUSEENTER) || (Message.Msg == CM_MOUSELEAVE) )
{
for( int i=0; i < this->ControlCount; i++ )
{
// Convert an integral type: Message.LParam into a pointer
TControl* pControl = reinterpret_cast<TControl*>(Message.LParam);
if (pControl)
for (int i=0; i < this->ControlCount; i++)
Display(Message, pControl, ControlName[i], Label1);
}
}
else
{
// Parent class' WndProc() or inherited WndProc()
TForm::WndProc( Message );
}
}
//-----------------------------------------------------------------------
WndProcMouseEnterLeave.h
#ifndef WndProcMouseEnterLeaveH
#define WndProcMouseEnterLeaveH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TScrollBox *ScrollBox1;
TRichEdit *RichEdit1;
TImage *Image1;
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall WndProc( Messages::TMessage &Message );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.