Jump to content

how to delete child window


Recommended Posts

i created a child window and set the parent window to another window. the child is transparent and i don't know how to delete it because I check using WinExist() for it's title and no results came. How do i delete it? Here's how i created it.

$clientsize = WinGetClientSize($list[$n][0])
$Child = GUICreate("Child win ", $clientsize[0], $clientsize[1], 0, 0, $WS_POPUP)
DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($Child), "hwnd", WinGetHandle($parent))
GUISetState(@SW_SHOW, $Child_GUI)
Link to comment
Share on other sites

;====================================================
;============= Example of a child window ============
;====================================================
; AutoIt version: 3.0.103
; Language:       English
; Author:         "SlimShady"
;
; ----------------------------------------------------------------------------
; Script Start
; ----------------------------------------------------------------------------

;Include constants
#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()

    ;Initialize variables
    Local $GUIWidth = 250, $GUIHeight = 250
    Local $ParentWin, $ParentWin_Pos, $ChildWin, $msg

    ;Create main/parent window
    $ParentWin = GUICreate("Parent GUI", $GUIWidth, $GUIHeight)
    ;Save the position of the parent window
    $ParentWin_Pos = WinGetPos($ParentWin, "")
    ;Show the parent window/Make the parent window visible
    GUISetState(@SW_SHOW)

    ;Create child window and add the parameter to make it the child of the parent window
    $ChildWin = GUICreate("Child GUI", $GUIWidth, $GUIHeight, $ParentWin_Pos[0] + 100, $ParentWin_Pos[1] + 100, -1, -1, $ParentWin)
    ;Show the child window/Make the child window visible
    GUISetState(@SW_SHOW)

    ;Switch to the parent window
    GUISwitch($ParentWin)

    ;Loop until:
    ;- user presses Esc when focused to the parent window
    ;- user presses Alt+F4 when focused to the parent window
    ;- user clicks the close button of the parent window
    While 1
        ;After every loop check if the user clicked something in the GUI windows
        $msg = GUIGetMsg(1)
        Select
            ;Check if user clicked on a close button of any of the 2 windows
            Case $msg[0] = $GUI_EVENT_CLOSE
                ;Check if user clicked on the close button of the child window
                If $msg[1] = $ChildWin Then
                    MsgBox(64, "Test", "Child GUI will now close.")
                    ;Switch to the child window
                    GUISwitch($ChildWin)
                    ;Destroy the child GUI including the controls
                    GUIDelete()
                    ;Check if user clicked on the close button of the parent window
                ElseIf $msg[1] = $ParentWin Then
                    MsgBox(64, "Test", "Parent GUI will now close.")
                    ;Switch to the parent window
                    GUISwitch($ParentWin)
                    ;Destroy the parent GUI including the controls
                    GUIDelete()
                    ;Exit the script
                    Exit
                EndIf

        EndSelect

    WEnd
EndFunc   ;==>_Main

This script can be found at "YourAutoIt3Directory\Examples\GUI\Simple\child.au3"

Link to comment
Share on other sites

i created a child window and set the parent window to another window. the child is transparent and i don't know how to delete it because I check using WinExist() for it's title and no results came. How do i delete it? Here's how i created it.

$clientsize = WinGetClientSize($list[$n][0])
$Child = GUICreate("Child win ", $clientsize[0], $clientsize[1], 0, 0, $WS_POPUP)
DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($Child), "hwnd", WinGetHandle($parent))
GUISetState(@SW_SHOW, $Child_GUI)

Probably you don't find the child window because you need to set the option

Opt("WinSearchChildren",1)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Thank you martin. It is finding it now but I don't think it is deleting it. :D

Is this the correct way to check if it exists and delete it?

If WinExists("Child win ") Then
        msgbox(0,"","found child")
        GUIDelete("Child win ") ; delete child if it exists
EndIf
Edited by Hypertrophy
Link to comment
Share on other sites

Your way might work, but I like to close gui's with variables in case there is another window with the same name.

If WinExists($Child) Then
        msgbox(0,"","found child")
        GUIDelete($Child) ; delete child if it exists
EndIf
Edited by Hawkwing

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

You're right, I didn't know I couldn't use window titles, only handles. But I am having trouble with getting the "variable undefined error". Here's how I'm doing things:

If WinExists($Child_GUI) Then
    msgbox(0,"","found child OL")
    GUIDelete($Child_GUI)   ; delete child if it exists
EndIf

; create child gui
$Child_GUI = GUICreate("Child ", 232, 323, 0, 0, $WS_POPUP)
DllCall("user32.dll", "int", "SetParent", "hwnd", WinGetHandle($Child_GUI), "hwnd", WinGetHandle($list[$n][0]))
GUISetState(@SW_SHOW, $Child_GUI)

I am checking to see if it exists first and if so delete it. But what if the child has never even been created yet? Then I get an "variable undefined error"

Link to comment
Share on other sites

The first line of that script is checking $Child_GUI to see if it's a window, but since it hasn't been declared yet it gets an error. Just put "Local $Child_GUI" at the start.

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

Can you post the whole script, or at least the gui part, so I can test it?

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

  • Moderators

Can you post the whole script, or at least the gui part, so I can test it?

Hmm, bit puzzled on why you need the "whole" script. A working version would have been nice however.
#include <windowsconstants.au3>



Global $h_parent = WinGetHandle("Program Manager")
Global $a_clientsize = WinGetClientSize($h_parent)

Global $h_child = GUICreate("Child win ", $a_clientsize[0] / 2, $a_clientsize[1] / 2, 0, 0, $WS_POPUP)
_GUI_SetAsChild($h_child, $h_parent)
GUISetState(@SW_SHOW, $h_child)

MsgBox(0, "Ready to delete?", "Go")
_GUI_Delete_Child($h_child)
MsgBox(0, "OK?", "Verify before clicking ok.")

Func _GUI_SetAsChild($h_gui, $h_parent)
    Local $a_ret = DllCall("user32.dll", "int", "SetParent", "hwnd", $h_gui, "hwnd", $h_parent)
    ; If there were an error, normally you'd want to GetLastError api call but I'm too lazy
    If @error Or $a_ret[0] = 0 Then Return SetError(1, 0, 0)
    Return 1
EndFunc

Func _GUI_Delete_Child($h_wnd)
    Local $a_ret = DllCall("user32.dll", "int", "DestroyWindow", "hwnd", $h_wnd)
    ; If there were an error, normally you'd want to GetLastError api call but I'm too lazy
    If @error Or $a_ret[0] = 0 Then Return SetError(1, 0, 0)
    Return 1
EndFunc

Try that.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Well, I didn't actually need the whole thing, just the gui part, but sometimes when autoit users post scripts, they leave out some part that doesn't seem relevant, yet still affects the problem. I probably wouldn't have been able to do anything about this anyway, I'm totally noob at dll stuff.

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Link to comment
Share on other sites

Hmm, bit puzzled on why you need the "whole" script. A working version would have been nice however.

#include <windowsconstants.au3>



Global $h_parent = WinGetHandle("Program Manager")
Global $a_clientsize = WinGetClientSize($h_parent)

Global $h_child = GUICreate("Child win ", $a_clientsize[0] / 2, $a_clientsize[1] / 2, 0, 0, $WS_POPUP)
_GUI_SetAsChild($h_child, $h_parent)
GUISetState(@SW_SHOW, $h_child)

MsgBox(0, "Ready to delete?", "Go")
_GUI_Delete_Child($h_child)
MsgBox(0, "OK?", "Verify before clicking ok.")

Func _GUI_SetAsChild($h_gui, $h_parent)
    Local $a_ret = DllCall("user32.dll", "int", "SetParent", "hwnd", $h_gui, "hwnd", $h_parent)
    ; If there were an error, normally you'd want to GetLastError api call but I'm too lazy
    If @error Or $a_ret[0] = 0 Then Return SetError(1, 0, 0)
    Return 1
EndFunc

Func _GUI_Delete_Child($h_wnd)
    Local $a_ret = DllCall("user32.dll", "int", "DestroyWindow", "hwnd", $h_wnd)
    ; If there were an error, normally you'd want to GetLastError api call but I'm too lazy
    If @error Or $a_ret[0] = 0 Then Return SetError(1, 0, 0)
    Return 1
EndFunc

Try that.

Tried it out and changed it to fit in my code and it didn't work. Of course your original works. Would it make a difference if I was using masks to make my child transparent? Maybe I need to delete the masks?

[autoit]

Link to comment
Share on other sites

Tried it out and changed it to fit in my code and it didn't work. Of course your original works. Would it make a difference if I was using masks to make my child transparent? Maybe I need to delete the masks?

[autoit]

"Didn't work" tells us very little.

Did the child get created? Did it show in the parent?

Did you pass the child window handle to the delete function?

Masks won't affect being able to delete a window.

If instead of deleting the child you have

_GUI_SetAsChild($h_gui, WInGetHandle("Program Manager"));where $h_gui is the child window handle

does it remove the child and set it as a top level window?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...