Click here to Skip to main content
11,923,958 members (60,502 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked

Windows handles from a mouseclick

, 9 Sep 2003
Rate this:
Please Sign up or sign in to vote.
How to get a window handle from a set of co-ordinates
<!-- Article image -->

Sample Image - hwndfrommouseclick.jpg

<!-- Add the rest of your HTML here -->


This is just a simple demonstration of how to obtain a window handle simply by clicking the window. It uses straight C, and should compile under any compiler that will compile windows GUI programs. The included makefile is designed for Microsoft nmake.exe.

How does it work?

For this method to work you need to create a window, so that you can process the WM_LBUTTONDOWN. In my application I created the "Get Window" button to start the capture, and it is released after the first click, so you need to click the "Get Window" button each time.

Firstly I captured the mouse, using:

HWND SetCapture(HWND hWnd);
so I could get WM_LBUTTONDOWN messages when I clicked on other windows. Then when my window is sent WM_LBUTTONDOWN messages. Firstly I tried using the WM_LBUTTONDOWN x and y positions (HI and LOWORDs of lParam), but after a few odd results, I realized the co-ordinates from the WM_LBUTTONDOWN were relative to my applications window. After a little digging in the Win32 SDK docs I found a function that does what I need.
DWORD GetMessagePos(void);

This returns the screen position of the cursor encoded in a DWORD, in the same way as lParam in the WM_LBUTTONDOWN message. The x co-ordinate is in the LOWORD, and y co-ordinate in the HIWORD, then translate this to a POINT structure which is defined as:

typedef struct tagPOINT { // pt  
    LONG x; 
    LONG y; 
Then I use:
HWND WindowFromPoint(POINT Point);
which will return the uppermost window under the mouse, i.e. the window you can see under the mouse, or NULL, if no window is found. I then retrieve the window title using:
int GetWindowText(HWND hWnd, LPTSTR lpString, int nMaxCount);
And that's the bare bones of it. The use of g_bCaptureSet is simply to stop me from setting the mouse capture more than once and also so that I only process WM_LBUTTONDOWN messages when I was set up to get window handles. <!------------------------------- That's it! --------------------------->


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
G Haranadh8-Feb-11 21:45
memberG Haranadh8-Feb-11 21:45 
Questionhow to do mouse over action? Pin
keithsw14-Oct-04 23:45
memberkeithsw14-Oct-04 23:45 
Questionhow to get the handles for controls Pin
manyvan14-Oct-03 23:01
membermanyvan14-Oct-03 23:01 
GeneralRe: Useless? Pin
Claudius Mokler10-Sep-03 2:43
memberClaudius Mokler10-Sep-03 2:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 10 Sep 2003
Article Copyright 2003 by recoup_this
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid