Click here to Skip to main content
15,888,733 members
Articles / Programming Languages / C++
Article

Windows handles from a mouseclick

Rate me:
Please Sign up or sign in to vote.
3.39/5 (16 votes)
9 Sep 20032 min read 65.7K   3.3K   29   4
How to get a window handle from a set of co-ordinates

Sample Image - hwndfrommouseclick.jpg

Introduction

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; 
} POINT; 
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.

License

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


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
G Haranadh8-Feb-11 20:45
G Haranadh8-Feb-11 20:45 
Questionhow to do mouse over action? Pin
keithsw14-Oct-04 22:45
keithsw14-Oct-04 22:45 
Questionhow to get the handles for controls Pin
manyvan14-Oct-03 22:01
manyvan14-Oct-03 22:01 
GeneralRe: Useless? Pin
Claudius Mokler10-Sep-03 1:43
Claudius Mokler10-Sep-03 1: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.