这是鼠标离开窗口的消息,一般很少用到。当有按钮处于窗口边缘时,在鼠标从边缘处离开窗口时,此按钮想捕获鼠标离开自己的消息,可以通过注册此消息,让主窗口将此消息发给自己。下面是C++Builder中的自绘按钮,并将此按钮作为关闭按钮,放置于窗口的右上角使用。代码如下:
//.h
bool m_bTrackingMouse; //鼠标是否离开标志
protected:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, MouseEnter)
VCL_MESSAGE_HANDLER(WM_MOUSELEAVE, TMessage, OnMouseLeave)
END_MESSAGE_MAP(TCustomControl)
void __fastcall MouseEnter(TMessage & msg);//鼠标进入控件
void __fastcall OnMouseLeave(TMessage & msg);//鼠标离开窗口消息
//.cpp
m_bTrackingMouse = false;//初始值为false
//鼠标进入控件表面
void __fastcall T360CloseButton::MouseEnter(TMessage & msg)
{
m_bTrackingMouse = false;//重新回到窗口时或进入按钮时,再次注册消息
m_mouse_active = 1; //鼠标在按钮上
Draw(); //内部绘图集中函数
Paint(); //自绘外部界面
}
void __fastcall T360CloseButton::OnMouseLeave(TMessage & msg)
{//移出窗口外
m_mouse_active = 2;//鼠标离开按钮
Draw();
Paint();
}
//鼠标在窗口内移动时,注册一次WM_MOUSELEAVE消息
void __fastcall T360CloseButton::MouseMove(Classes::TShiftState Shift, int X, int Y)
{
if(!m_bTrackingMouse)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;//消息类型为离开窗口
tme.hwndTrack = this->Handle;
tme.dwHoverTime = 1000;//离开窗口1000毫秒后发送消息
if(::_TrackMouseEvent(&tme))
{
m_bTrackingMouse = true;
}
}
}
|