Click here to Skip to main content
15,745,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
#include<windows.h>
#include<iostream>
using namespace std;

int main() {

		POINT pt;
		Sleep(3000);
		GetCursorPos(&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));
		GetWindowTextA(hWndChild,title,sizeof(title));
		cout <<"Window name : "<<title<<endl;
		cout <<"Class name  : "<<class_name<<endl;
		cout <<"hwnd        : "<<hWndChild<<endl<<endl;

		system("PAUSE");
		return 0;

}
Posted
Updated 26-Nov-19 22:20pm
Comments
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?

1 solution

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900