Click here to Skip to main content
14,452,520 members
Rate this:
Please Sign up or sign in to vote.
I am working on getting hwnd by using the mouse cursor pointing on button. The problem is I cannot reach the button which is inside a tab dialog.

I used Spy++ to get the hierarchy, there are 3 layers to reach the desired button. MainWindow (#32770) - Tab1 (#32770) - Btn1 (Button). It shows that Tab1 is not parented to SysTabControl32 and have the same parent window with SysTabControl32.

Instead of getting the Btn1, I always get SysTabControl32, which has the same hierarchy level with Tab1, means that I only can reach the SysTabControl32 but not the Tab1.

If I used WindowFromPoint and point my mouse on Btn1, I will get SysTabControl32; using ChildWindowFromPoint, I will get 0 as return value; using RealChildWindowFromPoint, I will get SysTabControl32.

Spy++ only can detect MainWindow, SysTabControl32 and Tab1 but it cannot detect Btn1. I can get all the child windows by using EnumChildWindow but the ...WindowFromPoint function still cannot find Tab1 and those child windows. Also, Spy++ is able to highlight Btn1 after I "Highlight" it but if I use 'Find Window' function in Spy++, Btn1 cannot be detected.

Is it the reason is the overlapping of sibling windows? It seems like SysTabControl32 is keep blocking the ...WindowFromPoint function to get the child windows. Does anyone know what is the cause of this situation and how to solve it? Please help..Thanks

What I have tried:

using namespace std;

int main() {

		POINT pt;

		HWND hWnd = WindowFromPoint(pt);
		HWND hWndParent = GetParent(hWnd);
		MapWindowPoints(NULL, hWndParent, &pt, 1);
		HWND hWndChild = RealChildWindowFromPoint(hWndParent,pt);

		char class_name[100];
		char title[100];
		GetClassNameA(hWndChild,class_name, sizeof(class_name));
		cout <<"Window name : "<<title<<endl;
		cout <<"Class name  : "<<class_name<<endl;
		cout <<"hwnd        : "<<hWndChild<<endl<<endl;

		return 0;

Updated 26-Nov-19 23:20pm
Shao Voon Wong 27-Nov-19 4:03am
From your code snippet, it looks like not MFC code. Please post the actual problematic code because WinAPI application starts in WinMain() not main().
Josh_0101 27-Nov-19 4:11am
I make it as a simple C++ coding and test it in console. It is a small part of MFC application that I'm debugging.
Shao Voon Wong 27-Nov-19 4:10am
Are you trying to get HWnd of other applications not written by you? Even you got their HWnd, you cannot SendMessage() to them because on modern Windows OSes, MS disallows cross-process communication through sending WinAPI message due to security concerns.
Josh_0101 27-Nov-19 4:26am
I can get some of the hwnd from the application but I found that some control inside the tab dialog cannot be get. Those explanation are based on my understanding from Spy++. Perhaps to get some idea from here what is the root cause and is there any solution for it.
Shao Voon Wong 27-Nov-19 4:31am
Why are you trying to get HWNDs from Spy++? What are you going to do with the HWNDs?
Josh_0101 27-Nov-19 4:45am
I use Spy++ to check the hierarchy of the window for me to decide which window function should be used. The purpose is to get the correct window caption when the mouse cursor is pointing on it.
Shao Voon Wong 27-Nov-19 5:13am
You are just trying to steal sensitive information like Bitcoin wallet, password, serial number or credit card numbers. If even Spy++ cannot see the HWND, you can forget about getting the HWND because the control may not even implemented using HWND.
Josh_0101 27-Nov-19 5:20am
It seems like you misunderstand what I'm asking. I can get all the HWND by referring to the hierarchy shown in Spy++. Those HWND are not hidden and I can get all the info, just the WindowFromPoint function couldn't reach them. Sorry to say that, I'm a beginner in this and it will be a long journey for me if I want to use this knowledge to steal sensitive information. Thanks for the suggestion but it's not applicable.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

MFC CButton inherits from CWnd base class. CWnd and its derived class has this GetSafeHwnd() to return a HWND. If you have the CButton object, call its GetSafeHwnd() function. If you want to capture a mouse input, subclass CButton class with your own class to handle WM_MOUSEMOVE message or other mouse messages yourself. Below is an useful subclassing article by Chris Maunder, the founder of CodeProject, which you can refer on how to do it. The article is 18 years old but not outdated: I still refer to it every year when writing MFC code.

Create your own controls - the art of subclassing[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100