SetParent vs PotPlayer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

SetParent vs PotPlayer

20 Apr 2021, 04:42

Code: Select all

#Persistent
#SingleInstance, Force
SetTitleMatchMode, 2

Global PotGuiHwnd
Global PotPlayerID
Global PotPlayerPID

Gui, Main: +HwndPotGuiHwnd -DPIScale

SetTimer, Main, 1
return


Main:
if WinExist("PotPlayer")
{
WinWait, PotPlayer
WinGet, CurrentPotPlayerID, ID, PotPlayer
WinGet, CurrentPotPlayerPID, PID, PotPlayer

;Hook
DllCall("SetWinEventHook"
, "UInt",   0x8004
, "UInt",   0x8004
, "Ptr" ,   0x0
, "Ptr" ,   RegisterCallback("WinEventProc")
, "UInt",   CurrentPotPlayerPID
, "UInt",   0x0
, "UInt",   0x0|0x2)  ;_In_  UINT dwflags
;----


DllCall("SetParent", ptr, PotGuiHwnd, ptr, CurrentPotPlayerID)
Gui, Main: Show, w300 h40 x0 y0 NA

WinWaitClose, ahk_id %CurrentPotPlayerID%
DllCall("UnhookWinEvent", "Ptr",Hook)
}
Return


WinEventProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime) {
WinSet, Top, , ahk_id %PotGuiHwnd%
}

Basically trying to make a nested gui inside PotPlayer and having some issues!

1.The code above works fine but when the PotPlayer window is closed and runs again, something weird happens, the gui "bugs out" and i can't get it to show up.
I can work-around this with gui destroy and creating the gui in the "main" every time but that's my last resort and i'd rather not do that.

2.You see that winevent hook? i made it because the gui window would get "behind" the potplayer window when the potplayer window is resized and on a couple other occasions.
you can see what happens if you just delete WinSet, Top, , ahk_id %PotGuiHwnd% line from the wineventproc function. Any way to make the gui "always above" instead of constantly firing WinSet, Top, , ahk_id %PotGuiHwnd%?
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: SetParent vs PotPlayer

20 Apr 2021, 05:50

Code: Select all

Gui, -DPIScale
Gui, Font, s12
Gui, Add, Text, w220 Center, TEST
Loop {
 WinWaitActive, PotPlayer ahk_class PotPlayer64
 Gui, % "+Owner" WinActive("A")
 Gui, +AlwaysOnTop
 Gui, Show, NoActivate, Test
 Gui, -AlwaysOnTop
 SoundBeep, 1500
 WinWaitClose
 Gui, Hide
 SoundBeep, 1000
}
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

20 Apr 2021, 06:12

mikeyww wrote:
20 Apr 2021, 05:50

Code: Select all

Gui, -DPIScale
Gui, Font, s12
Gui, Add, Text, w220 Center, TEST
Loop {
 WinWaitActive, PotPlayer ahk_class PotPlayer64
 Gui, % "+Owner" WinActive("A")
 Gui, +AlwaysOnTop
 Gui, Show, NoActivate, Test
 Gui, -AlwaysOnTop
 SoundBeep, 1500
 WinWaitClose
 Gui, Hide
 SoundBeep, 1000
}
Nope same issue :/ the setparent dllcall is at fault, i figured out some time ago that if i do Gui, Main: Hide right before potplayer window is closed then the code works flawlessly but otherwise the gui just disappears never to be seen again.
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: SetParent vs PotPlayer

20 Apr 2021, 06:18

I mean that you can run this script instead. Would it meet your needs?
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

20 Apr 2021, 06:22

mikeyww wrote:
20 Apr 2021, 06:18
I mean that you can run this script instead. Would it meet your needs?
N-Nope c.c I need the gui to be a child of the potplayer window so it moves with it and all that fun stuff.
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: SetParent vs PotPlayer

20 Apr 2021, 09:43

OK. Perhaps others will know how to solve the puzzle.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

20 Apr 2021, 10:11

Not all programs respond nicely to setparent I've learned. I don't have a working example but you could try creating an invisible GUI that covers PotPlayer and set this to be owned by PotPlayer. Then you can try to attach all the child windows you want to your own GUI. You'd have to use wineventhook to determine when PotPlayer is moved but it will minimize/stay on top with +owner
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

20 Apr 2021, 10:26

Playing around with something like this:

Code: Select all


Create_Overlay(hwnd)
{
	global Overlay_GUI

	wingetpos, X, Y, W, H, % "ahk_id " hwnd
	
	Gui, Overlay:+LastFoundExist
	IfWinExist
		gui, Overlay: Destroy
	gui, Overlay: -caption -Border -SysMenu +hwndOverlay_GUI +Owner%hwnd%
	gui, Overlay: Margin , 0, 0
	Gui, Overlay: Color, FF0000, FF0000
	Gui, Overlay: +LastFound                                     
	WinSet, TransColor, FF0000

	gui, Overlay: show, x%X% y%Y% w%w% h%H%
	
}

^t::
Create_Overlay(winexist("A"))
return

^r::
gui, Child_GUI: +Parent%Overlay_GUI%
gui, Child_GUI: show, w200 h200 x0 y0
return


First create the overlay with ctrl+t on your window. Then create the child with ctrl+r on the overlay.
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

20 Apr 2021, 10:46

@doubledave22
Sadly i really need to use SetParent. Thanks for your time though!!

What i learned though:
I know for a fact that if i Gui, Main: Hide or if i setparent the gui to desktop or something else before closing the potplayer window, it'll work as intended (it'll appear on the next potplayer window)
if i close the pot window while the gui is in it, the gui just gets yeeted to the shadow realm and i can't do anything to it.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

20 Apr 2021, 14:10

MiM wrote:
20 Apr 2021, 10:46
@doubledave22
Sadly i really need to use SetParent. Thanks for your time though!!

What i learned though:
I know for a fact that if i Gui, Main: Hide or if i setparent the gui to desktop or something else before closing the potplayer window, it'll work as intended (it'll appear on the next potplayer window)
if i close the pot window while the gui is in it, the gui just gets yeeted to the shadow realm and i can't do anything to it.
the line: gui, Child_GUI: +Parent%Overlay_GUI% is the same as the setparent dll call btw. If you attach a child window to a parent window then the parent window is closed, as far as I know there's no way to keep the child window alive for the next time the parent is made. You should consider your last resort option of re-making the child window whenever the parent is close/remade.

edit: is the issue you can't detect when potplayer closes? Wineventhook can do this as well as a simple timer to check if potplayer exists
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

20 Apr 2021, 14:45

doubledave22 wrote:
20 Apr 2021, 14:10
edit: is the issue you can't detect when potplayer closes? Wineventhook can do this as well as a simple timer to check if potplayer exists
O.O Can i somehow detect when window is closing with wineventhook, return 0 on that, run Gui, Main: Hide and then WinClose? if that's possible then i'll take it gladly.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

20 Apr 2021, 15:07

There's 3 methods I know of to detect window closing.

1) Simple timer

Code: Select all

settimer, Check_Exist, 100

Check_Exist:
if !winexist("ahk_exe PotPlayer) ; use windows spy to get the correct wintitle here (im just guessing)
{
 ; code here 
}
return
2) Shellhook. Here's an example: https://www.autohotkey.com/boards/viewtopic.php?t=51265 , you will need to check for wParam = 2 for window destroyed messages then run a check if potplayer exists to see if its closed.

3) Wineventhook. You have it reading from 0x8004 to 0x8004. You can add 0x8001 (EVENT_OBJECT_DESTROY) to 0x8004 instead to catch closing messages as well. Problem with this approach is there may be a bit more filtering to be done.

In your WinEventProc() function you will need to do some filtering like converting event to hex (event := Format("0x{:x}", event) and filtering for 0x8001 and running your closed code or 0x8004 and running your winset top code.
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

20 Apr 2021, 15:14

@doubledave22
Before i jump into it, are you sure i can cancel the initial window close? since that's what i need for this solution to work.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

20 Apr 2021, 16:14

MiM wrote:
20 Apr 2021, 15:14
@doubledave22
Before i jump into it, are you sure i can cancel the initial window close? since that's what i need for this solution to work.
No, these methods just alert you when the window has been closed. I don't know of any ways to block a window from being closed. You should be able to use any of these methods to react accordingly when it has closed though. Maybe someone else can help on that front or ultimately you can tell us what your end goal is and we can try to find another option for you
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: SetParent vs PotPlayer

20 Apr 2021, 16:29

It seemed like if you add one WinMove command to what I wrote, the result would match the described need, but I could be missing something.
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer

21 Apr 2021, 03:57

Bleh, i'll just re-create the gui every time the potplayer window closes, seems like this is an impossible issue to fix and that's the best work-around.

Appreciate if someone brainstorms this though:
MiM wrote:
20 Apr 2021, 04:42
2.You see that winevent hook? i made it because the gui window would get "behind" the potplayer window when the potplayer window is resized and on a couple other occasions.
you can see what happens if you just delete WinSet, Top, , ahk_id %PotGuiHwnd% line from the wineventproc function. Any way to make the gui "always above" instead of constantly firing WinSet, Top, , ahk_id %PotGuiHwnd%?
User avatar
MiM
Posts: 61
Joined: 20 Apr 2021, 04:31

Re: SetParent vs PotPlayer  Topic is solved

21 Apr 2021, 14:20

Figured it out!
Had to setparent to "null" after potplayer window closes with

Code: Select all

DllCall("SetParent", ptr, PotGuiHwnd, ptr, 0)
(specifically the 0 at the end), otherwise the gui would just not accept another setparent call.

Credit to the one and only, Lexikos.
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: SetParent vs PotPlayer

21 Apr 2021, 15:21

Cool! glad you solved it. I rarely settle for a solution that isn't the exact desired outcome I want anymore. Glad you stuck with it

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 218 guests