2

I have a program that is not mine that has 3 TEdit boxes and 3 TButton objects. I can easily get the button handles by using:

IntPtr buttonhwnd = FindWindowEx(mainhwnd, IntPtr.Zero, "TButton", "Button Text");

But I can't do the same with the Edit boxes since they don't have any text in them. Therefore FindWindowEx(hWnd, IntPtr.Zero, "TEdit", "") can get all of them. However, it only gets the first one it comes across and I need the last one. Is there a way to skip a number of boxes or differentiate between them?

ozdrgnaDiies
  • 1,909
  • 1
  • 19
  • 34
  • is this Borland VCL (delphi or C++ Builder) ? - anyway I think you should try to Enum all Child windows of your form, there is an api called guess what? EnumChildWindows - see this answer for some ideas... http://stackoverflow.com/a/821097/559144 – Davide Piras Dec 29 '11 at 10:20
  • The program I am trying to access is in Delphi and I am using C#. As for your link, I think the example is missing some declarations because I cannot get .NET to accept it. I went to PInvoke and tried to use their definition but it's the same thing. EnumWindow(s)Proc and EnumWindows seem to not be defined anywhere. Using a Delegate for EnumWindowsProc doesn't account for EnumWindowProc and doesn't match the parameters given by the example. But I'm most likely just doing something wrong. – ozdrgnaDiies Dec 29 '11 at 10:37
  • 1
    In general you should be using accessibility interfaces to drive UI programmatically. That said, you can see if the controls have unique IDs, in which case you can use that to distinguish them. – Raymond Chen Dec 29 '11 at 13:20
  • Please post your answer as an answer and accept it. – Werner Henze Jan 23 '12 at 10:12

1 Answers1

3

I found the answer:

Given there are 3 controls, I can use:

//Get first occuring Edit box
IntPtr edithWnd = FindWindowEx(mainhWnd, IntPtr.Zero, "TEdit", "");
//And the second
edithWnd = FindWindowEx(mainhWnd, edithWnd, "TEdit", "");
//And finally the one I want
edithWnd = FindWindowEx(mainhWnd, edithWnd, "TEdit", "");

Although not dynamic, it gets the job done for me. However, for future reference for people who might need this: Is there a way to differentiate between them besides knowing what Z position they're in?

ozdrgnaDiies
  • 1,909
  • 1
  • 19
  • 34
  • That's pretty much it. If there is no way to identify them, you just have to enumerate them all and "guess" or keep getting the next one (not as safe) until you find what you think is the correct one. – Deanna Sep 26 '12 at 15:28
  • I tried the same for SaveAsDialog, wanted to change the file path, but getting "Attempted to read or write protected memory" system violation exception – Jay Aug 18 '20 at 14:00