VB.NET - I can't manage this window - Win32 - FindWindowEx

238 views Asked by At

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 
1

There are 1 answers

0
Remy Lebeau On BEST ANSWER

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