Jump to content

Create a child window included in some parent window


Recommended Posts

We can create an our own toolbar for some program with creating a child window included in this program, but I seems easy problem is, how can we detect that the parent window is closed or not? As we know, our child window will not exit automaticly when its parent window is closed.

My method to do this is as following. Using the command WinExists every second. Of cause, it works, but I think this method is not a good way. Some time, the parent window will not be closed for may hours, and we use WinExists second and second, It's so waste, and can not close the child window in time.

Anybody has any better idea? Any reply would be appreciated. Thanks

Link to comment
Share on other sites

..., and can not close the child window in time.

We need to close the child window in time because that we some time need to restart the parent program, and I found that there is some strange problem, like the child window will not floating in the parent window but be covered by the parent window, this is a fatal problem. And also, we some time do not allow the child window run a second instance because mutex of resources.

Link to comment
Share on other sites

"...As we know, our child window will not exit automaticly when its parent window is closed...."

By this do you mean the closing of the script? Or how are you closing the parent window? If you have and exit button, the x button, or exitonesc, you could put a winkill() on the exit of parent window...

" I haven't failed. I've just found 10,000 ways that won't work." Thomas Edison "You cannot help men permanently by doing for them what they could and should do for themselves." Abraham Lincoln

Link to comment
Share on other sites

"...As we know, our child window will not exit automaticly when its parent window is closed...."

By this do you mean the closing of the script? Or how are you closing the parent window? If you have and exit button, the x button, or exitonesc, you could put a winkill() on the exit of parent window...

I mean the closing of the script
Link to comment
Share on other sites

then winkill() onautoitexit

" I haven't failed. I've just found 10,000 ways that won't work." Thomas Edison "You cannot help men permanently by doing for them what they could and should do for themselves." Abraham Lincoln

Link to comment
Share on other sites

try to search the forum as there are ways to embed the child in the parent :D

i think the func was guisetparent (not sure though)

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Link to comment
Share on other sites

try to search the forum as there are ways to embed the child in the parent :D

i think the func was guisetparent (not sure though)

DllCall("user32.dll", "int", "SetParent", "hwnd", $hChild, "hwnd", $hParent)
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

DllCall("user32.dll", "int", "SetParent", "hwnd", $hChild, "hwnd", $hParent)

I tried this UDF

#include <GUIConstants.au3>
GUICreate("testing", 100, 30, 1, 1)
GUISetState()
Run("notepad")
WinWait("untitled")
_SetParent("testing", "untitled")

While 1
    Sleep(200)
    
WEnd

Func _SetParent($TitleP, $TitleC)
    If WinExists($TitleP) Then
        If WinExists($TitleC) Then
            $HwndP = WinGetHandle($TitleP)
            $HwndC = WinGetHandle($TitleC)
            $user32 = DllOpen("user32.dll")
            DllCall($user32, "str", "SetParent", "HWnd", $HwndP, "HWnd", $HwndC)
            Return 1
        Else
            Return -1
        EndIf
    Else
        Return -1
    EndIf
EndFunc   ;==>_SetParent

This UDF still do not meet my need. Please try the following example. run it, and then a child window is create embbed the notepad window, close the notepad, we found the script is still running, how can we close the script as soon as the notepad is closed?

Edited by chenxu
Link to comment
Share on other sites

chenxu - I guess what you are trying to say is that your script makes a toolbar window for GUI that was not written in AutoIt, something like notepad.

If so, your WinExists method is best, as far as I know, if you are going to use AutoIt for this.

But this worked fine on my machine using Windows XP SP1 and AutoIt v3.2.10.0 -

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;http://www.autoitscript.com/forum/index.php?s=&showtopic=69527&view=findpost&p=515376

;The following line replaces $GUI_SS_DEFAULT_GUI because it doesn't appear in AutoIt v3.2.10.0 -
Global Const $DEFAULT_GUISTYLES = BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU)

;Global $notpad_PID = Run(@ComSpec & " /c notepad.exe", "", @SW_HIDE); returns a PID of the notepad.exe process
;Sleep(1800)

ShellExecute("notepad.exe", "", "", "open", @SW_MAXIMIZE)
Sleep(1300); required to wait for window to appear - use longer sleep if necessary on slower machine.

Opt("WinTitleMatchMode", 2)
Global $notepad_Title = WinGetTitle("Notepad"); gets the title of the most recently active notepad window

Opt("WinTitleMatchMode", 3)
Global $notepad_Handle = WinGetHandle($notepad_Title)
WinSetState($notepad_Handle, "", @SW_MAXIMIZE)

If @error Or $notepad_Handle = 0 Then MsgBox(0, "", "Error getting notepad window handle")
Global $child_Toolbar = GUICreate("Child GUI", 270, 40, -1, -1, _
    BitOR($DEFAULT_GUISTYLES, $WS_CLIPSIBLINGS), _
    BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW), $notepad_Handle)
GUISetState()

Sleep(300)
DllCall("user32.dll", "int", "SetParent", "hwnd", $child_Toolbar, "hwnd", $notepad_Handle)
While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
    If Not WinExists($notepad_Handle) Then ExitLoop
    Sleep(70)
WEnd

Not compiled, the script used only 6 megabytes of memory, not including the 3 megabytes used by Notepad. The script should close when Notepad closes. Don't use the DllCall on line 28 if you don't want to loose track of the toolbar window.

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

  • 2 years later...

I realize this is an old topic, but the issue still plagues us today.

I have an auto-it created GUI which I want to stay in-position with a parent window. This parent window is NOT created by Autoit.

Right now I use a loop every 500ms to check the location on the parent window and then update the autoit GUI to match, but that seems a bit silly if windows can already do that.

When I run the above code (that Squirrely1) posted, the 'Child GUI' only stays open for as long as the sleep function (on line 27), then the child GUI vanishes. The script is still running, but 'Child GUI' is invisible. I've tried putting 'GuiSetState(@SW_SHOW,$child_Toolbar)' in the loop, before the loop, but to no avail. The 'Child GUI' is just plain invisible. Not only invisible, but gone. WinExists reports it as not existing.

So, why does the DLL call make the child vanish? I'm on XP SP3, if that helps. Does anyone else have this issue?

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...