Base64更改指定程序托盘HB图标示例.ahk
base64Code:="iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsSAAALEgHS3X78AAAAaklEQVR42mNgqLhOPhrVjA8lr3lGpual5z/+//+/88AbkjVDdF549oNkm3HqRNNcsf3Vh+9/gSRROtE0u8999PLznx+//0H0E9CJ6Wy4/t23vxDQidXPEP2EdeIKMKD+Pbe/DpoUNqoZhABlozardsWURQAAAABJRU5ErkJggg=="
hIcon:=Base64PNG_to_HICON(base64Code, 48, 48)
; hIcon:=LoadPicture("Shell32.dll", "Icon42 w48 h-1", IMAGE_ICON:=1)
Try TrayIconInfo := GetTrayIconInfo(DllCall("GetCurrentProcessId"))
if TrayIconInfo.hWnd&&hIcon
ReplaceTrayIcon(hIcon, TrayIconInfo.hWnd, TrayIconInfo.Id)
MsgBox % TrayIconInfo.hWnd&&hIcon ? "修改成功!" : "修改失败!"
Base64PNG_to_HICON(Base64PNG, W:=0, H:=0) { ; By SKAN on D094/D357 @ tiny.cc/t-36636
Local BLen:=StrLen(Base64PNG), Bin:=0, nBytes:=Floor(StrLen(RTrim(Base64PNG,"="))*3/4)
Return DllCall("Crypt32.dll\CryptStringToBinary", "Str",Base64PNG, "UInt",BLen, "UInt",1
,"Ptr",&(Bin:=VarSetCapacity(Bin,nBytes)), "UIntP",nBytes, "UInt",0, "UInt",0)
? DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True, "UInt"
,0x30000, "Int",W, "Int",H, "UInt",0, "UPtr") : 0
}
BitmapFromBase64(ByRef base64) { ; Modified from just me's ImageToInclude
hBitmap := 0
, VarSetCapacity(B64, StrLen(base64) << !!A_IsUnicode)
, B64 := base64
If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", 0, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
Return False
VarSetCapacity(Dec, DecLen, 0)
If !DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &B64, "UInt", 0, "UInt", 0x01, "Ptr", &Dec, "UIntP", DecLen, "Ptr", 0, "Ptr", 0)
Return False
; Bitmap creation adopted from "How to convert Image data (JPEG/PNG/GIF) to hBITMAP?" by SKAN
; -> http://www.autohotkey.com/board/topic/21213-how-to-convert-image-data-jpegpnggif-to-hbitmap/?p=139257
hData := DllCall("Kernel32.dll\GlobalAlloc", "UInt", 2, "UPtr", DecLen, "UPtr")
, pData := DllCall("Kernel32.dll\GlobalLock", "Ptr", hData, "UPtr")
, DllCall("Kernel32.dll\RtlMoveMemory", "Ptr", pData, "Ptr", &Dec, "UPtr", DecLen)
, DllCall("Kernel32.dll\GlobalUnlock", "Ptr", hData)
, DllCall("Ole32.dll\CreateStreamOnHGlobal", "Ptr", hData, "Int", True, "PtrP", pStream)
, hGdip := DllCall("Kernel32.dll\LoadLibrary", "Str", "Gdiplus.dll", "UPtr")
, VarSetCapacity(SI, 16, 0), NumPut(1, SI, 0, "UChar")
, DllCall("Gdiplus.dll\GdiplusStartup", "PtrP", pToken, "Ptr", &SI, "Ptr", 0)
, DllCall("Gdiplus.dll\GdipCreateBitmapFromStream", "Ptr", pStream, "PtrP", pBitmap)
, DllCall("Gdiplus.dll\GdipCreateHBITMAPFromBitmap", "Ptr", pBitmap, "PtrP", hBitmap, "UInt", 0)
;DllCall("Gdiplus.dll\GdipDisposeImage", "Ptr", pBitmap)
, DllCall("Gdiplus.dll\GdiplusShutdown", "Ptr", pToken)
, DllCall("Kernel32.dll\FreeLibrary", "Ptr", hGdip)
, DllCall(NumGet(NumGet(pStream + 0, 0, "UPtr") + (A_PtrSize * 2), 0, "UPtr"), "Ptr", pStream)
Return {hBitmap:hBitmap,pBitmap:pBitmap}
}
ReplaceTrayIcon(hIcon, hWnd, iconId) {
static flag := NIF_ICON := 2, action := NIM_MODIFY := 1
VarSetCapacity(NOTIFYICONDATA, size := A_PtrSize*4 + 8, 0)
, NumPut(size , NOTIFYICONDATA)
, NumPut(hWnd , NOTIFYICONDATA, A_PtrSize)
, NumPut(iconId, NOTIFYICONDATA, A_PtrSize*2)
, NumPut(flag , NOTIFYICONDATA, A_PtrSize*2 + 4)
, NumPut(hIcon , NOTIFYICONDATA, A_PtrSize*3 + 8)
Return DllCall("Shell32\Shell_NotifyIcon", "UInt", action, "Ptr", &NOTIFYICONDATA)
}
GetTrayIconInfo(proceccNameOrPID := "") {
static TB_GETBUTTON := 0x417
, TB_BUTTONCOUNT := 0x418
, ptrSize := 4 << A_Is64bitOS
, szTBBUTTON := 8 + ptrSize*3
, szTRAYDATA := 16 + ptrSize*2
Arr := []
DetectHiddenWindows % (dhw_prev := A_DetectHiddenWindows ? "On" : "On")
WinGet, PID, PID, ahk_exe explorer.exe
RB := new RemoteBuffer(PID, 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")
}
SendMessage, TB_BUTTONCOUNT,,,, ahk_id %hToolBar%
Loop % ErrorLevel {
SendMessage, TB_GETBUTTON, A_Index - 1, RB.ptr,, ahk_id %hToolBar%
try RB.Read(TBBUTTON, szTBBUTTON)
catch
continue
try RB.Read(TRAYDATA, szTRAYDATA, NumGet(&TBBUTTON + 8 + ptrSize) - RB.ptr)
catch
continue
tipOffset := NumGet(&TBBUTTON + 8 + ptrSize*2) - RB.ptr
try RB.Read(tip, 1024, tipOffset)
hWnd := NumGet(TRAYDATA)
WinGet, PID, PID, ahk_id %hWnd%
(PID = proceccNameOrPID && found := true)
WinGet, processName, ProcessName, ahk_id %hWnd%
(processName = proceccNameOrPID && found := true)
WinGetTitle, title, ahk_id %hWnd%
Arr.Push({ PID: PID, ProcessName: processName, ID: NumGet(&TRAYDATA + ptrSize, "UInt"), WinTitle: title
, hWnd: hWnd, Tip: tip, CallbackMessage: NumGet(&TRAYDATA + 4 + ptrSize, "UInt")
, HICON: NumGet(&TRAYDATA + 16 + ptrSize, ptrSize = 4 ? "UInt" : "UInt64") })
} until found
} until found
DetectHiddenWindows %dhw_prev%
if (proceccNameOrPID && !found)
throw "Icon of specified process not found"
Return found ? Arr.Pop() : Arr
}
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
}
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)