Description
This example will display a message when the mouse leaves a control or
component.
To accomplish this we will detect the event "mouse moving inside the
control/component" and then, the event "mouse moving on the form that contains
that control/component (which is the same as mouse moving outside the component)".
(The Form is the Parent of the component) This example shows the method for 6
different types of components.
MouseLeavesControl.cpp
#include
#pragma hdrstop
#include "MouseLeavesControl.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
enum
{
MEMO1 = 1,
BUTTON1,
LISTBOX1,
SCROLLBOX1,
IMAGE1,
STATICTEXT1,
NUMBER_OF_CONTROLS = STATICTEXT1
};
bool Form1_mouse_moved = false;
int Component_Mouse_Move[10];
//------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
for ( int i=1; i<=NUMBER_OF_CONTROLS; i++ )
{
Component_Mouse_Move[i] = 0;
}
}
//------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
Form1_mouse_moved = true;
for ( int i=1; i<=NUMBER_OF_CONTROLS; i++ )
{
switch ( Component_Mouse_Move[i] )
{
case 0: //Mouse was not inside a component
break;
case 1: //Mouse was inside a component
ShowMessage("Leaving component "
+ String(i) );
break;
default: ShowMessage("Illegal Component Index");
}
}
Component_Mouse_Move[MEMO1] = 0;
Component_Mouse_Move[BUTTON1] = 0;
Component_Mouse_Move[LISTBOX1] = 0;
Component_Mouse_Move[SCROLLBOX1] = 0;
Component_Mouse_Move[IMAGE1] = 0;
Component_Mouse_Move[STATICTEXT1] = 0;
}
//-----------------------------------------------------------------------
void __fastcall TForm1::Memo1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
//
Component_Mouse_Move[MEMO1] = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
Component_Mouse_Move[BUTTON1] = 1;
}
//-----------------------------------------------------------------------
void __fastcall TForm1::ListBox1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
//
Component_Mouse_Move[LISTBOX1] = 1;
}
//-----------------------------------------------------------------------
void __fastcall TForm1::Button1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
//
Component_Mouse_Move[BUTTON1] = 1;
}
//------------------------------------------------------------------------
void __fastcall TForm1::ScrollBox1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
//
Component_Mouse_Move[SCROLLBOX1] = 1;
}
//------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
//
Component_Mouse_Move[IMAGE1] = 1;
}
//------------------------------------------------------------------------
void __fastcall TForm1::StaticText1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
//
Component_Mouse_Move[STATICTEXT1] = 1;
}
Homepage
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.