How to make a window handle picker ?

Posted on 2000-02-16
Last Modified: 2013-11-20
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.

P.S. : Sample code will be very nice.
Question by:mike_marquet
  • 4
  • 3

Accepted Solution

ScottyDawg earned 50 total points
ID: 2526363
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 :)

Author Comment

ID: 2526665
Thanks. But did you have a sample ?

No need of windows hooks ?

Author Comment

ID: 2527091
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;


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

  m_bLeftMouseButtonDown = FALSE;


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, "");
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Expert Comment

ID: 2527147
What exactly isn't working?

Author Comment

ID: 2527262
Sorry, I have found.

Forgotten to call ClientToScreen before calling WindowsFromPoint.

Expert Comment

ID: 2527272
ah good. Also you if the window is hidden or diabled you will need to use ChildWindowFromPoint().

Author Comment

ID: 2527615
Ok, thanks

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 60
VB.NET how to use the Vertical ScrollBar 5 103
Complete beginner needs help making a cron job 9 116
has22 challenge 11 93
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question