#Requires AutoHotkey v2.0
/*****************************************************************************************
 * Namespace: SetIcon
 * Tested with:  AHK 2.1-alpha.10
 * Tested on:  Win 11 (x64)
 * Function compiled by https://github.com/buliasz/AHKv2-Gdip/blob/master/Gdip_All.ahk
 * Changelog: 2014-5-21/v1.2.3/Nephren゜
 * Optimizing code,The taskbar icon is not displayed, cancel the taskbar full merge
 * MyGui := Gui()
 * MyGUi.show("w300 h300")
 * Text := "abc.jpg" ;png,gif,bmp,ico
 * Text := 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' ;Base64 Transparent
 * Text := A_WinDir '\explorer.exe'
 * Text := "H"
 * SetIcon(MyGUi.hwnd, Text)
 * SetIcon(MyGUi.hwnd, Text, 0xFFF3F300, 0xFF000000) ;'0xFF' + 'FFFFFF'
 ****************************************************************************************/

MyGui := Gui()
MyGUi.show("w300 h300")
; Text := "abc.jpg" ;png,gif,bmp,ico
; Text := 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' ;Base64 Transparent
; Text := A_WinDir '\explorer.exe'
Text := "H"
SetIcon(MyGUi.hwnd, Text)
SetIcon(MyGUi.hwnd, Text, 0xFFF3F300, 0xFF000000) ;'0xFF' + 'FFFFFF'

SetIcon(hwnd, str, BGColor := 0xFFF3F3F3, FontColor := 0xFF000000, TrayShow := 1, TitleShow := 1, TaskShow := 1, ClickShow := 0) {
  if (!DllCall("LoadLibrary", "str", "gdiplus", "UPtr"))
    return Msgbox(Error("Could not load GDI+ library"))
  si := Buffer(A_PtrSize = 8 ? 24 : 16, 0), NumPut("UInt", 1, si)
  DllCall("gdiplus\GdiplusStartup", "UPtr*", &pToken := 0, "UPtr", si.Ptr, "UPtr", 0)
  (pToken) || MsgBox("Gdiplus failed to start. Please ensure you have gdiplus on your system") ExitApp()
  (!ClickShow) || Persistent(), Tray := A_TrayMenu, A_TrayMenu.ClickCount := 1, Tray.Add(),
    Tray.Add("Menu_Show", (*) => (G := GuiFromHwnd(hwnd)) ? G.Show() : ""), Tray.Default := "Menu_Show"
  switch {
    case FileExist(str):
      SplitPath str, &OutFileName, &OutDir, &OutExtension, &OutNameNoExt
      if OutExtension ~= "exe|dll" {
        NHpic1 := LoadPicture(str, "w120 h120"), hIcon := LoadPicture(str, "w120 h120 Icon1", &isicon)
      } else {
        try {
          DllCall("gdiplus\GdipCreateBitmapFromFile", "UPtr", StrPtr(str), "UPtr*", &FpBitmap := 0)
          DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "UPtr", FpBitmap, "UPtr*", &NHpic1 := 0, "Int", Background := 0xffffffff)
          DllCall("gdiplus\GdipCreateHICONFromBitmap", "UPtr", FpBitmap, "UPtr*", &hIcon := 0)
        } catch {
          return MsgBox("Gdiplus failed to start.")
        }
      }
    case StrLen(str) > 50 && str ~= "^[a-zA-z0-9=/]+$":
      if !((DllCall("crypt32\CryptStringToBinary", "UPtr", StrPtr(str), "UInt", 0, "UInt", 0x01, "UPtr", 0, "UInt*", &DecLen := 0, "UPtr", 0, "UPtr", 0)) &&
        (DllCall("crypt32\CryptStringToBinary", "UPtr", StrPtr(str), "UInt", 0, "UInt", 0x01, "UPtr", (Dec := Buffer(DecLen, 0)).Ptr, "UInt*", &DecLen, "UPtr", 0, "UPtr", 0))
        && (pStream := DllCall("shlwapi\SHCreateMemStream", "UPtr", Dec.Ptr, "UInt", DecLen, "UPtr")))
        return MsgBox("Gdiplus failed to start.")
      DllCall("gdiplus\GdipCreateBitmapFromStreamICM", "UPtr", pStream, "Ptr*", &FpBitmap := 0), ObjRelease(pStream)
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "UPtr", FpBitmap, "UPtr*", &NHpic1 := 0, "Int", Background := 0xffffffff)
      DllCall("gdiplus\GdipCreateHICONFromBitmap", "UPtr", FpBitmap, "UPtr*", &hIcon := 0)
    case StrLen(Text := SubStr(str, 1, 1)):
      DllCall("gdiplus\GdipCreateBitmapFromScan0", "Int", 128, "Int", 128, "Int", 0, "Int", 0x26200A, "UPtr", 0, "UPtr*", &NpBitmap := 0)
      DllCall("gdiplus\GdipGetImageGraphicsContext", "UPtr", NpBitmap, "UPtr*", &pGraphics := 0)
      DllCall("gdiplus\GdipSetSmoothingMode", "UPtr", pGraphics, "Int", 2)
      IsNumber(BGColor) || (!(BGColor ~= "i)0x\w{8}") ? BGColor := "0x" BGColor : "")
      DllCall("gdiplus\GdipCreateSolidFill", "UInt", BGColor, "UPtr*", &pBrush := 0)
      DllCall("gdiplus\GdipCreateRegion", "UInt*", &Region := 0)
      DllCall("gdiplus\GdipGetClip", "UPtr", pGraphics, "UInt", Region), x := 0, y := 0, r := 20, w := 128, h := 128
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x - r, "Float", y - r, "Float", 2 * r, "Float", 2 * r, "Int", 4)
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x + w - r, "Float", y - r, "Float", 2 * r, "Float", 2 * r, "Int", 4)
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x - r, "Float", y + h - r, "Float", 2 * r, "Float", 2 * r, "Int", 4)
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x + w - r, "Float", y + h - r, "Float", 2 * r, "Float", 2 * r, "Int", 4)
      DllCall("gdiplus\GdipFillRectangle", "UPtr", pGraphics, "UPtr", pBrush, "Float", x, "Float", y, "Float", w, "Float", h)
      DllCall("gdiplus\GdipSetClipRegion", "UPtr", pGraphics, "UPtr", Region, "Int", 0)
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x - (2 * r), "Float", y + r, "Float", w + (4 * r), "Float", h - (2 * r), "Int", 4)
      DllCall("gdiplus\GdipSetClipRect", "UPtr", pGraphics, "Float", x + r, "Float", y - (2 * r), "Float", w - (2 * r), "Float", h + (4 * r), "Int", 4)
      DllCall("gdiplus\GdipFillEllipse", "UPtr", pGraphics, "UPtr", pBrush, "Float", x, "Float", y, "Float", 2 * r, "Float", 2 * r)
      DllCall("gdiplus\GdipFillEllipse", "UPtr", pGraphics, "UPtr", pBrush, "Float", x + w - (2 * r), "Float", y, "Float", 2 * r, "Float", 2 * r)
      DllCall("gdiplus\GdipFillEllipse", "UPtr", pGraphics, "UPtr", pBrush, "Float", x, "Float", y + h - (2 * r), "Float", 2 * r, "Float", 2 * r)
      DllCall("gdiplus\GdipFillEllipse", "UPtr", pGraphics, "UPtr", pBrush, "Float", x + w - (2 * r), "Float", y + h - (2 * r), "Float", 2 * r, "Float", 2 * r)
      DllCall("gdiplus\GdipSetClipRegion", "UPtr", pGraphics, "UPtr", Region, "Int", 0)
      DllCall("gdiplus\GdipDeleteRegion", "UPtr", Region), DllCall("gdiplus\GdipDeleteBrush", "UPtr", pBrush)
      DllCall("gdiplus\GdipCreateFontFamilyFromName", "UPtr", StrPtr("Arial"), "UInt", 0, "UPtr*", &hFamily := 0)
      (hFamily) || MsgBox("The font you have specified does not exist on the system") ExitApp()
      DllCall("gdiplus\GdipCreateFontFamilyFromName", "UPtr", StrPtr(Font := "Arial"), "UInt", 0, "UPtr*", &hFamily := 0)
      DllCall("gdiplus\GdipCreateFont", "UPtr", hFamily, "Float", 120, "Int", 1, "Int", 0, "UPtr*", &hFont := 0)
      DllCall("gdiplus\GdipCreateStringFormat", "Int", 0x4000, "Int", 0, "UPtr*", &hFormat := 0)
      DllCall("gdiplus\GdipCreateSolidFill", "UInt", FontColor, "UPtr*", &pBrush := 0)
      NumPut("Float", 0, "Float", 0, "Float", 128, "Float", 128, RC := Buffer(16))
      DllCall("gdiplus\GdipSetStringFormatAlign", "UPtr", hFormat, "Int", 1)
      DllCall("gdiplus\GdipSetTextRenderingHint", "UPtr", pGraphics, "Int", RenderingHint := 4)
      DllCall("gdiplus\GdipMeasureString", "UPtr", pGraphics, "UPtr", StrPtr(Text), "Int", -1, "UPtr", hFont, "UPtr", RC.Ptr, "UPtr", hFormat, "UPtr", (RC2 := Buffer(16)).Ptr, "uint*", &Chars := 0, "uint*", &Lines := 0)
      ReturnRC := RC2.Ptr ? NumGet(RC2, 0, "Float") "|" NumGet(RC2, 4, "Float") "|" NumGet(RC2, 8, "Float") "|" NumGet(RC2, 12, "Float") "|" Chars "|" Lines : 0
      ReturnRC := DllCall("gdiplus\GdipDrawString", "UPtr", pGraphics, "UPtr", StrPtr(Text), "Int", -1, "UPtr", hFont, "UPtr", RC.Ptr, "UPtr", hFormat, "UPtr", pBrush)
      DllCall("gdiplus\GdipDeleteBrush", "UPtr", pBrush)
      DllCall("gdiplus\GdipDeleteStringFormat", "UPtr", hFormat)
      DllCall("gdiplus\GdipDeleteFont", "UPtr", hFont)
      DllCall("gdiplus\GdipDeleteFontFamily", "UPtr", hFamily)
      DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "UPtr", NpBitmap, "UPtr*", &NHpic1 := 0, "Int", Background := 0xffffffff)
      DllCall("gdiplus\GdipCreateHICONFromBitmap", "UPtr", NpBitmap, "UPtr*", &hIcon := 0)
      DllCall("gdiplus\GdipDeleteGraphics", "UPtr", pGraphics)
      DllCall("DeleteObject", "UPtr", NpBitmap)
      DllCall("gdiplus\GdiplusShutdown", "UPtr", pToken)
      hModule := DllCall("GetModuleHandle", "str", "gdiplus", "UPtr")
      (hModule) || Msgbox(Error("GDI+ library was unloaded before shutdown"))
      (DllCall("FreeLibrary", "UPtr", hModule)) || Msgbox(Error("Could not free GDI+ library"))
  }
  return (!TrayShow || TraySetIcon("HBitMap:*" NHpic1), !TitleShow || SendMessage(0x80, 0, hIcon, , hwnd), !TaskShow || SendMessage(0x80, 1, hIcon, , hwnd))
}

 

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