How to get a window from a mouse point

3 02 2009

description

 

 

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.

howcanidoit

 

 

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.

mycodesnippet

 

 

void FindWindowFromMousePoint()
{
   //give 1 second to move your mouse
   Sleep(1000);
  
   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"));
   }
   else
   {
      AfxMessageBox(_T("The mouse is over %s"),chClassname);
   }
}

mynote

 

 

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

Advertisements

Actions

Information

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s




%d bloggers like this: