I want to hook another program's window, so I can get when the cursor enters the window. How开发者_如何学编程 to do that? I know that it's with SetWindowsHookEx but I can't find an example (even a native one). I'm doing this in C#2.0, but I can use a native dll if there is a way. Hooking the WM_PAINT or some looping method will also do the job.
You probably want to set a hook on WM_NCHITTEST which is sent whenever the mouse moves over a window. You simply need to get the window handle then call SetWindowsHookEx with WH_CALLWNDPROC. Your CallWndProc hook should look something like this:
LRESULT CALLBACK CallWndProc( int nCode, WPARAM wParam, LPARAM lParam ) {
if( nCode < 0 ) {
return CallNextHookEx( NULL, nCode, wParam, lParam );
}
CWPSTRUCT* pCWP = ( CWPSTRUCT* )lParam;
switch( pCWP -> message ) {
case WM_NCHITTEST: {
...
return CallNextHookEx( NULL, nCode, wParam, lParam );
}
default:
return CallNextHookEx( NULL, nCode, wParam, lParam );
}
}
You could also try SetWindowsHookEx with WH_MOUSE.
There is no need to use SetWindowsHookEx. You can hook the window by subclassing it.
If you wanted to install this hook for every window, then you might want to reconsider this method, but all the same, EnumWindows would still work fine.
精彩评论