Setting Icon of Windows Issues (WinXP and Win7)

强颜欢笑 提交于 2019-12-13 03:04:04

问题


I'm making a Firefox addon with js-ctypes and was using user32.dll functions to set the icons of all windows of a profile.

I plan to do this for Mac OS and Linux but trying to knock out Windows first.

So I'm setting the icons like this: GitHub - Gist :: Noitidart / _ff-addon-snippet-ChangeWindowIcon.js - Rev2

That code is simplified. This code I use to apply to all windows:

Cu.import('resource://gre/modules/ctypes.jsm');

var user32 = ctypes.open('user32.dll');

var SendMessage = user32.declare('SendMessageW', ctypes.winapi_abi, ctypes.uintptr_t,
    ctypes.int32_t,
    ctypes.unsigned_int,
    ctypes.int32_t,
    ctypes.voidptr_t
);

var LoadImage = user32.declare('LoadImageA', ctypes.winapi_abi, ctypes.voidptr_t,
    ctypes.int,
    ctypes.char.ptr,
    ctypes.unsigned_int,
    ctypes.int,
    ctypes.int,
    ctypes.unsigned_int
);

var IMAGE_BITMAP = 0;
var IMAGE_ICON = 1;
var LR_LOADFROMFILE = 16;


// RUNNING STUFF BELOW - ABVOE WAS JUST DEFINING STUFF
var DOMWindows = Services.wm.getEnumerator(null);
while (DOMWindows.hasMoreElements()) {
    var aDOMWindow = DOMWindows.getNext();
    var basewindow = aDOMWindow.QueryInterface(Ci.nsIInterfaceRequestor)
        .getInterface(Ci.nsIWebNavigation)
        .QueryInterface(Ci.nsIDocShellTreeItem)
        .treeOwner
        .QueryInterface(Ci.nsIInterfaceRequestor)
        .nsIBaseWindow;
    var nativeHandle = basewindow.nativeHandle;

    var targetWindow_handle = parseInt(nativeHandle);

    var hIconBig = LoadImage(targetWindow_handle, 'C:\\Documents and Settings\\SONY VAIO\\My Documents\\Downloads\\puzzle.ico', IMAGE_ICON, 256, 256, LR_LOADFROMFILE); //MUST BE A FILEPATH TO A ICO!!!
    var hIconSmall = LoadImage(targetWindow_handle, 'C:\\Documents and Settings\\SONY VAIO\\My Documents\\Downloads\\puzzle.ico', IMAGE_ICON, 16, 16, LR_LOADFROMFILE); //MUST BE A FILEPATH TO A ICO!!!

    var successSmall = SendMessage(targetWindow_handle, 0x0080 /** WM_SETICON **/ , 0 /** ICON_SMALL **/ , hIconSmall); //if it was success it will return 0? im not sure. on first time running it, and it was succesful it returns 0 for some reason
    var successBig = SendMessage(targetWindow_handle, 0x0080 /** WM_SETICON **/ , 1 /** ICON_BIG **/ , hIconBig); //if it was success it will return 0? im not sure. on first time running it, and it was succesful it returns 0 for some reason   

}

user32.close();

The issues are as follows:

Issues On WinXP

  • When press Alt + Tab the icon is the normal one
  • If windows are lumped into one group (because of taskbar overflow) and ALL icons are changed, the lumped group icon is still not the changed one. As seen in image here:

Issues On Win7

  • If the application IS PINNED and even if all windows icons are changed to be the same, it still does not changed the pin icon
  • If the application is NOT PINNED then if change the icon for all windows it will change the icon on the taskbar HOWEVER if you right click on the icon in the taskbar it reverts to what it was normally and running the snippet above again won't set it back, to get it back to icon you set you have to pin and unpin

回答1:


You really shouldn't put so much questions into a single question...

When press Alt + Tab the icon is the normal one

You're trying to load and set 256x256 icons. XP does not support such icons. You should really add some error checking ;) IIRC you should set 32x32 icons for the big one. Or more precisely SM_CXICON and/or SM_CXSMICON

If windows are lumped into one group (because of taskbar overflow) and ALL icons are changed, the lumped group icon is still not the changed one. As seen in image here:

I think you're out of luck for this one. XP will take whatever icon is the main resource icon in the .exe IIRC.

XP is dead anyway...

Issues On Win 7

IIRC you'll have to work with System.AppUserModel.RelaunchIcon...

Edit Actually I might be wrong regarding the XP grouping stuff. Last messed around with icons on win32 quite a while ago. GCLP_HICON/GCLP_HICONSM might work.



来源:https://stackoverflow.com/questions/24028715/setting-icon-of-windows-issues-winxp-and-win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!