Parent App or Program Window to GUI

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Oblituarius
Posts: 13
Joined: 31 Mar 2019, 16:57

Parent App or Program Window to GUI

Post by Oblituarius » 05 Jul 2022, 11:36

Hi everyone!
I'm trying to use @joedf 's LibCon library (https://github.com/joedf/LibCon.ahk) for debugging my script, and I decided I wanted to keep it for the final product as well so that users can keep track of stuff.
I'm trying to parent the console window that is generated to a GUI so I can add buttons, text, etc.

Currently, this is what it looks like.
image.png
image.png (22.61 KiB) Viewed 880 times
This is the code so far, when I try to use SetChild, be it the ID or the Title version, the console window never appears to be rendered inside the GUI. I'm using AutoHotkey v1.1.34.03

Code: Select all

#NoEnv
#SingleInstance, Force
#Persistent
#InstallKeybdHook
#UseHook
#KeyHistory, 0
#HotKeyInterval 1
#MaxHotkeysPerInterval 2500

#Include LibCon.ahk ; include the LibCon-library : https://github.com/joedf/LibCon.ahk
OnExit, CleanUp

Global ConsoleID
Global ConsoleH
Global MainWT := "MainWindow"

Gui, 1: Show , x5 y600 w300 h100, %MainWT%
Gui, +LastFound
Gui 1: +hwndGui1
SmartStartConsole()
GetConsoleTitle(ConsoleID)
SetConsoleSize( 121, 10, 0 )
SetConsoleTitle( "Console" )
ID := DllCall("GetParent", UInt,WinExist("A")), ID := !ID ? WinExist("A") : ID
;MoveWindow(ahk_id %id%, 0, 740) ;Used only if the windows are seperate
;SetChildID(ID, 1) ;To attempt to parent Console to MainWindow


puts(TimeStamp() . " - Hello World!") ; write some test to the console 
sleep, 5000
puts(TimeStamp() . " - Some more text")
sleep, 5000
return

; Library
SetChildID(Window_ID, Gui_Number)
{ 
  Gui, %Gui_Number%: +LastFound 
  Return DllCall("SetParent", "uint", Window_ID, "uint", WinExist()) ; success = handle to previous parent, failure =null
} 

SetChildTitle(Window_Title_Text, Gui_Number)
{ 
  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", Parent_Handle, "uint", WinExist() ) ; success = handle to previous parent, failure =null
} 

Set_Parent_by_class(Window_Class, Gui_Number)
{ 
  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
} 

MoveWindow(WinTitle, xPos, yPos)
{
    WinGetPos,,, Width, Height, %WinTitle%
    WinMove, %WinTitle%,, xPos, yPos
}

TimeStamp()
{
	FormatTime, HelixTime, A_Now, M/d/yyyy hh:mm:ss
	return HelixTime
}

CleanUp:
FreeConsole() ;Kill Console and keep ahk running till exit
sleep 100
ExitApp

GuiClose:
FreeConsole()
ExitApp
Any help is appreciated!

User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Parent App or Program Window to GUI

Post by joedf » 05 Jul 2022, 12:08

This is a neat idea - to embed the console on a GUI window. I've never tried this before, I've always just emulated it when I needed in a GUI.
@Ronins has done a great job on making libcon into a class (though it looks like a subset of the functions supported by libcon, the most important ones i'd say). And from a screenshot on the post, it seems the embedding on a GUI was successful here:
viewtopic.php?t=2827

Found it! Embeded console example here:
https://github.com/AnmolSinha1201/AHK-Console-Class/blob/master/Example4.ahk

Maybe you can retro-fit it to your needs without having to change libraries. :+1:

EDIT: looks like this is what you need

Code: Select all

EmbedConsole(GuiHWnd, X=10, Y=10)
{
	ConsoleHWnd := DllCall("GetConsoleWindow")
	VarSetCapacity(ConsoleRect, 16)
	DllCall("GetClientRect", "uint", ConsoleHWnd, "uint", &ConsoleRect)
	CWidth := NumGet(ConsoleRect, 8)
	CHeight:= NumGet(ConsoleRect, 12)
	WinSet, Style, -0x80C40000, ahk_id %ConsoleHWnd% ;WS_PopUp, WS_Caption, WS_ThickFrame
	WinSet, Style, +0x40000000, ahk_id %ConsoleHWnd% ;WS_Child
	WinSet, ExStyle, -0x200, ahk_id %ConsoleHWnd% ;WS_Ex_ClientEdge
	WinSet, Style, +0x2000000, % "ahk_id " GuiHWnd ;WS_ClipChildren
	DllCall("SetParent", "int", ConsoleHWnd, "int", GuiHWnd)
	DllCall("SetWindowPos", "int", ConsoleHWnd, "int", 0, "int", X, "int", Y, "int", CWidth, "int", CHeight, "int", 0x400)
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Oblituarius
Posts: 13
Joined: 31 Mar 2019, 16:57

Re: Parent App or Program Window to GUI

Post by Oblituarius » 05 Jul 2022, 14:19

Thanks for your reply @joedf! Being a noobie on AHK, I've tried running that code you provided and can't seem to get it to work.
Surely, the MainWindow GUI is the only one that stays (the console does blink into life for a couple of frames before it disappears)
This is how it looks with your function:
image.png
image.png (4.4 KiB) Viewed 792 times

Code: Select all

init:
#NoEnv
#SingleInstance, Force
#Persistent
#InstallKeybdHook
#UseHook
#KeyHistory, 0
#HotKeyInterval 1
#MaxHotkeysPerInterval 2500

DetectHiddenWindows, On

#Include LibCon.ahk ; include the LibCon-library : https://github.com/joedf/LibCon.ahk
OnExit, CleanUp

Global ConsoleID
Global ConsoleH
Global MainWT := "MainWindow"

Gui, 1: Show , x5 y600 w300 h100, %MainWT%
Gui, +LastFound
Gui 1: +hwndGui1
SmartStartConsole()
EmbedConsole(Gui1, X=10, Y=10)
return


; Library
MoveWindow(WinTitle, xPos, yPos)
{
    WinGetPos,,, Width, Height, %WinTitle%
    WinMove, %WinTitle%,, xPos, yPos
}

TimeStamp()
{
	FormatTime, HelixTime, A_Now, M/d/yyyy hh:mm:ss
	return HelixTime
}

EmbedConsole(GuiHWnd, X=10, Y=10)
{
	ConsoleHWnd := DllCall("GetConsoleWindow")
	VarSetCapacity(ConsoleRect, 16)
	DllCall("GetClientRect", "uint", ConsoleHWnd, "uint", &ConsoleRect)
	CWidth := NumGet(ConsoleRect, 8)
	CHeight:= NumGet(ConsoleRect, 12)
	WinSet, Style, -0x80C40000, ahk_id %ConsoleHWnd% ;WS_PopUp, WS_Caption, WS_ThickFrame
	WinSet, Style, +0x40000000, ahk_id %ConsoleHWnd% ;WS_Child
	WinSet, ExStyle, -0x200, ahk_id %ConsoleHWnd% ;WS_Ex_ClientEdge
	WinSet, Style, +0x2000000, % "ahk_id " GuiHWnd ;WS_ClipChildren
	DllCall("SetParent", "int", ConsoleHWnd, "int", GuiHWnd)
	DllCall("SetWindowPos", "int", ConsoleHWnd, "int", 0, "int", X, "int", Y, "int", CWidth, "int", CHeight, "int", 0x400)
}

CleanUp:
FreeConsole() ;Kill Console and keep ahk running till exit
sleep 100
ExitApp

GuiClose:
FreeConsole()
ExitApp
I'm not sure what am I doing wrong, if you could guide me more, I would appreciate it.

I also tried out Example4 with Console.ahk and this is all that I get. Not sure if this is all that it's supposed to appear or not. Maybe it's a Windows 11 issue?
image.png
image.png (9.4 KiB) Viewed 796 times

User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Parent App or Program Window to GUI

Post by joedf » 05 Jul 2022, 14:57

Yeah,this code was written a while and has only been tested mostly for Win7...
I tested on my win10 system, it doesn't work either...

Code: Select all

;WinGetPos,,, W, H, % "ahk_id" ConsoleHWnd ;commented this
W:=600, H:=800 ;added this
My suspicion is GetConsoleWindow works differently...
See https://docs.microsoft.com/en-us/windows/console/getconsolewindow

Perhaps you'll need to use AHK with the console subsystem flag set...
more notes here: https://github.com/joedf/LibCon.ahk/blob/master/LibCon_Compiler/Note.md

maybe give Ahk2CLI a try?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Oblituarius
Posts: 13
Joined: 31 Mar 2019, 16:57

Re: Parent App or Program Window to GUI

Post by Oblituarius » 05 Jul 2022, 16:02

I just gave Ahk2CLI a try, and unfortunately, even for Example4 it still fails to show the console inside the GUI. I'm not too savvy around MS stuff, so I'll see if I can find any other alternatives, or just keep them separated, besides, it was more about the aesthetic of having 1 window haha thanks for your help!

User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Parent App or Program Window to GUI

Post by joedf » 05 Jul 2022, 16:27

No worries, I have one more suggestion is the emulated path... Take what you will from this, avoids cmd altogether.
You could use AfterLemon+tidbit's "GUI-based" Console:
https://github.com/AfterLemon/Class_Console

Here's a more recent fork of it:
https://github.com/hi5/Class_Console
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Oblituarius
Posts: 13
Joined: 31 Mar 2019, 16:57

Re: Parent App or Program Window to GUI

Post by Oblituarius » 05 Jul 2022, 19:57

That looks rather interesting! I will definitely look at it tomorrow for sure! Thank you so much!

Post Reply

Return to “Ask for Help (v1)”