How to make a window handle picker ?

How must I proceed to make a window handle picker like the window handle picker in Spy++ (CTRL-F). The window were you can drag a symbole to the window you want to get the handle from.

Thanks for answer.
Mike

P.S. : Sample code will be very nice.
mike_marquetAsked:
Who is Participating?
 
ScottyDawgConnect With a Mentor Commented:
Use the function WindowFromPoint(...) which given a CPoint object will return the CWnd* at that position - but remember the returned CWnd* may be tempory so call GetSafeHwnd() on it.

Also, because you want to recieve mouse input from outside of your own window (during the drag bit), use the SetCapture() and ReleaseCapture() functions. Hope this helps :)
0
 
mike_marquetAuthor Commented:
Thanks. But did you have a sample ?

No need of windows hooks ?
0
 
mike_marquetAuthor Commented:
I have try it and the Capture works but when I call WindowFromPoint(...)
in my mouse move message, it doesn't work fine.
The handle received is not correct.

Here's some sample (CWindowHandlePickerDlg is derived from CDialog) :

void CWindowHandlePickerDlg::OnLButtonDown(UINT nFlags, CPoint point)
 {
  CDialog::OnLButtonDown(nFlags, point);

  m_bLeftMouseButtonDown = TRUE;

  SetCapture();
 }

void CWindowHandlePickerDlg::OnLButtonUp(UINT nFlags, CPoint point)
 {
  CDialog::OnLButtonUp(nFlags, point);

  m_bLeftMouseButtonDown = FALSE;

  ReleaseCapture();
 }

void CWindowHandlePickerDlg::OnMouseMove(UINT nFlags, CPoint point)
 {
  CDialog::OnMouseMove(nFlags, point);

  if (!m_bLeftMouseButtonDown) return;

  CWnd *pWnd = WindowFromPoint(point);

  if (pWnd)
   {
    CString  str;

    str.Format("HEX : %X", pWnd->GetSafeHwnd());
   
    SetDlgItemText(IDST_WINDOWHANDLE, str);
   }
  else SetDlgItemText(IDST_WINDOWHANDLE, "");
 }
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ScottyDawgCommented:
What exactly isn't working?
0
 
mike_marquetAuthor Commented:
Sorry, I have found.

Forgotten to call ClientToScreen before calling WindowsFromPoint.
0
 
ScottyDawgCommented:
ah good. Also you if the window is hidden or diabled you will need to use ChildWindowFromPoint().
0
 
mike_marquetAuthor Commented:
Ok, thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.