AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SetParent function (make a gui belong to another window)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Sun Mar 26, 2006 6:04 pm    Post subject: SetParent function (make a gui belong to another window) Reply with quote

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.

Code:

; 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
}
*/


Last edited by evl on Mon Mar 27, 2006 11:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Sun Mar 26, 2006 6:10 pm    Post subject: Reply with quote

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

Code:

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):

Code:

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
}


Last edited by evl on Tue Apr 11, 2006 11:25 pm; edited 3 times in total
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Mon Mar 27, 2006 11:15 pm    Post subject: Reply with quote

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.autohotkey.com/forum/viewtopic.php?p=54403#54403
Back to top
View user's profile Send private message
Thalon



Joined: 12 Jul 2005
Posts: 640

PostPosted: Tue Mar 28, 2006 6:37 am    Post subject: Reply with quote

This can create really nice effects!
Also if it is simple you brought me to the idea of "window-collections" Very Happy

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

Thalon
_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum
SacredVault
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Tue May 16, 2006 8:42 pm    Post subject: Reply with quote

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.autohotkey.com/forum/viewtopic.php?t=9844
http://www.autohotkey.com/forum/viewtopic.php?t=8044

Code:

; 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
}
Back to top
View user's profile Send private message
ProsperousOne



Joined: 19 Sep 2005
Posts: 100

PostPosted: Wed Aug 20, 2008 2:26 pm    Post subject: Reply with quote

As stated, why doesn't this work with some programs (like notes or Lotus Notes)?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group