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
}
}