I am trying to make a dll to detect whether the mouse is over the window. I have researched, and found out a few things, but my c++ isnt too good. please help me translate this code from msdn.
#if(_WIN32_WINNT < 0x0400)
#define WM_MOUSELEAVE WM_USER+2
#define TME_LEAVE 1
typedef struct tagTRACKMOUSEEVENT {
DWORD cbSize;
DWORD dwFlags;
HWND hwndTrack;
} TRACKMOUSEEVENT, *LPTRACKMOUSEEVENT;
VOID CALLBACK
TrackMouseTimerProc(HWND hWnd,UINT uMsg,UINT idEvent,DWORD dwTime) {
RECT rect;
POINT pt;
GetClientRect(hWnd,&rect);
MapWindowPoints(hWnd,NULL,(LPPOINT)&rect,2);
GetCursorPos(&pt);
if (!PtInRect(&rect,pt) || (WindowFromPoint(pt) != hWnd)) {
if (!KillTimer(hWnd,idEvent)) {
// Error killing the timer!
}
PostMessage(hWnd,WM_MOUSELEAVE,0,0);
}
}
BOOL
TrackMouseEvent(LPTRACKMOUSEEVENT ptme) {
OutputDebugString(TEXT("TrackMouseEvent\n"));
if (!ptme || ptme->cbSize < sizeof(TRACKMOUSEEVENT)) {
OutputDebugString(TEXT("TrackMouseEvent: invalid "
"TRACKMOUSEEVENT structure\n"));
return FALSE;
}
if (!IsWindow(ptme->hwndTrack)) {
OutputDebugString(
TEXT("TrackMouseEvent: invalid hwndTrack\n"));
return FALSE;
}
if (!(ptme->dwFlags & TME_LEAVE)) {
OutputDebugString(TEXT("TrackMouseEvent: invalid dwFlags\n"));
return FALSE;
}
return SetTimer(ptme->hwndTrack, ptme->dwFlags,
100,(TIMERPROC)TrackMouseTimerProc);
}
#endif
LRESULT CALLBACK
MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
TRACKMOUSEEVENT tme;
static BOOL fInWindow;
static BOOL fInMenu;
switch (uMsg) {
case WM_CREATE:
fInWindow = FALSE;
fInMenu = FALSE;
return 0;
case WM_MOUSEMOVE:
if (!fInWindow) {
fInWindow = TRUE;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
if (!TrackMouseEvent(&tme)) {
MessageBox(hWnd,
TEXT("TrackMouseEvent Failed"),
TEXT("Mouse Leave"),MB_OK);
}
}
break;
case WM_MOUSELEAVE:
fInWindow = FALSE;
if (!fInMenu)
MessageBox(hWnd,TEXT("Elvis has left the building"),
TEXT("Mouse Leave"),MB_OK);
break;
case WM_ENTERMENULOOP:
fInMenu = TRUE;
break;
case WM_EXITMENULOOP:
fInMenu = FALSE;
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return FALSE;
}
reference page for trackmouseevent:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputstructures/trackmouseevent.asp