Jump to content

Activation, selection of child windows


Tick
 Share

Recommended Posts

The main program window can have several child (MDIClient) windows for different purposes. The content of the main menu items depends on which of the windows is active. How to choose one of the child windows correctly?

Opt("WinSearchChildren", 1)
Opt("WinTitleMatchMode", 2)
$hWnd=WinGetHandle ("GW:"); The part of the window name that I want to select
WinActivate($hWnd) ; does not work, other child window still active

MsgBox(0,"",WinGetTitle($hWnd)); the full name of the desired window is displayed

This is not working in my case, desired window is not selected. WinActivate($hWnd, "GW:") not working too..

 

Autoit window info shows CLASS:MDIClient; INSTANCE:2; ID:65283, and Instance and ID value is not constant. When I close several windows and reopen the desired window, they may become different values. Only the title of the window is always the same.

I found a way, but think  it is not quite correct:

$hWnd=WinGetHandle ("GW:")
WinSetState($hWnd, "", @SW_MINIMIZE)
WinSetState($hWnd, "", @SW_SHOWDEFAULT)

the window is simply minimized and then restored, becoming active as I want. But it is not user friendly.

I am sure there is a better solution. Please help me find it!

Edited by Tick
Link to comment
Share on other sites

Hi Tick,
Maybe I didn't understand well but I got no problem to activate a child window when its handle is known, like in the following script (Esc to ask which window to activate) :

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "HotKeyPressed")
Global $hMain, $hChild1, $hChild2

$hMain = GUICreate("Main (ESC to ask which window to activate)", 640, 480, 100, 100)
GUICtrlCreateLabel("Label on main gui", 10, 10)

$hChild1 = GUICreate("Child 1", 300, 300, 50, 50, -1, $WS_EX_MDICHILD, $hMain)
GUISetBkColor($COLOR_CREAM)
GUICtrlCreateLabel("Label 1",10,10)

$hChild2 = GUICreate("Child 2", 300, 300, 100, 100, -1, $WS_EX_MDICHILD, $hMain)
GUISetBkColor($COLOR_SKYBLUE)
GUICtrlCreateLabel("Label 2", 10, 10)

GUISetState(@SW_SHOW, $hMain)
GUISetState(@SW_SHOW, $hChild1)
GUISetState(@SW_SHOW, $hChild2)

Local $aMsg = 0
While 1
    $aMsg = GUIGetMsg($GUI_EVENT_ARRAY)
    Switch $aMsg[1]
        Case $hMain
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
            EndSwitch

        Case $hChild1
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($hChild1)
            EndSwitch

        Case $hChild2
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($hChild2)
            EndSwitch
    EndSwitch
WEnd

;========================================================
Func HotKeyPressed()

    HotKeySet("{ESC}") ; deactivate hotkey

    Local $iNumber = - 1
    Do
        $sNumber = InputBox("Activate what ?", _
            "0 = main, 1 = child1, 2 = child2 (0-2)", "0", " M1", 220, 140, Default, Default, 0, $hMain)
        If @error Then ; Esc or Cancel button in InputBox
            $iNumber = - 1
            ExitLoop
        EndIf
        If $sNumber < "0" Or $sNumber > "2" Then ContinueLoop
        $iNumber = Number($sNumber)
    Until IsInt($iNumber) And $iNumber >= 0 And $iNumber <= 2
    ; MsgBox($MB_TOPMOST, "Result", "Your choice : " & $iNumber)

    Switch $iNumber
        Case -1, 0
            WinActivate($hMain)
        Case 1
            WinActivate($hChild1)
        Case 2
            WinActivate($hChild2)
        Case Else
            MsgBox($MB_TOPMOST, "Impossible", "$iNumber = " & $iNumber)
            WinActivate($hMain)
    EndSwitch

    HotKeySet("{ESC}", "HotKeyPressed") ; reactivate hotkey
EndFunc   ;==>HotKeyPressed

 

Link to comment
Share on other sites

3 hours ago, Nine said:

You may also want to try :

_WinAPI_SetWindowPos ($hWnd, $HWND_TOP, 0, 0, 0, 0, BitOR($SWP_SHOWWINDOW, $SWP_NOMOVE, $SWP_NOSIZE))

 

Thank you very much! I would never have guessed! :):) Of cource it works.

Link to comment
Share on other sites

3 hours ago, pixelsearch said:

Hi Tick,
Maybe I didn't understand well but I got no problem to activate a child window when its handle is known, like in the following script (Esc to ask which window to activate) :

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>

HotKeySet("{ESC}", "HotKeyPressed")
Global $hMain, $hChild1, $hChild2

$hMain = GUICreate("Main (ESC to ask which window to activate)", 640, 480, 100, 100)
GUICtrlCreateLabel("Label on main gui", 10, 10)

$hChild1 = GUICreate("Child 1", 300, 300, 50, 50, -1, $WS_EX_MDICHILD, $hMain)
GUISetBkColor($COLOR_CREAM)
GUICtrlCreateLabel("Label 1",10,10)

$hChild2 = GUICreate("Child 2", 300, 300, 100, 100, -1, $WS_EX_MDICHILD, $hMain)
GUISetBkColor($COLOR_SKYBLUE)
GUICtrlCreateLabel("Label 2", 10, 10)

GUISetState(@SW_SHOW, $hMain)
GUISetState(@SW_SHOW, $hChild1)
GUISetState(@SW_SHOW, $hChild2)

Local $aMsg = 0
While 1
    $aMsg = GUIGetMsg($GUI_EVENT_ARRAY)
    Switch $aMsg[1]
        Case $hMain
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
            EndSwitch

        Case $hChild1
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($hChild1)
            EndSwitch

        Case $hChild2
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($hChild2)
            EndSwitch
    EndSwitch
WEnd

;========================================================
Func HotKeyPressed()

    HotKeySet("{ESC}") ; deactivate hotkey

    Local $iNumber = - 1
    Do
        $sNumber = InputBox("Activate what ?", _
            "0 = main, 1 = child1, 2 = child2 (0-2)", "0", " M1", 220, 140, Default, Default, 0, $hMain)
        If @error Then ; Esc or Cancel button in InputBox
            $iNumber = - 1
            ExitLoop
        EndIf
        If $sNumber < "0" Or $sNumber > "2" Then ContinueLoop
        $iNumber = Number($sNumber)
    Until IsInt($iNumber) And $iNumber >= 0 And $iNumber <= 2
    ; MsgBox($MB_TOPMOST, "Result", "Your choice : " & $iNumber)

    Switch $iNumber
        Case -1, 0
            WinActivate($hMain)
        Case 1
            WinActivate($hChild1)
        Case 2
            WinActivate($hChild2)
        Case Else
            MsgBox($MB_TOPMOST, "Impossible", "$iNumber = " & $iNumber)
            WinActivate($hMain)
    EndSwitch

    HotKeySet("{ESC}", "HotKeyPressed") ; reactivate hotkey
EndFunc   ;==>HotKeyPressed

 

Thanks, Pixelsearch! I really don't know why it doesn't work in my case.
I tried your script, and of course everything works fine on the newly created elements. But in an external program, I cannot select the desired window using selects. Nine's version works great.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...