这段脚本实现了动态修改窗口图标的功能。它创建了一个记事本窗口,并调用自定义函数 ChangeWindowIcon 修改其标题栏图标。图标可以从 .ico 文件、.dll.icl 图标库中加载。

 

Plain text
复制到剪贴板
Open code in new window
EnlighterJS 3 Syntax Highlighter
#NoEnv
SetBatchLines -1
SetWorkingDir %A_ScriptDir%
Run Notepad
WinWait, ahk_class Notepad
ChangeWindowIcon("shell32.dll", WinExist("ahk_class Notepad"), 204)
Return
; 修改窗口标题图标的函数
ChangeWindowIcon(IconFile, hWnd:="", IconNumber:=1, IconSize:=128) {
; ico图标文件IconNumber和IconSize不用填,如果是icl图标库需要填
hWnd := hWnd ? hWnd : WinExist("A")
if (!hWnd)
Return "窗口不存在!"
if not IconFile~="\.ico$"
hIcon := LoadIcon(IconFile, IconNumber, IconSize)
else
hIcon := DllCall("LoadImage", "uint", 0, "str", IconFile, "uint", 1, "int", 0, "int", 0, "uint", LR_LOADFROMFILE:=0x10)
if (!hIcon)
Return "图标文件不存在!"
SendMessage, WM_SETICON:=0x80, ICON_SMALL2:=0, hIcon,, ahk_id %hWnd% ; 设置窗口的小图标
; SendMessage, STM_SETICON:=0x0170, hIcon, 0,, Ahk_ID %hWnd%
SendMessage, WM_SETICON:=0x80, ICON_BIG:=1, hIcon,, ahk_id %hWnd% ; 将窗口的大图标设置为同一个
}
; 获取exe/dll/icl文件中指定图标找返回
LoadIcon(Filename, IconNumber, IconSize) {
if DllCall("PrivateExtractIcons", "str", Filename, "int", IconNumber-1, "int", IconSize, "int", IconSize
, "ptr*", hIcon, "uint*", 0, "uint", 1, "uint", 0, "ptr")
Return hIcon
}
#NoEnv SetBatchLines -1 SetWorkingDir %A_ScriptDir% Run Notepad WinWait, ahk_class Notepad ChangeWindowIcon("shell32.dll", WinExist("ahk_class Notepad"), 204) Return ; 修改窗口标题图标的函数 ChangeWindowIcon(IconFile, hWnd:="", IconNumber:=1, IconSize:=128) { ; ico图标文件IconNumber和IconSize不用填,如果是icl图标库需要填 hWnd := hWnd ? hWnd : WinExist("A") if (!hWnd) Return "窗口不存在!" if not IconFile~="\.ico$" hIcon := LoadIcon(IconFile, IconNumber, IconSize) else hIcon := DllCall("LoadImage", "uint", 0, "str", IconFile, "uint", 1, "int", 0, "int", 0, "uint", LR_LOADFROMFILE:=0x10) if (!hIcon) Return "图标文件不存在!" SendMessage, WM_SETICON:=0x80, ICON_SMALL2:=0, hIcon,, ahk_id %hWnd% ; 设置窗口的小图标 ; SendMessage, STM_SETICON:=0x0170, hIcon, 0,, Ahk_ID %hWnd% SendMessage, WM_SETICON:=0x80, ICON_BIG:=1, hIcon,, ahk_id %hWnd% ; 将窗口的大图标设置为同一个 } ; 获取exe/dll/icl文件中指定图标找返回 LoadIcon(Filename, IconNumber, IconSize) { if DllCall("PrivateExtractIcons", "str", Filename, "int", IconNumber-1, "int", IconSize, "int", IconSize , "ptr*", hIcon, "uint*", 0, "uint", 1, "uint", 0, "ptr") Return hIcon }
#NoEnv
SetBatchLines -1
SetWorkingDir %A_ScriptDir%

Run Notepad
WinWait, ahk_class Notepad

ChangeWindowIcon("shell32.dll", WinExist("ahk_class Notepad"), 204)
Return

; 修改窗口标题图标的函数
ChangeWindowIcon(IconFile, hWnd:="", IconNumber:=1, IconSize:=128) {
  ; ico图标文件IconNumber和IconSize不用填,如果是icl图标库需要填
  hWnd := hWnd ? hWnd : WinExist("A")
  if (!hWnd)
    Return "窗口不存在!"
  if not IconFile~="\.ico$"
    hIcon := LoadIcon(IconFile, IconNumber, IconSize)
  else
    hIcon := DllCall("LoadImage", "uint", 0, "str", IconFile, "uint", 1, "int", 0, "int", 0, "uint", LR_LOADFROMFILE:=0x10)
  if (!hIcon)
    Return "图标文件不存在!"
  SendMessage, WM_SETICON:=0x80, ICON_SMALL2:=0, hIcon,, ahk_id %hWnd%  ; 设置窗口的小图标
  ; SendMessage, STM_SETICON:=0x0170, hIcon, 0,, Ahk_ID %hWnd%
  SendMessage, WM_SETICON:=0x80, ICON_BIG:=1, hIcon,, ahk_id %hWnd%  ; 将窗口的大图标设置为同一个
}

; 获取exe/dll/icl文件中指定图标找返回
LoadIcon(Filename, IconNumber, IconSize) {
  if DllCall("PrivateExtractIcons", "str", Filename, "int", IconNumber-1, "int", IconSize, "int", IconSize
        , "ptr*", hIcon, "uint*", 0, "uint", 1, "uint", 0, "ptr")
    Return hIcon
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。