Show a GUI only when another Window is Active... Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Show a GUI only when another Window is Active...

20 Jan 2019, 20:40

...BUT also keep it active if GUI is active.

I have been trying to do this a long time ago.

Code: Select all

Window_Focuser:
	IfWinNotActive, ahk_exe EVOLUTIO.exe
		WinSet, AlwaysOnTop, Off, EVO_WIN
		WinSet, Bottom,, EVO_WIN
		else,
		WinSet, AlwaysOnTop, On, EVO_WIN
		return
This is what I have come up with. The problem is that I also need the GUI to be clickable. After I click it, it just becomes unstable, it disappears or gets lost and after a while, it gets back on. The GUI is also meant to be an active window, it has dropdownlists to choose, etc.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

20 Jan 2019, 20:47

Have you tried setting the window as the owner of your gui?
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 06:25

Hellbent wrote:
20 Jan 2019, 20:47
Have you tried setting the window as the owner of your gui?
Does +Owner work for GUI and 3rd party Window? I think its only for GUIs
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...  Topic is solved

21 Jan 2019, 06:33

fenchai wrote:
21 Jan 2019, 06:25
Does +Owner work for GUI and 3rd party Window? I think its only for GUIs
Yeah it works.
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 07:50

thankyou that worked! I didn't know O.O
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 08:04

fenchai wrote:
21 Jan 2019, 07:50
thankyou that worked! I didn't know O.O
You can set another window as the parent too, but caution is needed with that.
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 08:17

Hellbent wrote:
21 Jan 2019, 08:04
fenchai wrote:
21 Jan 2019, 07:50
thankyou that worked! I didn't know O.O
You can set another window as the parent too, but caution is needed with that.
what do you mean with caution? like be careful or is it another code?

Also sorry to bother but everything works BUT when I activate main window, the GUI does not get activated. (it stays at bottom) but I think owner+ works because minimizing and restoring main window will do the same for the GUI.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 08:35

fenchai wrote:
21 Jan 2019, 08:17

what do you mean with caution? like be careful or is it another code?
By caution I mean that the gui will only exist in the client area of the parent so if you were to try to move the gui out of the way off the window it will be lost in limbo, also while playing around with this a while ago I found that position of the gui can cause it to not show up at all, there are a few other things that I saw as well but can't recall them all, testing will reveal if it is a good fit.

fenchai wrote:
21 Jan 2019, 08:17
Also sorry to bother but everything works BUT when I activate main window, the GUI does not get activated. (it stays at bottom) but I think owner+ works because minimizing and restoring main window will do the same for the GUI.
I have only ever played around with this so I don't know all the ins and outs, but are you minimizing the gui its self? Because I think that it should pop up the gui otherwise, like I said though, I've only played around with doing it and haven't used it in a project yet so there could be things that I'm out of the loop on. There are work around hacks if needed though.
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 09:55

That's not good. because I am looping it every 3 seconds to follow the main window (by moving the GUI) so far it's working and it's not sending the GUI into limbo.

Also nope, I never minimize or restore the GUI. Since it is on +owner mode, whenever I minimize the main window (which I often do not do), it minimizes the gui and if I restore main window, it will restore GUI also. But it is very strange that GUI does not pop. Gui has no settings other than +toolwindow and -caption. the GUI is always besides the main window (by my moving looping code from above ☝) but when I open say notepad.exe it covers the gui even when main window is active.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 10:04

fenchai wrote:
21 Jan 2019, 09:55
That's not good. because I am looping it every 3 seconds to follow the main window (by moving the GUI) so far it's working and it's not sending the GUI into limbo.
Are you talking about doing that with the window as the parent of your gui?
If you are, then you don't need to do anything like that, the gui will follow the window on it's own, it's part of the window.
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 11:53

Hellbent wrote:
21 Jan 2019, 10:04
fenchai wrote:
21 Jan 2019, 09:55
That's not good. because I am looping it every 3 seconds to follow the main window (by moving the GUI) so far it's working and it's not sending the GUI into limbo.
Are you talking about doing that with the window as the parent of your gui?
If you are, then you don't need to do anything like that, the gui will follow the window on it's own, it's part of the window.
I tried that and it didn't work. The GUI stays at the center of the screen and in the background like bottom of every other window. also looks like owner+ does not work because GUI does not respond to parent minimize or restore actions.

The code below works well but has some problems...
  • GUI getting covered by other windows even when parent is active window.
  • When reactivating the Parent Window GUI will not show because of the problem above.
  • Looks like GUI is not able to be an active window unless activated. This is what is causing the above problems
      • Looks like Right-Clicking the GUI script Tray makes the GUI Activate to solve above problem
      Apart from that everything works!

      Code: Select all

      #NoEnv
      SetBatchLines, -1
      SendMode Input
      SetWorkingDir %A_ScriptDir%
      #SingleInstance, force
      
      ; builds menu
      Menu, Tray, NoStandard
      Menu, Tray, Icon, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\EVOLUTIO_0000.ico
      Menu, Tray, Add, About, About_B
      Menu, Tray, Add
      Menu, Tray, Add, Reload_B
      Menu, Tray, Add
      Menu, Tray, Add, Exit, Exit_B
      
      ;default variables
      EVOLUTIO_width := 1028 ; width of parent
      EVOLUTIO_height := 794 ; height of parent
      
      IfWinExist ahk_exe EVOLUTIO.exe
      	SetTimer, ExitAppIfEVOLUTIOisOff, 150 ; exits app if parent window does not exist
      	SetTimer, FocusListadeClientes, 150 ; this is something that has nothing to do with the problem please omit
      	SetTimer, Window_coordChecker, 500 ; this checks parent coords and other stuff.
      	SetTimer, Window_coordSnapper, 150 ;this checks and compares old parent coords with new parent codes, if they aren't equal, it moves the gui.
      	gosub, Initiate_Gui
      
      return
      
      Window_coordChecker:
      	WinGetPos, xpos, ypos,xw,yw,ahk_exe EVOLUTIO.exe
      	old_xpos := xpos
      	old_ypos := ypos
      	old_xw := xw
      	old_yw := yw
      	return
      
      Window_coordSnapper:
      	WinGetPos, xpos, ypos,xw,yw,ahk_exe EVOLUTIO.exe
      	If (xw != EVOLUTIO_width) & (yw != EVOLUTIO_height)
      	{
      		return ; stop condition
      	} 
      	else if (old_xpos != xpos) || (old_ypos != ypos) 
      	{
      			;~ ToolTip, %xw% and %yw%
      			moveX := xpos+1030
      			moveY := ypos+0
      			WinMove, EVO_WIN,,%moveX%,%moveY%
      			;~ ToolTip
      		} return
      
      ExitAppIfEVOLUTIOisOff:
      	IfWinNotExist ahk_exe EVOLUTIO.exe
      		ExitApp
      		return
      
      FocusListadeClientes:
      	IfWinActive, Lista de clientes
      	{
      		Send, {Tab}
      		WinWaitClose, Lista de clientes
      	} else{
      		return
      	} return
      
      Initiate_Gui:
      
      		Gui, 1: margin, 5,5
      		Gui, 1: +ToolWindow -Caption ;-SysMenu 
      		Gui, 1: font, cblack s13 bold
      		Gui, 1: Color, silver ;1A6DAE
      		Gui, 1: Add, Picture, y+10 w50 h-1 gAYUDA, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\Seller.png
      		Gui, 1: Add, DropDownList, y+1 w60 vSellerChoice Choose1, 1|11|12|13|2|22|23|33
      		Gui, 1: Add, Picture, y+10 w50 h-1 gDESCUENTO, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\discount.png
      		Gui, 1: Add, DropDownList, y+1 w60 vDiscountChoice Choose2, 10|20|25|other
      		Gui, 1: Add, Picture, y+10 w50 h-1 gFACTURAR, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\print.png
      		Gui, 1: +Owner EVOLUTIO.exe
      		
      		Gui, 1: Show, NoActivate AutoSize, EVO_WIN
      		
      return
      The rest of the script are labels so nothing important.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 19:31

fenchai wrote:
21 Jan 2019, 11:53
Hellbent wrote:
21 Jan 2019, 10:04
fenchai wrote:
21 Jan 2019, 09:55
That's not good. because I am looping it every 3 seconds to follow the main window (by moving the GUI) so far it's working and it's not sending the GUI into limbo.
Are you talking about doing that with the window as the parent of your gui?
If you are, then you don't need to do anything like that, the gui will follow the window on it's own, it's part of the window.
I tried that and it didn't work. The GUI stays at the center of the screen and in the background like bottom of every other window. also looks like owner+ does not work because GUI does not respond to parent minimize or restore actions.

The code below works well but has some problems...
  • GUI getting covered by other windows even when parent is active window.
  • When reactivating the Parent Window GUI will not show because of the problem above.
  • Looks like GUI is not able to be an active window unless activated. This is what is causing the above problems
      • Looks like Right-Clicking the GUI script Tray makes the GUI Activate to solve above problem
      Apart from that everything works!

      Code: Select all

      #NoEnv
      SetBatchLines, -1
      SendMode Input
      SetWorkingDir %A_ScriptDir%
      #SingleInstance, force
      
      ; builds menu
      Menu, Tray, NoStandard
      Menu, Tray, Icon, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\EVOLUTIO_0000.ico
      Menu, Tray, Add, About, About_B
      Menu, Tray, Add
      Menu, Tray, Add, Reload_B
      Menu, Tray, Add
      Menu, Tray, Add, Exit, Exit_B
      
      ;default variables
      EVOLUTIO_width := 1028 ; width of parent
      EVOLUTIO_height := 794 ; height of parent
      
      IfWinExist ahk_exe EVOLUTIO.exe
      	SetTimer, ExitAppIfEVOLUTIOisOff, 150 ; exits app if parent window does not exist
      	SetTimer, FocusListadeClientes, 150 ; this is something that has nothing to do with the problem please omit
      	SetTimer, Window_coordChecker, 500 ; this checks parent coords and other stuff.
      	SetTimer, Window_coordSnapper, 150 ;this checks and compares old parent coords with new parent codes, if they aren't equal, it moves the gui.
      	gosub, Initiate_Gui
      
      return
      
      Window_coordChecker:
      	WinGetPos, xpos, ypos,xw,yw,ahk_exe EVOLUTIO.exe
      	old_xpos := xpos
      	old_ypos := ypos
      	old_xw := xw
      	old_yw := yw
      	return
      
      Window_coordSnapper:
      	WinGetPos, xpos, ypos,xw,yw,ahk_exe EVOLUTIO.exe
      	If (xw != EVOLUTIO_width) & (yw != EVOLUTIO_height)
      	{
      		return ; stop condition
      	} 
      	else if (old_xpos != xpos) || (old_ypos != ypos) 
      	{
      			;~ ToolTip, %xw% and %yw%
      			moveX := xpos+1030
      			moveY := ypos+0
      			WinMove, EVO_WIN,,%moveX%,%moveY%
      			;~ ToolTip
      		} return
      
      ExitAppIfEVOLUTIOisOff:
      	IfWinNotExist ahk_exe EVOLUTIO.exe
      		ExitApp
      		return
      
      FocusListadeClientes:
      	IfWinActive, Lista de clientes
      	{
      		Send, {Tab}
      		WinWaitClose, Lista de clientes
      	} else{
      		return
      	} return
      
      Initiate_Gui:
      
      		Gui, 1: margin, 5,5
      		Gui, 1: +ToolWindow -Caption ;-SysMenu 
      		Gui, 1: font, cblack s13 bold
      		Gui, 1: Color, silver ;1A6DAE
      		Gui, 1: Add, Picture, y+10 w50 h-1 gAYUDA, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\Seller.png
      		Gui, 1: Add, DropDownList, y+1 w60 vSellerChoice Choose1, 1|11|12|13|2|22|23|33
      		Gui, 1: Add, Picture, y+10 w50 h-1 gDESCUENTO, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\discount.png
      		Gui, 1: Add, DropDownList, y+1 w60 vDiscountChoice Choose2, 10|20|25|other
      		Gui, 1: Add, Picture, y+10 w50 h-1 gFACTURAR, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\print.png
      		Gui, 1: +Owner EVOLUTIO.exe
      		
      		Gui, 1: Show, NoActivate AutoSize, EVO_WIN
      		
      return
      The rest of the script are labels so nothing important.

This won't work

Code: Select all

Gui, 1: +Owner EVOLUTIO.exe
You need to get the handle of the window you want to set as the owner.

You can do that with this

Code: Select all

The_Hwnd := WinActive()  ; you need to fill in the windows title or some other related info. See link below
https://autohotkey.com/docs/commands/WinActive.htm

Once you have the handle you use that to set the window as the owner of your gui

Code: Select all

Gui, 1: +Owner%The_Hwnd%
fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 22:23

Does +Owner need to be before or after Showing GUI? anyways I tested both before and after.

there is an error that no parent or owner exists

Code: Select all

#SingleInstance, force 

	The_Hwnd := WinActive("ahk_class Notepad")
	
		Gui, 1: margin, 5,5
		;~ Gui, 1: +ToolWindow -Caption ;-SysMenu 
		Gui, 1: font, cblack s13 bold
		Gui, 1: Color, silver ;1A6DAE
		Gui, 1: Add, Picture, y+10 w50 h-1 gnull, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\Seller.png
		Gui, 1: Add, DropDownList, y+1 w60 vSellerChoice Choose1, 1|11|12|13|2|22|23|33
		Gui, 1: Add, Picture, y+10 w50 h-1 gnull, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\discount.png
		Gui, 1: Add, DropDownList, y+1 w60 vDiscountChoice Choose2, 10|20|25|other
		Gui, 1: Add, Picture, y+10 w50 h-1 gnull, C:\Users\%A_UserName%\Google Drive\AHK\R-EVOLUTIO\print.png
		Gui, 1: +Owner%The_Hwnd%
		Gui, 1: Show, NoActivate AutoSize, EVO_WIN


return

null:
return
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 22:42

Here this should fix it.

Code: Select all

#SingleInstance, Force

IfWinNotExist, ahk_class Notepad
{
	msgbox, The window doesn't exist`nThe script will now exit
	ExitApp
}
WinGet, The_Hwnd , ID, ahk_class Notepad
Gui,1: +Owner%The_Hwnd%
Gui,1:Show,w300 h100, test

return
GuiClose:
	ExitApp
	

fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 22:54

Hellbent wrote:
21 Jan 2019, 22:42
Here this should fix it.

Code: Select all

#SingleInstance, Force

IfWinNotExist, ahk_class Notepad
{
	msgbox, The window doesn't exist`nThe script will now exit
	ExitApp
}
WinGet, The_Hwnd , ID, ahk_class Notepad
Gui,1: +Owner%The_Hwnd%
Gui,1:Show,w300 h100, test

return
GuiClose:
	ExitApp
	

Wow that worked, the GUI now pops when Parent window is on and also stays on top of the parent window. that's great! this solved my issue with my older script!

But I don't understand why you say that with this +owner, I don't need to make the GUI move in a loop. I mean this +Owner setting only works for copying minimize and restore from Parent window right? since the GUI stays in the center and does not move when the parent is moving.

by the loop, what it does is that it moves the GUI to the top right side of the parent and IF parent changes position, then GUI moves.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 23:13

fenchai wrote:
21 Jan 2019, 22:54


But I don't understand why you say that with this +owner, I don't need to make the GUI move in a loop. I mean this +Owner setting only works for copying minimize and restore from Parent window right? since the GUI stays in the center and does not move when the parent is moving.
I said parent
Parent != Owner

here is a example of setting " Scite4AutoHotkey " as the parent.

You can do this with notepad but the result is not desired as the edit field covers the gui.

Try using this on different windows and see what you get.

Code: Select all

#SingleInstance, Force

WinGet, TargetHwnd , ID , ahk_class SciTEWindow
Gui, 1: +HwndGuiHwnd
Gui, 1: Color, 222222

DllCall( "SetParent" , "uint" , GuiHwnd , "uint" , TargetHwnd )

Gui,1:Show, w300 h100, test
return
GuiClose:
*^ESC::
	ExitApp
	

fenchai
Posts: 292
Joined: 28 Mar 2016, 07:57

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 23:28

I think "uint" is supposed to be "unit"? (if left unchanged nothing happens) after I changed that the GUI shows but it's just a normal GUI, does not react to scite minimizing or restoring or moving. It's just a normal GUI
gregster
Posts: 9020
Joined: 30 Sep 2013, 06:48

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 23:35

I don't think so... Uint stands for unsigned integer: https://autohotkey.com/docs/commands/DllCall.htm#unsigned
But Unit is no valid type: https://autohotkey.com/docs/commands/DllCall.htm#types
Last edited by gregster on 21 Jan 2019, 23:36, edited 1 time in total.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Show a GUI only when another Window is Active...

21 Jan 2019, 23:36

fenchai wrote:
21 Jan 2019, 23:28
I think "uint" is supposed to be "unit"? (if left unchanged nothing happens) after I changed that the GUI shows but it's just a normal GUI, does not react to scite minimizing or restoring or moving. It's just a normal GUI
uint means unsigned integer, something that you don't normally have to worry about when using ahk.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot], gsxr1300, mikeyww, PsysimSV and 288 guests