为什么我的CStatic派生类没有收到OnMouseLeave事件但可以接收OnMouseMove

时间:2015-01-01 18:18:25

标签: c++ mfc

代码

#include "stdafx.h"
#include "TestClass.h"
IMPLEMENT_DYNAMIC(TestClass, CStatic)
TestClass::TestClass()
{

}
void TestClass::Ini(CWnd* parent)
{
    Create(L"hello world",WS_CHILD|WS_VISIBLE|SS_CENTER | SS_NOTIFY , 
    CRect(0,0,50,50), parent, 200);
}
void TestClass::OnMouseMove(UINT nFlags, CPoint point)
{
     CStatic::OnMouseMove(nFlags, point);
}
void TestClass::OnMouseLeave()
{
     CStatic::OnMouseLeave();
}
TestClass::~TestClass()
{
}


BEGIN_MESSAGE_MAP(TestClass, CStatic)
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
END_MESSAGE_MAP()  

正如你所看到我使用SS_NOTIFY并且我无法获得OnMouseLeave事件,但是OnMouseMove没有任何问题。 注:
我创建了一个自定义窗口,因此我删除了标题栏。

1 个答案:

答案 0 :(得分:0)

如果要处理鼠标离开和鼠标悬停事件,则必须在OnMouseMove函数中调用TrackMouseEvent。

void TestClass::OnMouseMove(UINT nFlags, CPoint point)
{
    TRACKMOUSEEVENT MouseBehaviour;

    CStatic::OnMouseMove(nFlags, point);

    MouseBehaviour.cbSize = sizeof(TRACKMOUSEEVENT);
    MouseBehaviour.dwFlags = TME_HOVER | TME_LEAVE;
    MouseBehaviour.hwndTrack = GetSafeHwnd();
    MouseBehaviour.dwHoverTime = HOVER_DEFAULT;

    TrackMouseEvent(&MouseBehaviour);
}