Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

SetParent function (make a gui belong to another window)


  • Please log in to reply
13 replies to this topic
evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Here is a function to make a gui window belong to another program's window (it will then get minimized/moved along with the other parent window without needing to use a timer). Some program's don't work well with this method, like notepad and calculator, you also can't position the gui on the titlebar.

(The original code came from Laszlo's taskbar clock).

You can either use the title of a program's window to identify the parent or if you use the function I commented out below instead, then you can use the class's name.

; e.g. run an internet explorer window and go to the page: "about:blank" so that is the start of the title text 
;   then run this script and it adds a gui window onto the internet explorer window. 
; Some programs, such as notepad and calculator, don't seem to work properly with this technique. 

Gui, 7: Margin, 0, 0 
Gui, 7: +ToolWindow ; -Caption ; no title, no taskbar icon 
Gui, 7: Add, Text,, This gui is stuck to the parent... `nbut I haven't programmed it to do anything `;-) 

Set_Parent_by_title("about:blank", 7) ; can be done before or after showing the gui
Gui, 7: Show, x200 y200 
Return 



/*
or to use the window id of the parent instead of the title: 

Set_Parent_by_id(Window_ID, Gui_Number) ; title text is the start of the title of the window, gui number is e.g. 99 
{ 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall("SetParent", "uint", WinExist(), "uint", Window_ID) ; success = handle to previous parent, failure =null
} 
*/



Set_Parent_by_title(Window_Title_Text, Gui_Number) ; title text is the start of the title of the window, gui number is e.g. 99 
{ 
  WinGetTitle, Window_Title_Text_Complete, %Window_Title_Text% 
  Parent_Handle := DllCall( "FindWindowEx", "uint",0, "uint",0, "uint",0, "str", Window_Title_Text_Complete) 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall( "SetParent", "uint", WinExist(), "uint", Parent_Handle ) ; success = handle to previous parent, failure =null
} 



/* 
or to use the class instead of the title: 

Set_Parent_by_class(Window_Class, Gui_Number) ; class e.g. Shell_TrayWnd, gui number is e.g. 99 
{ 
  Parent_Handle := DllCall( "FindWindowEx", "uint",0, "uint",0, "str", Window_Class, "uint",0) 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall( "SetParent", "uint", WinExist(), "uint", Parent_Handle ) ; success = handle to previous parent, failure =null
} 
*/ 


evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Here's an example which sticks a gui to the desktop:

Gui, 7: Margin, 0, 0 
Gui, 7: +ToolWindow ; -Caption ; no title, no taskbar icon 
Gui, 7: Add, Text,, This gui is stuck to the parent... `nbut I haven't programmed it to do anything `;-) 
Set_Parent_by_class("Progman", 7) 
Gui, 7: Show, x200 y200 
Return 

Set_Parent_by_class(Window_Class, Gui_Number) ; class e.g. Shell_TrayWnd, gui number is e.g. 99 
{ 
  Parent_Handle := DllCall( "FindWindowEx", "uint",0, "uint",0, "str", Window_Class, "uint",0) 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall( "SetParent", "uint", WinExist(), "uint", Parent_Handle ) 
} 




Here's another example that creates a GUI within a GUI (i.e. a window within a window so you can have multiple windows but all inside one interface):

Gui, add, text, x-10 y-10, ; need something in the parent window to enable minimizing
Gui, +lastfound
Gui_1_ID := WinExist()

Gui, Show, h140 w200, Parent 
Set_Parent_by_id(Gui_1_ID, 2) ; Window_ID, Gui_Number
Gui, 2: Show, x5 y5 h80 w150, Child 
Return 

Set_Parent_by_id(Window_ID, Gui_Number) ; title text is the start of the title of the window, gui number is e.g. 99 
{ 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall("SetParent", "uint", WinExist(), "uint", Window_ID) ; success = handle to previous parent, failure =null 
}


evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Updated my first post a bit with some slight improvements and another function for using the parent windows ahk_id directly, thanks to Goyyah, who wrote a script for sticking a clock to the desktop, here:

http://www.autohotke... ... 4403#54403

Thalon
  • Members
  • 641 posts
  • Last active: Jan 02 2017 12:17 PM
  • Joined: 12 Jul 2005
This can create really nice effects!
Also if it is simple you brought me to the idea of "window-collections" :D

It could be also a way to hide the window from taskbar (as sometimes mentioned)..

Thalon

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Attaching (docking) a gui window to a specific toolbar (e.g. Quick Launch):

This is an example I customised from an idea by jonib:
http://www.autohotke...opic.php?t=9844
http://www.autohotke...opic.php?t=8044

; Note that a toolbar title can be hidden by right-clicking & un-locking the Taskbar and then properties of the toolbar.


Gui, 7: Margin, 0, 0 
Gui, 7: -Caption +ToolWindow  ; no title, no taskbar icon
Gui, 7: Add, Text,, BAR TEXT HERE!

Set_Parent_to_Toolbar("Quick Launch", 7) ; (Toolbar title (name) to attach to, gui number)

Gui, 7: Show, x0 y4
Return 



Set_Parent_to_Toolbar(ToolbarName, Gui_Number) ; title text is the start of the title of the window, gui number is e.g. 99 
{ 
  hw_tray:=FindToolbar(ToolbarName) 
  If ! hw_tray
    { 
    Msgbox, Toolbar "%ToolbarName%" not found! Exiting.
    Exitapp 
    } 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall("SetParent", "uint", WinExist(), "uint", hw_tray) ; success = handle to previous parent, failure =null 
} 

FindToolbar(ToolbarName) 
{    
  WinExist("ahk_class Shell_TrayWnd") ; Find Systemtray 
  WinGet, Controls ,ControlList ;Get list of all controls 
  Loop, Parse, Controls, `n 
  { 
    ControlGetText, CurControl , %A_LoopField% 
    if CurControl=%ToolbarName% ;Find the toolbar we want by comparing controls Text 
       ControlGet, hwnd, Hwnd,,%A_LoopField% ;Get the handle for the toolbar 
  } 
  Return hwnd 
}


ProsperousOne
  • Members
  • 115 posts
  • Last active: Mar 12 2011 09:15 PM
  • Joined: 19 Sep 2005
As stated, why doesn't this work with some programs (like notes or Lotus Notes)?

  • Guests
  • Last active:
  • Joined: --
sorry for posting in an old topic but i think this is worth knowing.

For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

http://msdn.microsof...ry ... 85).aspx

tommy
  • Members
  • 84 posts
  • Last active: Nov 03 2014 06:15 AM
  • Joined: 05 Oct 2010
Nice job with this. Is there a way to set it so I can place the child window so it overlaps the title blue section on top of the screen? I tried alwaysontop but that didn't seem to work.

ZeLen1y
  • Members
  • 44 posts
  • Last active: Oct 13 2014 09:43 PM
  • Joined: 11 Oct 2006
Nice try but this function can be found in majkinetor's standart library "Win - Set of window functions"

http://www.autohotke...pic.php?t=47856

Example
Gui, 7: Margin, 0, 0
Gui, 7: +ToolWindow
Gui, 7: Add, Text,, This gui is stuck to the parent... `nbut I haven't programmed it to do anything `;-)

[color=red]Gui, 7: +LastFound
Win_SetParent(WinExist(), WinExist("about:blank"))
[/color]

Gui, 7: Show, x200 y200
Return


tidbit
  • Administrators
  • 2709 posts
  • Hates playing Janitor
  • Last active: Jan 15 2016 11:37 PM
  • Joined: 09 Mar 2008
ZeLen1y your point is? It's always good t have options.
and fyi, this one is about 3 years older.

rawr. be very afraid
*poke*
. Populate the AutoHotkey city. Pointless but somewhat fun. .


tommy
  • Members
  • 84 posts
  • Last active: Nov 03 2014 06:15 AM
  • Joined: 05 Oct 2010
Nice work with this. Can you advise how I can make the child window overlaps the title blue bar on top of the screen? It seems when I use a negative number to move the GUI up it goes under the Title bar instead of over the top. Your help would be appreciated.

batagy
  • Members
  • 15 posts
  • Last active: Mar 10 2011 11:00 AM
  • Joined: 21 Feb 2007
Hi!

So is it maybe possible to grouping some processes into one tabbed group?
I mean, to make a tabbed interface to Putty.
A very simple way would be also enough, just to group the same processes into one window, which has 1 taskbar button only, and then you can select from the Putty windows with a simple clicking on a tab at the upper part.

I mean a function like the below program:
http://www.windowtabs.com/

Maybe the mentioned library "Win 1.25 - Set of window functions" could do that?

Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

 

@evl:

Here's an example which sticks a gui to the desktop:

 

I am using this method in this (simplified) script:


WinGet, IdDesktop, ID, ahk_class Progman

Gui Child: -dpiscale -Caption 0x400000 	
Gui Child: Add, Text,, I am a Child

Gui Child: +LastFound
IdChild := WinExist()

DllCall("SetParent", ptr, IdChild, ptr, IdDesktop)  

Gui Child: Show

MY QUESTION:

 

Is there a way to bring such a desktop child temporarily on top of all other windows (by a hotkey defined in the same ahk script)?



Zvonko
  • Members
  • 173 posts
  • Last active: Jan 24 2016 05:21 PM
  • Joined: 17 Oct 2014

 

MY QUESTION:

 

Is there a way to bring such a desktop child temporarily on top of all other windows (by a hotkey defined in the same ahk script)?

 

And the answer is:

http://www.autohotke...sktop-in-front/