0

I trying to select a window in system process but it returns nothing.

Here is the window I'm trying to get:

enter image description here

What's wrong with the code below?

Sub Find_Window

    Dim Profit As Integer = Win32.FindWindow(Nothing, "ProfitPro - 5.0.0.35 - Registrado"
    Dim Menu As Integer = Win32.FindWindowEx(Profit, Nothing, "Editor de Estratégias", Nothing)

    If (Not Menu = 0) Then
        Win32.SetForegroundWindow(Menu)
        SendKeys.Send("{TAB}")
    End If

End Sub 
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • 1
    There are **three** windows involved there. You are jumping straight from the top level window to the window you want. You need to get the `MDIClient` inbetween, **first**. – Idle_Mind Mar 26 '20 at 19:05
  • `FindWindowEx` allows to specify the class name. Use that. Specify `IntPtr.Zero` instead of `Nothing` for a null handle. `Nothing` is ok to pass a null string (as the last argument when calling `FindWindowEx`. As of now, you're passing the Window title as the class name). – Jimi Mar 26 '20 at 19:31
  • Thank tou for your answer. It Works perfectly. – Jorge Pereira Mar 28 '20 at 16:22

1 Answers1

0

There are a few problems with your code.

  • the editor window you are looking for is an MDI child, so you need to get the HWND of its parent MDIClient window first, which you are not doing.

  • in your final FindWindowEx() call, you are passing the editor's text in the lpszClass parameter instead of the lpszWindow parameter.

  • you can't always pass a child window to SetForegroundWindow(). Since you know the editor is an MDI child, you should instruct MDI to bring the editor into focus, if that is what you are trying to do.

Try this instead:

Sub Find_Window

    Dim Profit As IntPtr = Win32.FindWindow("TProfitChartForm", "ProfitPro - 5.0.0.35 - Registrado")
    Dim MDI As IntPtr = Win32.FindWindowEx(Profit, IntPtr.Zero, "MDIClient", "")
    Dim Editor As IntPtr = Win32.FindWindowEx(MDI, IntPtr.Zero, "TLanguageEditorForm", "Editor de Estratégias")

    If Editor <> 0 Then
        Win32.SetForegroundWindow(Profit)
        Win32.SendMessage(MDI, WM_MDIACTIVATE, Editor, 0)
        ' TODO: add this
        ' Dim ChildTextFieldInsideOfEditor as IntPtr = Win32.FindWindowEx(Editor, ...)
        ' Dim ThisThreadId = Win32.GetCurrentThreadId()
        ' Dim EditorThreadId as Integer = Win32.GetWindowThreadProcessId(Editor, ref ProcID)
        ' Win32.AttachThreadInput(ThisThreadId, EditorThreadId, True);
        ' Win32.SetFocus(ChildTextFieldInsideOfEditor)
        ' Win32.AttachThreadInput(ThisThreadId, EditorThreadId, False);
        SendKeys.Send("{TAB}")
    End If

End Sub 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770