Couldn't get HWND of button inside a tab dialog

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 546 Views - Last Post: 02 December 2019 - 09:01 PM Rate Topic: -----

#1 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Couldn't get HWND of button inside a tab dialog

Posted 29 November 2019 - 02:34 AM

I am developing an simple application like Spy++ to get window handle 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.

I drag the icon of Find Window in Spy++, it can detect MainWindow, SysTabControl32 and Tab1 but it cannot detect Btn1. I can get all the child windows' hwnd by using EnumChildWindows but the ...WindowFromPoint function still cannot detect 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.

It seems like the SysTabControl32 is blocking the ...WindowFromPoint function to reach those child windows which are inside Tab1.

Does anyone know what is the cause of this situation and how to solve it? Please help..Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Couldn't get HWND of button inside a tab dialog

#2 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 29 November 2019 - 02:39 AM

Below is the code I have tried:
#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;

}

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 29 November 2019 - 07:03 AM

What application is showing this unusual heirarchchy? Do you see the same behavior with other apps?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 29 November 2019 - 07:52 PM

Now that I'm not just skimming through stuff on my phone, and I've had some chance to think about this some more, I think I see the issue now. I think that you are under the misconception that controls within the tab display area has to be a child of the tab control. Although this maybe true in the case of the .NET Framework WinForms tab control, as well as the ASP.NET and jQuery tab controls, the same is not true for native Win32 API tab controls. It is up to the developer how they want to setup their hierarchy because all the Win32 Tab Common Control does is manage the tabs, the text and icons on the tabs, as well as any border that maybe associated with the display area of the tab control. What actually gets displayed within area is actually controlled by the code hosting the tab control. All the tab control does is notify the host that a new tab has been chosen. It is up to the host to determine how to respond to this notification.

See How to Create a Tabbed Dialog Box
Was This Post Helpful? 0
  • +
  • -

#5 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 30 November 2019 - 06:36 AM

Hi Skydiver,

The application is a software developed by another team that we cooperate together and due to some issues, i'm trying to fix on my part but not the application that developed by them.

In the application, there are others tab control don't have this issue. That's why I have no idea why the tab control with classname 'SysTabControl32' can be in front and 'block' those tab dialog (eg. tab1, tab2, ..) and the button/text inside tab dialog to be detected. If my understanding is correct, the 'SysTabControl32' is just a control to let the user can switch between the tab pages but the button/text controls are parented to the tab dialog that we created.

Any advise for this issues? Or the only way is do some changes for the tab control in the application?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 30 November 2019 - 07:23 AM

Since your target is a known target, unlike the unknown targets the Spy++ had to deal with, you could just get the windows you are interested in by ID instead of trying to get the window by position on screen.

Of course that assumes that the author of that dialog is not evil and randomizes their window control IDs just for the heck of it to thwart your team from trying to mess with their dialog... :-)
Was This Post Helpful? 0
  • +
  • -

#7 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 01 December 2019 - 06:21 PM

My application need to have the behavior same as Spy++, means I have to assume that I don't know all the information of those controls such as hWnd, CtrlID, ClassName, WindowCaption and so on. To have this behavior, these parameter should not be constant/fixed.

Any idea to for this issue? Or it doesn't have a better solution anymore??
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 01 December 2019 - 09:23 PM

Well, given that Spy++ itself can't find the window when you drag the search icon over the button, then it sounds like your current code is already at parity with Spy++. From what I recall, Spy++ also uses RealChildWindowFromPoint() to determine what window you are hovering over.

If I were stuck in your shoes, my first approach would just depend on searching for child windows by ID and tell the other team not to change control IDs since they already screwed you over with their odd window hierarchy. If you can't depend on them to lock IDs, then it's going to be time to do your own hit testing. Enumerate all windows and checking each of their visible states and client rectangles. (Pray that the other team doesn't start using client regions instead of client rectangles.)
Was This Post Helpful? 0
  • +
  • -

#9 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 01:10 AM

Your idea is enumerate all the child windows of MainWindow, get all the client rectangles (rect) and compare with the mouse cursor position? If yes, this idea I have tried before.

The steps are: use EnumChildWindows to enumerate all the child windows of MainWindow, get all RECT of the child windows, use PtInRect to check whether the cursor point is inside the rectangle of control(eg. button).

But I don't know how to get all the client rectangle of child windows and how to filter them to get what control that I pointed by the mouse cursor. Also, I found that even I know all the information about the child windows, in the end I still get the tab control window (SysTabControl32) because the rectangle of SysTabControl32 is bigger than all those child windows which are 'behind' of it. So, the result might still the same.

Is it the analysis is correct? I'm not sure because I failed to develop the code to get all client rectangle and filter the child windows.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 02:22 PM

I must be missing something. Why can't you very the window with the smallest rectangle that contains the point you after interested in?
Was This Post Helpful? 0
  • +
  • -

#11 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 06:14 PM

I also thinking about that. Since I don't know how to get all the rectangle of those child window, I can't proceed to the that stage.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 06:31 PM

Wait... so you know how to enumerate and find windows, but you don't even know how to get the window rectangle (GetWindowRect())? How did you learn to do Windows programming, and what have you been doing with the Win32 API? I find this very, very, very unusual.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 07:19 PM

Looks like you also crossposted in StackOverflow.

I would have really appreciated it if you had not lied to me when I was asking about the application that you were trying to find the button on. You said in post #5 that you were working with the other team. In your SO post you say that it is actually a 3rd party application.

Can you at least tell us what that 3rd party application is, and why do you need to find the buttons on that app?
Was This Post Helpful? 0
  • +
  • -

#14 Josh_0101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-November 19

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 08:20 PM

Sorry I might misunderstand what 3rd party application stand for. The application is a software for machine that we developed ourselves. I'm developing the add-on tools for it, an automated software testing apps, so to make everything automated, I need the window handle to do the job.

I have asked the same question in some active forum but those suggestion still can't fix my issue.

I'm beginner in window programming so I'm not sure how to implement
GetWindowRect
together with
CALLBACK
of
EnumWindowsProc
after use the
EnumChildWindow
in
main()
.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Couldn't get HWND of button inside a tab dialog

Posted 02 December 2019 - 08:45 PM

Since you contracted out the application that runs on your hardware, then you can have them write their app so that they either fix the IDs for their controls, or that they actually fix the hierarchy of their window controls so that you can find them.

As a quick aside, if you are planning on localizing the application for various languages, the first step is fixing the IDs of the controls to well known values that do not change. Why is that? It is because the most localizing firms will take a pass through the application pulling out the IDs of the various controls and put them into a database with the associated strings/images and their sizes and positions. Then for each target language, they will make another copy of the database with the strings/images created appropriate for the new language, and if necessary resize or move the controls. These new values are then re-introduced into the application as just a resource only build.

Instead of rolling your own testing application, I highly recommend getting a testing suite off the shelf. Something like Winium.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2