how to auto-hide a gui when the gui window is inactive? thanks Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

how to auto-hide a gui when the gui window is inactive? thanks

18 Mar 2024, 22:35

how to auto-hide a gui when the gui window is inactive? thanks

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance force

item := ["aaa","bbb","ccc"]                  ; Menu items
myGui := Gui()
myGui.OnEvent("Escape", GuiEscape)
myGui.OnEvent("Close", GuiEscape)
myGui.SetFont("s12")

ogcListViewLV := myGui.Add("ListView", "vLV r12 w250 -Hdr", ["11","22222222222"])
ogcListViewLV.OnEvent("Itemfocus", FocusFile)
ogcButtonOK := myGui.Add("Button", "xm ym Hidden default", "OK")
ogcButtonOK.OnEvent("Click", ButtonOK)                 ; For pressing Enter
For each, row in item
	ogcListViewLV.Add("",A_index,  row)                                        ; Add Menu item to ListView
myGui.Title := "Menu (F3 = show again)"
showGui()                                                 ; Display the ListView

return

!F3::showGui()                  ; Alt + F3 = display ListView

showGui()     
{
  myGui.Title := "Menu (F3 = show again)"
  myGui.Show()                    
  return
}


FocusFile(GuiCtrlObj, ROW)
{
	If GetKeyState("LButton") || GetKeyState("RButton") = true
	{
		myGui.Hide()
		callone(item[ROW])
    return
	}
} 

ButtonOK(*)                                                 ; User pressed ENTER
{ 
	fc := myGui.FocusedCtrl.Name              ; Get the focused Control Name
	if (fc = "LV") {                          ; Focused Control is the ListView
		myGui.Hide()
		callone(item[ROW := ogcListViewLV.GetNext(0,"Focused")]) ; Focused row
	}
	return
} 

GuiEscape(*)
{ 
	myGui.Hide()
	return
} 


callone(gwitem)
{
  MsgBox(gwitem,"")  
}
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: how to auto-hide a gui when the gui window is inactive? thanks

19 Mar 2024, 06:59

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
g.AddText 'w230', 'Test'
Loop {
 WinWaitNotActive g
 g.Hide
 SoundBeep 1000
 WinWait g
 SoundBeep 1500
}

F3::g.Show
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: how to auto-hide a gui when the gui window is inactive? thanks

19 Mar 2024, 07:40

thank for your help,the code very nice!

The following script to press F3 twice to re-show gui window,don't know what went wrong?

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
g.AddText 'w230', 'Test'
showgui()
return

F3::showgui()

showgui()
{
  g.show
  OnMessage(0x86,ListviewGuiLostFocus)
return
}


ListviewGuiLostFocus(wParam, lParam, *) { 
  if(wParam=0)
  {
    OnMessage(0x86,emptyFun)
    g.Hide()
  }
  return
}

emptyFun(wParam, lParam, *) { 
  return
}
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: how to auto-hide a gui when the gui window is inactive? thanks

19 Mar 2024, 10:29

I have not used that message and so cannot advise you. At the least, you can see that your wParam is zero, so the GUI is hidden according to your code. A simple message box can show you when each function is called, and what the parameters' values are.

Others here may know more. Microsoft indicates the following.
WM_NCACTIVATE: Sent to a window when its nonclient area needs to be changed to indicate an active or inactive state. Processing messages related to the nonclient area of a standard window is not recommended, because the application must be able to draw all the required parts of the nonclient area for the window. If an application does process this message, it must return TRUE to direct the system to complete the change of active window. If the window is minimized when this message is received, the application should pass the message to the DefWindowProc function.
Source: WM_NCACTIVATE message (Winuser.h) - Win32 apps | Microsoft Learn
just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: how to auto-hide a gui when the gui window is inactive? thanks  Topic is solved

20 Mar 2024, 06:48

Code: Select all

    OnMessage(0x86,emptyFun)
In v2, this line doesn't replace the previously assigned message handler function, it just adds a second handler.
Try

Code: Select all

#Requires AutoHotkey v2.0

g := Gui()
g.AddText 'w230', 'Test'
showgui()
return

F3::showgui()

showgui()
{
  g.show
  OnMessage(0x86, ListviewGuiLostFocus)
return
}


ListviewGuiLostFocus(wParam, lParam, *) {
  if(wParam=0)
  {
    OnMessage(0x86, ListviewGuiLostFocus, 0) ; <<<<< remove the message handler
    g.Hide()
  }
  return
}
User avatar
Seven0528
Posts: 345
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: how to auto-hide a gui when the gui window is inactive? thanks

20 Mar 2024, 08:03

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
myGui:=Gui()
myGui.show("w240 h240")
myGui.onEvent("Escape", myGui_Escape)
onMessage(0x0006,WM_ACTIVATE)

WM_ACTIVATE(wParam, lParam, Msg, hWnd)    {
    static WA_ACTIVE:=1
        ,WA_CLICKACTIVE:=2
        ,WA_INACTIVE:=0
    switch (hWnd)
    {
        case myGui.hWnd:
            switch (lParam)
            {
                ;  case WA_ACTIVE:
                ;  case WA_CLICKACTIVE:
                case WA_INACTIVE:
                    myGui.hide()
                    return 0
            }
    }
}
myGui_Escape(guiObj)    {
    onMessage(0x0006,WM_ACTIVATE,false)
    myGui.hide()
    onMessage(0x0006,WM_ACTIVATE)
}
getMyGuiVisible()    {
    static GWL_STYLE:=-16
        ,WS_VISIBLE:=0x10000000
    style:=dllCall("User32.dll\GetWindowLong" (A_PtrSize==8?"Ptr":""), "Ptr",myGui.hWnd, "Int",GWL_STYLE, (A_PtrSize==8?"Ptr":"Int"))
    return !!(style&WS_VISIBLE)
}

F3::  {
    if (!getMyGuiVisible())    {
        onMessage(0x0006,WM_ACTIVATE,false)
        myGui.show()
        onMessage(0x0006,WM_ACTIVATE)
    }
}
 I can't shake the thought that there might be a cleaner code, but for now, here is the code that works.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Seven0528
Posts: 345
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: how to auto-hide a gui when the gui window is inactive? thanks

20 Mar 2024, 08:09

 Oops, there was already a response...
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: how to auto-hide a gui when the gui window is inactive? thanks

20 Mar 2024, 21:32

in v1 I could press the digital keystrokes(1-9) to select the the corresponding item in the gLabel of the Listview.But i am having trouble translating this to the Listview of V2.how to detect digital keystrokes in a Listview control of v2?thanks

Code: Select all

#Requires AutoHotkey v1.1.33
#SingleInstance force

item := ["aaa","bbb","ccc","ddd","eee"]                  ; Menu items

Gui Add, ListView, gExpansion AltSubmit vLV -Hdr, 11|22222222222
For each, row in item
 LV_Add("",A_index,  row)                     ; Add menu item to ListView
Gui Show                                      ; Display the ListView
return

F3::Gui Show

Expansion:
Switch A_GuiEvent {
 Case "Normal":                                           ; User clicked on a row
  Gui Hide
  MsgBox 0, Click, % item[ROW := A_EventInfo]
 Case "K":                                                ; User pressed a non-ENTER key
  key := GetKeyName(Format("vk{:x}", VK := A_EventInfo))
  If (key = "Space") {                                    ; User pressed SPACE
   Gui Hide
   Row :=  LV_GetNext(0, "Focused")
   MsgBox 0, Space, % item[Row]
  }
  
  If (vk > 48 and vk < 58) {                                    ; User pressed Digital1-9
   Gui Hide
   MsgBox 0, Space, % item[vk-48]
  }
  
  If (vk > 96 and vk < 106) {                                    ; User pressed Digital1-9
   Gui Hide
   MsgBox 0, Space, % item[vk-96]
  }
}
Return
User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: how to auto-hide a gui when the gui window is inactive? thanks

20 Mar 2024, 21:55

Code: Select all

#Requires AutoHotkey v2.0
txt := ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
g   := Gui(, 'Test')
g.SetFont 's10'
LV := g.AddListView('-Hdr', [1, 2])
LV.OnEvent 'ItemFocus', LV_ItemFocus
For n, item in txt
 LV.Add(, n, item)
g.Show

LV_ItemFocus(LV, item) {
 MsgBox txt[item]
}
Explained: Events
just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: how to auto-hide a gui when the gui window is inactive? thanks

21 Mar 2024, 03:46

Code: Select all

#Requires AutoHotkey v2.0
Items := ["aaa", "bbb", "ccc", "ddd", "eee"]
G1 := Gui( , "Test")
G1LV1 := g1.AddListView("-Hdr", ["11", "22222222222"])
G1LV1.OnEvent("Click", LV_Clicked) ; v1 event "Normal"
G1LV1.OnNotify(-155, LVN_KEYDOWN) ; v1 event "K"
For Idx, Txt In Items
   G1LV1.Add( , Idx, Txt)
G1.Show()

F3::G1.Show()

LV_Clicked(LV, Info) {
   G1.Hide()
   If Info
      MsgBox(Items[Info], "Click")
}

LVN_KEYDOWN(LV, NMLVKEYDOWN) {
   ; LVN_KEYDOWN -> https://learn.microsoft.com/en-us/windows/win32/controls/lvn-keydown
   ; NMLVKEYDOWN -> https://learn.microsoft.com/en-us/windows/win32/api/commctrl/ns-commctrl-nmlvkeydown
   Static SizeOfNMHDR := A_PtrSize * 3
   Local VK := NumGet(NMLVKEYDOWN, SizeOfNMHDR, "UShort")
   Local Key := GetKeyName(Format("vk{:0X}", VK))
   Local Row := 0
   Switch {
      Case (VK = 32): ; Space
         G1.Hide()
         If (Row := LV.GetNext(0, "Focused"))
            MsgBox(Items[Row], Key)
      Case (VK > 48 && VK < 58): ; 1 - 9
         G1.Hide()
         MsgBox(Items[VK - 48], Key)
      Case (VK > 96 && VK < 106): ; Numpad1 - 9
         G1.Hide()
         MsgBox(Items[VK - 96], Key)
   }
}
Explained: OnNotify
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: how to auto-hide a gui when the gui window is inactive? thanks

26 Mar 2024, 20:25

Thanks for all the help!
I modified the script above and tried to select the listview item by pressing the spacebar, it failed, I don't know how to do it in LV_ItemFocus.

Code: Select all

#Requires AutoHotkey v2.0
txt := ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
g   := Gui(, 'Test')
g.SetFont 's10'
LV := g.AddListView('-Hdr', [1, 2])
LV.OnEvent 'ItemFocus', LV_ItemFocus
	LV_ButtonOK := g.Add("Button", "xm ym Hidden default", "OK")
	LV_ButtonOK.OnEvent("Click", ButtonOK)                 ; For pressing Enter

For n, item in txt
 LV.Add(, n, item)
g.Show

LV_ItemFocus(LV, item) {
if GetKeyState("up") || GetKeyState("down") = true
  return
 g.hide
 MsgBox txt[item]
}

ButtonOK(*)      ; User pressed ENTER
{
	if (g.FocusedCtrl = LV)
	{
	g.Hide()
	msgbox(txt[ROW := LV.GetNext(0,"Focused")]) ; Focused row
	}
	return
}
gongnl
Posts: 96
Joined: 05 Jan 2015, 03:57
Location: /gongnltmp/

Re: how to auto-hide a gui when the gui window is inactive? thanks

27 Mar 2024, 06:46

After replacing the F3 hotkey of G1.show with space & s, I can't use the space bar to select the listview list item, please help me to solve it, thanks!
just me wrote:
21 Mar 2024, 03:46

Code: Select all

#Requires AutoHotkey v2.0
Items := ["aaa", "bbb", "ccc", "ddd", "eee"]
G1 := Gui( , "Test")
G1LV1 := g1.AddListView("-Hdr", ["11", "22222222222"])
G1LV1.OnEvent("Click", LV_Clicked) ; v1 event "Normal"
G1LV1.OnNotify(-155, LVN_KEYDOWN) ; v1 event "K"
For Idx, Txt In Items
   G1LV1.Add( , Idx, Txt)
G1.Show()

space & s::G1.Show()           ;<------F3::G1.Show() ,The space bar doesn't work anymore.

LV_Clicked(LV, Info) {
   G1.Hide()
   If Info
      MsgBox(Items[Info], "Click")
}

LVN_KEYDOWN(LV, NMLVKEYDOWN) {
   ; LVN_KEYDOWN -> https://learn.microsoft.com/en-us/windows/win32/controls/lvn-keydown
   ; NMLVKEYDOWN -> https://learn.microsoft.com/en-us/windows/win32/api/commctrl/ns-commctrl-nmlvkeydown
   Static SizeOfNMHDR := A_PtrSize * 3
   Local VK := NumGet(NMLVKEYDOWN, SizeOfNMHDR, "UShort")
   Local Key := GetKeyName(Format("vk{:0X}", VK))
   Local Row := 0
   Switch {
      Case (VK = 32): ; Space
         G1.Hide()
         If (Row := LV.GetNext(0, "Focused"))
            MsgBox(Items[Row], Key)
      Case (VK > 48 && VK < 58): ; 1 - 9
         G1.Hide()
         MsgBox(Items[VK - 48], Key)
      Case (VK > 96 && VK < 106): ; Numpad1 - 9
         G1.Hide()
         MsgBox(Items[VK - 96], Key)
   }
}
Explained: OnNotify

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, jamith and 141 guests