Windows 11 Overflow

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Windows 11 Overflow

Post by jrachr » 24 Jan 2023, 07:38

Good day.Came across this interesting script that I am trying to get to work. While the overflow in windows 11 is exactly what this script seems to be for I am trying to further educate myself in the use and works of Ahk. Does anyone out there know why this is not working It is supposed to toggle the overflow icons of the running apps on and off. Thank you


Code: Select all

;#NoTrayIcon
#NoEnv
SetBatchLines, -1
Menu, Tray, Icon, C:\Users\Jr\Desktop\Icons\4-DeepPurple\H.ico

NumpadPgDn:: 
   HideShowTrayIcon("Razer Central.exe")
  
  
   Return

HideShowTrayIcon(exeName) {
   static IconInfo := {}
   if res := GetTrayIconByProcName(exeName) {
      IconInfo[exeName] := res
      ShowIcon(res, false)
   }
   else {
      if IconInfo.HasKey(exeName)
         ShowIcon(IconInfo[exeName], true)
      else
         MsgBox, Tray icon of %exeName% not found
   }
}

ShowIcon(Info, showHide) {
   static WM_TASKBARCREATED := DllCall("RegisterWindowMessage", "Str", "TaskbarCreated")
   if !showHide
      TrayIconActions("delete",, Info.hwnd, Info.id)
   else {
      if hIcon := DllCall("CopyIcon", "Ptr", Info.hIcon, "Ptr")
         TrayIconActions("add", hIcon, Info.hwnd, Info.id, Info.msg)
      else
         DllCall("PostMessage", "Ptr", Info.hwnd, "UInt", WM_TASKBARCREATED, "Ptr", 0, "Ptr", 0)
   }
}

GetTrayIconByProcName(procName) {
   static TB_GETBUTTON   := 0x417
        , TB_BUTTONCOUNT := 0x418
        , PtrSize := 4 << A_Is64bitOS
        , szTBBUTTON := 8 + PtrSize*3
        , szTRAYDATA := 16 + PtrSize*2
   
   prevDHW := A_DetectHiddenWindow
   DetectHiddenWindows, On
   lastFound := WinExist()
   WinGet, PID, PID, ahk_exe explorer.exe
   RemoteBuff := new RemoteBuffer(PID, szTRAYDATA)
   VarSetCapacity(TBBUTTON, szTBBUTTON)
   VarSetCapacity(TRAYDATA, szTRAYDATA)
   found := false
   Loop 2 {
      if (A_Index = 2)
         ControlGet, hToolBar, hwnd,, ToolbarWindow321, ahk_class NotifyIconOverflowWindow
      else {
         for k, v in ["TrayNotifyWnd", "SysPager", "ToolbarWindow32"]
            hToolBar := DllCall("FindWindowEx", "Ptr", k = 1 ? WinExist("ahk_class Shell_TrayWnd") : hToolBar, "Ptr", 0, "Str", v, "UInt", 0, "Ptr")
      }
      WinExist("ahk_id" . hToolBar)
      SendMessage, TB_BUTTONCOUNT
      Loop % ErrorLevel {
         SendMessage, TB_GETBUTTON, A_Index - 1, RemoteBuff.ptr
         RemoteBuff.Read(TBBUTTON, szTBBUTTON)
         RemoteBuff.Read(TRAYDATA, szTRAYDATA, NumGet(&TBBUTTON + 8 + PtrSize) - RemoteBuff.ptr)
         WinGet, processName, ProcessName, % "ahk_id" hWnd := NumGet(TRAYDATA)
         if (processName = procName) {
            found := true
            uID := NumGet(&TRAYDATA + PtrSize, "UInt")
            msg := NumGet(&TRAYDATA + 4 + PtrSize, "UInt")
            hIcon := NumGet(&TRAYDATA + 16 + PtrSize, PtrSize = 4 ? "UInt" : "UInt64")
            break 2
         }
      }
   }
   DetectHiddenWindows, %prevDHW%
   WinExist("ahk_id" . lastFound)
   Return found ? {hwnd: hWnd, id: uID, msg: msg, hIcon: hIcon} : ""
}

TrayIconActions(command := "delete", hIcon := "", hWnd := "", uID := 0x404, nMsg := 0x404)
{
   static NIF_MESSAGE := 1, NIF_ICON := 2, NIM_ADD := 0, NIM_MODIFY := 1, NIM_DELETE := 2
   (hWnd = "" && hWnd := A_ScriptHwnd)
   uFlags := (command = "add" ? NIF_MESSAGE : 0) | (command != "delete" ? NIF_ICON : 0)
   action :=  command = "add" ? NIM_ADD : command = "modify" ? NIM_MODIFY : NIM_DELETE
   
   VarSetCapacity(NOTIFYICONDATA, size := A_PtrSize = 8 ? 848 : A_IsUnicode? 828 : 444, 0)
   NumPut(size, NOTIFYICONDATA, "UInt")
   NumPut(hWnd, NOTIFYICONDATA, A_PtrSize)
   NumPut(uID,  NOTIFYICONDATA, A_PtrSize*2)
   ( command = "add" && NumPut(nMsg, NOTIFYICONDATA, A_PtrSize*2 + 8, "UInt") )
   if (command != "delete")  {
      NumPut(uFlags, NOTIFYICONDATA, A_PtrSize*2 + 4, "UInt")
      NumPut(hIcon,  NOTIFYICONDATA, A_PtrSize*3 + 8)
   }
   DllCall("shell32\Shell_NotifyIcon", UInt, action, Ptr, &NOTIFYICONDATA)
}

class RemoteBuffer
{
   __New(PID, size) {
      static flags := (PROCESS_VM_OPERATION := 0x8) | (PROCESS_VM_WRITE := 0x20) | (PROCESS_VM_READ := 0x10)
           , Params := ["UInt", MEM_COMMIT := 0x1000, "UInt", PAGE_READWRITE := 0x4, "Ptr"]
         
      if !this.hProc := DllCall("OpenProcess", "UInt", flags, "Int", 0, "UInt", PID, "Ptr")
         throw Exception("Can't open remote process PID = " . PID . "`nA_LastError: " . A_LastError, "RemoteBuffer.__New")
      
      if !this.ptr := DllCall("VirtualAllocEx", "Ptr", this.hProc, "Ptr", 0, "Ptr", size, Params*) {
         DllCall("CloseHandle", "Ptr", this.hProc)
         throw Exception("Can't allocate memory in remote process PID = " . PID . "`nA_LastError: " . A_LastError, "RemoteBuffer.__New")
      }
   }
   
   __Delete() {
      DllCall("VirtualFreeEx", "Ptr", this.hProc, "Ptr", this.ptr, "UInt", 0, "UInt", MEM_RELEASE := 0x8000)
      DllCall("CloseHandle", "Ptr", this.hProc)
   }
   
   Read(ByRef localBuff, size, offset = 0) {
      VarSetCapacity(localBuff, size, 0)
      if !DllCall("ReadProcessMemory", "Ptr", this.hProc, "Ptr", this.ptr + offset, "Ptr", &localBuff, "Ptr", size, "PtrP", bytesRead)
         throw Exception("Can't read data from remote buffer`nA_LastError: " . A_LastError, "RemoteBuffer.Read")
      Return bytesRead
   }
   
   Write(pData, size, offset = 0) {
      if !res := DllCall("WriteProcessMemory", "Ptr", this.hProc, "Ptr", this.ptr + offset, "Ptr", pData, "Ptr", size, "PtrP", bytesWritten)
         throw Exception("Can't write data to remote buffer`nA_LastError: " . A_LastError, "RemoteBuffer.Write")
      Return bytesWritten
   }
}

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Windows 11 Overflow

Post by jNizM » 25 Jan 2023, 02:22

@teadrinker should be your code
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 25 Jan 2023, 08:04

@jNizM Hi there. Please excuse my ignorance but I really don't understand what you mean by this @teadrinker code comment and have no idea how to proceed. Please help. Thank you.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Windows 11 Overflow

Post by jNizM » 25 Jan 2023, 10:05

I linked teadrinker because that looks like his code. Maybe he can help you with that.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 25 Jan 2023, 10:36

Understood. So should I send message or will it get to him somehow. Excuse my ignorance with how forum works. IE. If @teadrinker is there has a message already gone to him. Tk's

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 25 Jan 2023, 12:12

@jNizM. Belay previous jNizM. Should learn to read a little closer. Just realized your first post was to teadrinker and not me(duhhhhhhhhh). Will wait to see if I hear from him. Tk's

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Windows 11 Overflow

Post by teadrinker » 25 Jan 2023, 16:31

jNizM wrote: Maybe he can help you with that
Unfortunately, I don't have Windows 11.

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 25 Jan 2023, 17:09

@teadrinker. Understood. As I said I just wanted to have something to play with so I could better learn ahk a little more. So if I just wanted to hide a running app icon that was buried in the systray(overflow as Windows 11 calls it now) this cannot be done just like #notrayicon does with running ahk scripts? Tk's again.At least now I know.

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Windows 11 Overflow

Post by JoeWinograd » 26 Jan 2023, 14:41

jrachr wrote:Does anyone out there know why this is not working...
My guess is that your W11 is Build 22623, where that code does not work (I haven't tested it, but it likely works in W11 Build 22621 and all previous W11 builds, as well as all W10 builds and maybe even all Windows versions going back to XP). These forum links discuss the issue in detail:

viewtopic.php?f=76&t=111306
viewtopic.php?f=23&t=111398
viewtopic.php?f=6&t=1229&sid=34a4d6275e260f4de41654da9c2a3473&start=80#p494711

This post by @lexikos explains the problem:
viewtopic.php?f=23&t=111398#p499459

I haven't studied all the code that you posted, but a quick look shows that it relies on the Shell_TrayWnd and NotifyIconOverflowWindow toolbars, which are the issues that lexikos discussed, and likely explains why it isn't working for you. Regards, Joe

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 26 Jan 2023, 16:35

@JoeWinograd. Thanks for info Joe. However my build is 22621.1105 and not 623. So it has to be something I am doing wrong. Any ideas would be helpful.Maybe I have to put class or full path?

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Windows 11 Overflow

Post by JoeWinograd » 26 Jan 2023, 18:06

jrachr wrote:However my build is 22621.1105 and not 623. So it has to be something I am doing wrong.
Correct! You are doing something wrong. It works perfectly here in W11 Build 22621. It is able to toggle Hide/Show of the icon in both the main tray and the overflow tray. It also works in W10. But, of course, it fails in 22623.
jrachr wrote:Maybe I have to put class or full path?
No! Just the file name of the .exe is all you need in the HideShowTrayIcon call.
jrachr wrote:Any ideas would be helpful.
Two ideas: (1) Razer Central.exe is not running. (2) NumLock is on, which means you're sending Numpad3, not NumpadPgDn. Regards, Joe

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 26 Jan 2023, 18:43

@JoeWinograd So the following works. However for whatever reason the dsatray.exe.(Intel Driver support utility. Not even sure I need it.All it does is check for new drivers or updates.Suppose I could do that on my own once in a while.) comes up can't find dsatrayicon. The one thing of interest.If there is already an exe hidden and you hide something it will come up exe not found and the only way to get it back is to restart explorer.No big deal. Just remember to unhide before adding anything new and reloading.Also you mention it also works for on taskbar icons? How do I hide say the windows icon?

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Windows 11 Overflow

Post by JoeWinograd » 26 Jan 2023, 19:23

jrachr wrote:So the following works.
I don't understand what "following" works.

> However for whatever reason the dsatray.exe.(Intel Driver support utility.

Something missing in that partial sentence.

> If there is already an exe hidden and you hide something it will come up exe not found and the only way to get it back is to restart explorer.

I'm guessing what you mean is: "If there is already an icon hidden for one .exe and you try to hide the icon for a different .exe, the latter results in Tray icon of <.exe file name> not found." I haven't tried that.

> Also you mention it also works for on taskbar icons?

No, I didn't mention that. It works on system tray (aka notification area) icons.

> How do I hide say the windows icon?

What do you mean by "the windows icon"?

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 26 Jan 2023, 19:51

@JoeWinograd. Lol.My bad. Forgot to add script that is why you don't understand half my message. DUH. So just so I got this.I have a running app(Razer Central.exe) which hides/shows just fine. If I add another exe (hideShowIcon("DSATray.exe") and reload it should now hi

Code: Select all

d
e/show both? Well for some reason it only hides the razer Central.exe but doesn't hide DSATray.exe . What I meant by the windows button was the start button on the taskbar but if this only hides /shows overflow then it doesn't matter. Off the mark do you know if there is a way yet to hide volume/network icons bottom right ? Tk's again for all your help.

Code: Select all

;#NoTrayIcon
#NoEnv
SetBatchLines, -1
Menu, Tray, Icon, C:\Users\Jr\Desktop\Icons\4-DeepPurple\H.ico

F11:: 
 
   HideShowTrayIcon("Razer Central.exe")
   HideShowTrayIcon("DSATray.exe")
   Return

HideShowTrayIcon(exeName) {
   static IconInfo := {}
   if res := GetTrayIconByProcName(exeName) {
      IconInfo[exeName] := res
      ShowIcon(res, false)
   }
   else {
      if IconInfo.HasKey(exeName)
         ShowIcon(IconInfo[exeName], true)
      else
         MsgBox, Tray icon of %exeName% not found
   }
}

ShowIcon(Info, showHide) {
   static WM_TASKBARCREATED := DllCall("RegisterWindowMessage", "Str", "TaskbarCreated")
   if !showHide
      TrayIconActions("delete",, Info.hwnd, Info.id)
   else {
      if hIcon := DllCall("CopyIcon", "Ptr", Info.hIcon, "Ptr")
         TrayIconActions("add", hIcon, Info.hwnd, Info.id, Info.msg)
      else
         DllCall("PostMessage", "Ptr", Info.hwnd, "UInt", WM_TASKBARCREATED, "Ptr", 0, "Ptr", 0)
   }
}

GetTrayIconByProcName(procName) {
   static TB_GETBUTTON   := 0x417
        , TB_BUTTONCOUNT := 0x418
        , PtrSize := 4 << A_Is64bitOS
        , szTBBUTTON := 8 + PtrSize*3
        , szTRAYDATA := 16 + PtrSize*2
   
   prevDHW := A_DetectHiddenWindow
   DetectHiddenWindows, On
   lastFound := WinExist()
   WinGet, PID, PID, ahk_exe explorer.exe
   RemoteBuff := new RemoteBuffer(PID, szTRAYDATA)
   VarSetCapacity(TBBUTTON, szTBBUTTON)
   VarSetCapacity(TRAYDATA, szTRAYDATA)
   found := false
   Loop 2 {
      if (A_Index = 2)
         ControlGet, hToolBar, hwnd,, ToolbarWindow321, ahk_class NotifyIconOverflowWindow
      else {
         for k, v in ["TrayNotifyWnd", "SysPager", "ToolbarWindow32"]
            hToolBar := DllCall("FindWindowEx", "Ptr", k = 1 ? WinExist("ahk_class Shell_TrayWnd") : hToolBar, "Ptr", 0, "Str", v, "UInt", 0, "Ptr")
      }
      WinExist("ahk_id" . hToolBar)
      SendMessage, TB_BUTTONCOUNT
      Loop % ErrorLevel {
         SendMessage, TB_GETBUTTON, A_Index - 1, RemoteBuff.ptr
         RemoteBuff.Read(TBBUTTON, szTBBUTTON)
         RemoteBuff.Read(TRAYDATA, szTRAYDATA, NumGet(&TBBUTTON + 8 + PtrSize) - RemoteBuff.ptr)
         WinGet, processName, ProcessName, % "ahk_id" hWnd := NumGet(TRAYDATA)
         if (processName = procName) {
            found := true
            uID := NumGet(&TRAYDATA + PtrSize, "UInt")
            msg := NumGet(&TRAYDATA + 4 + PtrSize, "UInt")
            hIcon := NumGet(&TRAYDATA + 16 + PtrSize, PtrSize = 4 ? "UInt" : "UInt64")
            break 2
         }
      }
   }
   DetectHiddenWindows, %prevDHW%
   WinExist("ahk_id" . lastFound)
   Return found ? {hwnd: hWnd, id: uID, msg: msg, hIcon: hIcon} : ""
}

TrayIconActions(command := "delete", hIcon := "", hWnd := "", uID := 0x404, nMsg := 0x404)
{
   static NIF_MESSAGE := 1, NIF_ICON := 2, NIM_ADD := 0, NIM_MODIFY := 1, NIM_DELETE := 2
   (hWnd = "" && hWnd := A_ScriptHwnd)
   uFlags := (command = "add" ? NIF_MESSAGE : 0) | (command != "delete" ? NIF_ICON : 0)
   action :=  command = "add" ? NIM_ADD : command = "modify" ? NIM_MODIFY : NIM_DELETE
   
   VarSetCapacity(NOTIFYICONDATA, size := A_PtrSize = 8 ? 848 : A_IsUnicode? 828 : 444, 0)
   NumPut(size, NOTIFYICONDATA, "UInt")
   NumPut(hWnd, NOTIFYICONDATA, A_PtrSize)
   NumPut(uID,  NOTIFYICONDATA, A_PtrSize*2)
   ( command = "add" && NumPut(nMsg, NOTIFYICONDATA, A_PtrSize*2 + 8, "UInt") )
   if (command != "delete")  {
      NumPut(uFlags, NOTIFYICONDATA, A_PtrSize*2 + 4, "UInt")
      NumPut(hIcon,  NOTIFYICONDATA, A_PtrSize*3 + 8)
   }
   DllCall("shell32\Shell_NotifyIcon", UInt, action, Ptr, &NOTIFYICONDATA)
}

class RemoteBuffer
{
   __New(PID, size) {
      static flags := (PROCESS_VM_OPERATION := 0x8) | (PROCESS_VM_WRITE := 0x20) | (PROCESS_VM_READ := 0x10)
           , Params := ["UInt", MEM_COMMIT := 0x1000, "UInt", PAGE_READWRITE := 0x4, "Ptr"]
         
      if !this.hProc := DllCall("OpenProcess", "UInt", flags, "Int", 0, "UInt", PID, "Ptr")
         throw Exception("Can't open remote process PID = " . PID . "`nA_LastError: " . A_LastError, "RemoteBuffer.__New")
      
      if !this.ptr := DllCall("VirtualAllocEx", "Ptr", this.hProc, "Ptr", 0, "Ptr", size, Params*) {
         DllCall("CloseHandle", "Ptr", this.hProc)
         throw Exception("Can't allocate memory in remote process PID = " . PID . "`nA_LastError: " . A_LastError, "RemoteBuffer.__New")
      }
   }
   
   __Delete() {
      DllCall("VirtualFreeEx", "Ptr", this.hProc, "Ptr", this.ptr, "UInt", 0, "UInt", MEM_RELEASE := 0x8000)
      DllCall("CloseHandle", "Ptr", this.hProc)
   }
   
   Read(ByRef localBuff, size, offset = 0) {
      VarSetCapacity(localBuff, size, 0)
      if !DllCall("ReadProcessMemory", "Ptr", this.hProc, "Ptr", this.ptr + offset, "Ptr", &localBuff, "Ptr", size, "PtrP", bytesRead)
         throw Exception("Can't read data from remote buffer`nA_LastError: " . A_LastError, "RemoteBuffer.Read")
      Return bytesRead
   }
   
   Write(pData, size, offset = 0) {
      if !res := DllCall("WriteProcessMemory", "Ptr", this.hProc, "Ptr", this.ptr + offset, "Ptr", pData, "Ptr", size, "PtrP", bytesWritten)
         throw Exception("Can't write data to remote buffer`nA_LastError: " . A_LastError, "RemoteBuffer.Write")
      Return bytesWritten
   }
}

User avatar
JoeWinograd
Posts: 2198
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Windows 11 Overflow

Post by JoeWinograd » 26 Jan 2023, 20:21

jrachr wrote:If I add another exe (hideShowIcon("DSATray.exe") and reload it should now hide/show both?
I haven't studied the code, but I'm sure that it operates only on the .exe specified in the HideShowTrayIcon call, i.e., it does not affect other icons. So, my guess is...NO, it should not "now hide/show both".

> What I meant by the windows button was the start button on the taskbar

Even without studying the code, I'm sure it cannot show/hide the Start button, as it operates only on icons in the system tray (aka notification area). I don't even know if it's possible to show/hide the Start button.

> do you know if there is a way yet to hide volume/network icons

I don't know.

Regards, Joe

jrachr
Posts: 543
Joined: 01 Mar 2021, 17:33

Re: Windows 11 Overflow

Post by jrachr » 26 Jan 2023, 20:26

@JoeWinograd. Understood.Thank You.

Post Reply

Return to “Ask for Help (v1)”