Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey all,

 

I'm trying to use FindWindowEx() to get a handle to the Calculator buttons.

 

I'm using this code to drill down through the levels, which works fine until I get to the button level:

 

hwndFrame = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "CalcFrame", "");

hwndDialog = FindWindowEx(hwndFrame, IntPtr.Zero, "#32770", "");

hwndButton = FindWindowEx(hwndDialog, IntPtr.Zero, "Button", "2");

 

The top two lines populate the handles just fine, but the third does not.

 

Anyone got a clue as to why it's returning nothing for the button handle? I'm using Window Detective to obtain the UI information and it lists "Button" as the class type, however it doesn't list anything for WindowTitle. I've tried it with "" and "2". I've seen "2" in a supposedly working example on the net.

 

My declaration is:

 

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

 

Help, thanks.

 

Jib.

  • 2 weeks later...
  • Leaders
Posted

First of all, I have a feeling that there may be differences between Calc.exe on different versions of Windows. This might be an issue. This is what I did to get the handle to a particular button.

            w = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "SciCalc", "Calculator");
           w = FindWindowEx(w, IntPtr.Zero, null, "tan");

Looking at Calc.exe on my machine with Spy++, the heirarchy of windows doesn't even resemble what you've posted.

 

The top-level window is "Calculator" whose class is "SciCalc", which directly contains all of the buttons, whose classes are all "Button". I know I've looked at this program with Spy++ a very long time ago (I think when I was running Windows ME) and there were way more windows involved, and everything had "Static" for the class.

[sIGPIC]e[/sIGPIC]
Posted

Hmm, perhaps Calc isn't the best starter project. I'm using Windows Detective and it's results are somewhat irregular. Sometimes the buttons appear with WindowText such as "2", other times they contain nothing.

 

I'm automating an application and need to reproduce mouse clicks and text entries. I can do this by taking a screen dump and mapping out the button positions dependant on a pre-known pixel set. I thought the window-handle approach might be a little more robust though..?

  • Leaders
Posted

It would be more robust if you could ensure that you were always dealing with the same version of the program. Even though the appearance of Calc.exe hasn't changed much over the years, the changes that happen under the hood are significant, and it is updated with each version of Windows.

 

Now, if you try to use predetermined corrdinates of the buttons, what happens if the user has large fonts enabled. Many applications scale themselves with the font size. (Whether or not this actually affects calc, I don't know).

[sIGPIC]e[/sIGPIC]
Posted

Well to be honest the actual target application is likely to change a lot more than Calc, so it will be a life's work once I get to that point. I'll be running it on my computer to start with, so there will be less variables, however I hope to run it elsewhere eventually.

 

I just want to take a robust approach that requires as few alterations to the code as possible really.

  • 4 months later...
Posted (edited)

Hi everyone,

 

I understand this topic is little old, but I just saw it and would like to submit a solution.

 

The mistake comes from here:

hwndButton = FindWindowEx(hwndDialog, IntPtr.Zero, "Button", "2");

 

In the calc version of WINDOWS 7 is different.

These are more than 1 control with class "#32770", which is the problem. This code selects the first one, and this is wrong, as the buttons are in the second.

 

A possible solution would be:

 

IntPtr hwnd = (IntPtr)FindWindow(null, "Calculator");

IntPtr hwndFrame = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "CalcFrame", string.Empty);

IntPtr hwndDialog = FindWindowEx(hwndFrame, IntPtr.Zero, "#32770", string.Empty);

IntPtr hwndDialog2 = FindWindowEx(hwndFrame, hwndDialog, "#32770", string.Empty);

IntPtr hwndButton = FindWindowEx(hwndDialog2, IntPtr.Zero, "Button", "5");

 

Cheers,

B&W

Edited by blackandwhitebg
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...