MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SetTrayIcon

Method SetTrayIcon

source/script.cpp:1074–1170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1072
1073
1074ResultType Script::SetTrayIcon(LPTSTR aIconFile, int aIconNumber, ToggleValueType aFreezeIcon)
1075{
1076 bool force_update = false;
1077
1078 if (aFreezeIcon != NEUTRAL) // i.e. if it's blank, don't change the current setting of mIconFrozen.
1079 {
1080 if (mIconFrozen != (aFreezeIcon == TOGGLED_ON)) // It needs to be toggled.
1081 {
1082 mIconFrozen = !mIconFrozen;
1083 force_update = true; // Ensure the icon correctly reflects the current setting and suspend/pause status.
1084 }
1085 }
1086
1087 if (*aIconFile == '*' && !aIconFile[1]) // Restore the standard icon.
1088 {
1089 if (mCustomIcon)
1090 {
1091 GuiType::DestroyIconsIfUnused(mCustomIcon, mCustomIconSmall); // v1.0.37.07: Solves reports of Gui windows losing their icons.
1092 // If the above doesn't destroy the icon, the GUI window(s) still using it are responsible for
1093 // destroying it later.
1094 mCustomIcon = NULL; // To indicate that there is no custom icon.
1095 mCustomIconSmall = NULL;
1096 free(mCustomIconFile);
1097 mCustomIconFile = NULL;
1098 mCustomIconNumber = 0;
1099 force_update = true;
1100 }
1101 aIconFile = _T(""); // Handle this like TraySetIcon(,,n) in case n was specified.
1102 }
1103
1104 if (!*aIconFile) // No icon specified, or it was already reset to default above.
1105 {
1106 if (force_update)
1107 UpdateTrayIcon(true);
1108 return OK; // We were called just to freeze/unfreeze the icon.
1109 }
1110
1111 // v1.0.43.03: Load via LoadPicture() vs. ExtractIcon() because ExtractIcon harms the quality
1112 // of 16x16 icons inside .ico files by first scaling them to 32x32 (which then has to be scaled
1113 // back to 16x16 for the tray and for the SysMenu icon). I've visually confirmed that the
1114 // distortion occurs at least when a 16x16 icon is loaded by ExtractIcon() then put into the
1115 // tray. It might not be the scaling itself that distorts the icon: the pixels are all in the
1116 // right places, it's just that some are the wrong color/shade. This implies that some kind of
1117 // unwanted interpolation or color tweaking is being done by ExtractIcon (and probably LoadIcon),
1118 // but not by LoadImage.
1119 // Also, load the icon at actual size so that when/if this icon is used for a GUI window, its
1120 // appearance in the alt-tab menu won't be unexpectedly poor due to having been scaled from its
1121 // native size down to 16x16.
1122
1123 if (aIconNumber == 0) // Must validate for use in two places below.
1124 aIconNumber = 1; // Must be != 0 to tell LoadPicture that "icon must be loaded, never a bitmap".
1125
1126 int image_type;
1127 // L17: For best results, load separate small and large icons.
1128 HICON new_icon_small;
1129 HICON new_icon = NULL; // Initialize to detect failure to load either icon.
1130 HMODULE icon_module = NULL; // Must initialize because it's not always set by LoadPicture().
1131 if (!_tcsnicmp(aIconFile, _T("HICON:"), 6) && aIconFile[6] != '*')

Callers 1

BIF_DECLFunction · 0.80

Calls 3

ATOI64Function · 0.85
LoadPictureFunction · 0.85
RuntimeErrorMethod · 0.80

Tested by

no test coverage detected