How to get a window from a mouse point

3 02 2009




Sometimes it is very useful to get the handle of other application windows when the mouse cursor is over it. Once you get the handle of a window you could perform different operations using it.




This could be established by using the Windows APIs GetCursorPos and WindowFromPoint. You could also analyse the classname of the window you got by another API GetClassName.




void FindWindowFromMousePoint()
   //give 1 second to move your mouse
   POINT screenpoint;
   CWnd* pFoundWindow = NULL;
   //Get the mouse point
   GetCursorPos (&screenpoint); 
   //get handle to the window under the cursor
   pFoundWindow = WindowFromPoint (screenpoint);
   char* chClassname = new char[255];
   //Get the classname from the window handle
   ::GetClassName(pFoundWindow->m_hWnd, chClassname, 255);
   CString strClassname = chClassname;
   if( 0 == strClassname.CompareNoCase(_T("SysListView32")) )
      AfxMessageBox(_T("The mouse is over the desktop"));
      AfxMessageBox(_T("The mouse is over %s"),chClassname);




The WindowFromPoint function doesn’t retrieve a handle to a hidden or disabled window.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: