截获WM_ACTIVATE这个消息试试 MSDN: lParam Handle to the window being activated or deactivated, depending on the value of the wParam parameter. If the low-order word of wParam is WA_INACTIVE, lParam is the handle to the window being activated. If the low-order word of wParam is WA_ACTIVE or WA_CLICKACTIVE, lParam is the handle to the window being deactivated. This handle can be NULL.
MSDN:
lParam
Handle to the window being activated or deactivated, depending on the value of the wParam parameter. If the low-order word of wParam is WA_INACTIVE, lParam is the handle to the window being activated. If the low-order word of wParam is WA_ACTIVE or WA_CLICKACTIVE, lParam is the handle to the window being deactivated. This handle can be NULL.
HWND PreWnd;LRESULT CDemoDlg::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_IsInWindow=false;
//Add your code
trace("leave");
return 0;
} void CDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{ if (!m_IsInWindow)
{
m_IsInWindow = TRUE;
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&trackmouseevent); //handle move in event
PreWnd=::GetForegroundWindow();
}
CDialog::OnMouseMove(nFlags, point);
}
我来试下这个方法,全局钩子不到真没办法的时候还是没想到要用。刚看到有这个一个消息WM_ACTIVATEAPP,似乎可以,但还没有详细研究
第一个应该是自己
第二个不就是上一个拥有焦点的窗口么
你们是怎么试的?
不行啊!刚刚试了一下,这个消息是在不同程序之间切换时就收到的,但激活自己窗口时收不到前一个窗口的信息,线程ID为NULL
枚举后的窗口表是基本不改变顺序的。
比如
我激活A窗口,再激活自己的窗口,再调用枚举,窗口列表是:: 自己窗口--A窗口--B窗口--C窗口
然后再激活B窗口,再激活自己的窗口,再调用枚举,窗口列表依旧是 ::自己窗口--A窗口--B窗口--C窗口
CWnd* pOldWnd
);
这个pOldWnd就是失去焦点(从激活状态变为未激活)的那个窗口的句柄了. 你在b程序中添加WM_SETFOCUS消息响应函数, 就可以得到pOldWnd了
这个我也作了测试,线程ID是有空为NULL
这个需要你记录激活窗口的历史记录,使用鼠标肯定不对,应为tab也是可以切换窗口的。
Enumwindow也不对。如果是本进程的窗口,还好办,如果是其他进程的窗口,难!不知道楼主想干什么!