WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WM_SetIcon Message to Change Icon

 Keywords:  Change set Icon WM_SETICON LoadIcon LoadImage SendMessage SendMessageA DllCall Custom Ico LoadImage

Question:

Has anyone used the WM_SETICON message to alter the icon of a running program instance? I have a bunch of instances of a third party program that I want to mark with individual icons to help users keep them straight.

Answer:

;Example to change a running window's icon        Only tested on WinXP  ....IFICantBYTE
Wnd = AskItemlist("Pick a window to change the running icon of",WinItemizeEx("",@TRUE,@FALSE,0),@TAB,@SORTED,@SINGLE,@TRUE) ; Returns the full name of window(s) matching a partial window name.
WinShow(Wnd)
WinActivate(Wnd)
Message("OK - we are ready","We didn't have to activate the window, we just did in this example so we can see it in action - Watch the window's icon and push OK to continue")

hWnd = DllHwnd(Wnd) ; Obtains a window handle for use in DllCall’s when required.
;hWnd = DllHwnd("") ; "" is this WinBatch window

;Msg_ID
WM_SETICON = 128 ; H80

;wParam
;The system displays the large icon in the ALT+TAB dialog, and the small icon in the window caption.
ICONSMALL = 0 ; the small icon for the window.

User32 = StrCat(DirWindows(1),"User32.dll")
OIC_WARNING = 32515
OIC_ERROR = 32513
hNewIcon1=DllCall(User32,long:"LoadIconA",lpnull,long:OIC_WARNING);get a system icon handle
hNewIcon2=DllCall(User32,long:"LoadIconA",lpnull,long:OIC_ERROR);get another system icon

; Alternate the icon a few times for effect
For Loop = 1 To 5
   DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:hNewIcon1) ;Change the icon
   TimeDelay(0.5)
   DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:hNewIcon2) ;Change it again
   TimeDelay(0.5)
Next Loop

;The following is a quick solution to reset the window and it's taskbar window back to what it originally was - I'm sure there are more elegant ways, but this works on XP at least.
;Sending a NULL icon handle seems to reset the window back to the "default" icon it had (without using some other dll call to work it out in the first place)
DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:0)
;Hiding and Showing the window makes the taskbar redraw the window's icon here too.
WinHide(Wnd)
WinShow(Wnd)

User Reply:

I'd seen that code and it does work well. Pretty cool effect, for certain purposes. However, I'd like to control that icon, not just pick from the already defined set of six icons. I need four custom icons (which are easy enough to make) but I can't see how to change the code to specify my ico file. .

Answer:

This is the SetIcon example changed now to include a LoadImage UDF. The UDF is basically just a call to the Win32 API LoadImage function (plus a lot of explaining and examples because it is a tricky DLLCall to use - made my head spin when I first tried to use it!) Hope it helps you - you should now be able to load your custom icons.
;-------------------------------------------------------------------------------------------------------------------------------------------;
;LoadImage : Load an Image into memory and return a handle to it. (BMPS, Icons, Cursors)     - (Usefull with ImageList UDFs to fill them)   ;
;v1.2 by ....IFICantBYTE                                                                                                                    ;
; >>>>  !  >>>>      See the various examples at the bottom... it's then easier to understand what the details below are explaining!        ;
;-------------------------------------------------------------------------------------------------------------------------------------------;
;hinst     : Handle to an instance of the module that contains the image to be loaded.
;              (May have to use LoadLibrary to get a handle for resources in DLL and EXE files)
;            To load an OEM image (system icons, or stand-alone BMP files etc), set this parameter to zero.
;lpszName  : Specifies the image to load.
;            If hinst is NON-NULL and fuLoad omits LR_LOADFROMFILE, lpszName specifies the image resource in the hinst module.
;            If the image resource is to be loaded by name, then lpszName is a pointer to a null-terminated string that contains the name
;            of the image resource.
;            If the image resource is to be loaded by ordinal, use the MAKEINTRESOURCE macro **(see below) to convert the image ordinal into
;            a form that can be passed to the LoadImage function.
;      NOTE: In my tests, I've found that I can just pass a string to this for either a filename or a resource ordinal.
;            For a resource in a DLL or EXE, you need to put a hash # in front of the ordinal number... eg. "#28"
;            I've also found that in the case of icons, it is talking about the Icon resource's Group Name in the file, not the individual one.
;
;         ** The following is talking about a MACRO in MSD VB&.NET environment not Winbatch, but taken from MSD web site for info purposes.
;            [ To pass these constants to the LoadImage function, use the MAKEINTRESOURCE macro.                                             ]
;            [ For example, to load the OCR_NORMAL cursor, pass MAKEINTRESOURCE(OCR_NORMAL) as the lpszName parameter and NULL as the hinst  ]
;            [ parameter.                                                                                                                    ]
;         - I think this Macro is just converting a known OEM resource name to it's integer value and doing something to pass back as
;                 two 16bit Words? - High and Low values or a string if it doesn't know it?? - Whatever, passing a string as I previously said
;                 seems to work.
;
;            If the hinst parameter is NULL and the fuLoad parameter omits the LR_LOADFROMFILE value, the lpszName specifies the OEM image
;            to load.
;            The OEM image identifiers are defined in Winuser.h and have the following prefixes.
;            OBM_ OEM bitmaps
;            OIC_ OEM icons
;            OCR_ OEM cursors
;         - Again, use their decimal values as a string with a hash in front. eg. OIC_INFORMATION = 32516, so use "#32516"
;
;            If the fuLoad parameter includes the LR_LOADFROMFILE value, lpszName is the name of the file that contains the image
;            (such as a stand-alone BMP file - include the path if needed).
;
;uType     : The type of image to be loaded. This parameter can be one of the following values.
;            IMAGE_BITMAP = 0 Loads a bitmap.
;            IMAGE_CURSOR = 2 Loads a cursor.
;            IMAGE_ICON = 1 Loads an icon.
;cxDesired : The width, in pixels, of the icon or cursor.
;            If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function uses the SM_CXICON or SM_CXCURSOR system
;            metric value to set the width.
;            If this parameter is zero and LR_DEFAULTSIZE is not used, the function uses the actual resource width.
;cyDesired : The height, in pixels, of the icon or cursor.
;            If this parameter is zero and the fuLoad parameter is LR_DEFAULTSIZE, the function uses the SM_CYICON or SM_CYCURSOR system
;            metric value to set the height.
;            If this parameter is zero and LR_DEFAULTSIZE is not used, the function uses the actual resource height.
;fuLoad    : This parameter can be one or more of the following values:
;     NOTE : Specifying the correct flags is important for things to work - especially when to use or omit LR_LOADFROMFILE and LR_SHARED
;            LR_DEFAULTCOLOR = 0        The default flag - it does nothing. All it means is "not LR_MONOCHROME".
;            LR_CREATEDIBSECTION = 8192 When the uType parameter specifies IMAGE_BITMAP, causes the function to return a DIB section bitmap
;                                       rather than a compatible bitmap. This flag is useful for loading a bitmap without mapping it to the
;                                       colors of the display device.
;            LR_DEFAULTSIZE = 64        Uses the width or height specified by the system metric values for cursors or icons, if the
;                                       cxDesired or cyDesired values are set to zero. If this flag is not specified and cxDesired and
;                                       cyDesired are set to zero, the function uses the actual resource size.
;                                       If the resource contains multiple images, the function uses the size of the first image.
;            LR_LOADFROMFILE = 16       Loads the image from the file specified by the lpszName parameter. If this flag is not specified,
;                                       lpszName is the name of the resource.
;            LR_LOADMAP3DCOLORS = 4096  Searches the color table for the image and replaces the following shades of gray with the
;                                       corresponding 3-D color:
;                                       Color Replaced with
;                                       Dk Gray, RGB(128,128,128) COLOR_3DSHADOW
;                                       Gray, RGB(192,192,192) COLOR_3DFACE
;                                       Lt Gray, RGB(223,223,223) COLOR_3DLIGHT
;                                NOTE : Do not use this option if you are loading a bitmap with a color depth greater than 8bpp.(256 colors)
;            LR_LOADTRANSPARENT = 32    Retrieves the color value of the first pixel in the image and replaces the corresponding entry in
;                                       the color table with the default window color (COLOR_WINDOW).
;                                       All pixels in the image that use that entry become the default window color. This value applies
;                                       only to images that have corresponding color tables.
;                                NOTE : Do not use this option if you are loading a bitmap with a color depth greater than 8bpp.
;
;                                NOTE : If fuLoad includes both the LR_LOADTRANSPARENT and LR_LOADMAP3DCOLORS values, LRLOADTRANSPARENT
;                                       takes precedence.
;                                       However, the color table entry is replaced with COLOR_3DFACE rather than COLOR_WINDOW.
;
;            LR_MONOCHROME = 1          Loads the image in black and white.
;            LR_SHARED = 32768          Shares the image handle if the image is loaded multiple times.
;                                       If LR_SHARED is not set, a second call to LoadImage for the same resource will load the image again
;                                       and return a different handle.
;                                       When you use this flag, the system will destroy the resource when it is no longer needed.
;                                NOTE : Do not use LR_SHARED for images that have non-standard sizes, that may change after loading,
;                                       or that are loaded from a file.
;                                       BUT when loading a system icon or cursor, you MUST use LR_SHARED or the function will fail to load
;                                       the resource.
;                                       Under Windows 95/98/Me: The function finds the first image with the requested resource name in the
;                                       cache, regardless of the size requested.
;
;            LR_VGACOLOR = 128          Uses true VGA colors.
;-----------------------------------------------------------------------------------------------------------------------------------------------
;Returns   : Handle of the newly loaded image if successful or NULL otherwise.
;-----------------------------------------------------------------------------------------------------------------------------------------------
;Example syntax :
;  hBMP1 = LoadImage(0,"C:\MyPicture.BMP",0,0,0,16|64) ;load a stand-alone BMP file into RAM at it's default dimensions and get a handle to it.
;  hBMP2 = LoadImage(0,"C:\MyPicture.BMP",0,300,100,1|16) ;load the BMP file into RAM as a monochrome picture at 300x100 pixels
;  hIcon1 = LoadImage(0,"#32516",1,16,16,32768);Load a system icon (in this case 32516 = the OIC_INFORMATION icon) - Note the LR_SHARED flag
;  hIcon2 = LoadImage(0,"C:\Program Files\WinBatch\Icons\grayowl2.ico",1,16,16,16);load a valid ico file icon at size 16x16 - Note LR_LOADFROMFILE also = 16
;How to load a packaged resource inside another file: (in this case first an icon then a bmp then a cursor)
;  DLLorEXEfile = "C:\Windows\System32\ieframe.dll"
;  hInstOfFile = DllCall(StrCat(DirWindows(1),"Kernel32.dll"),long:"LoadLibraryA",lpstr:DLLorEXEfile)
;  IconNameinDLLorEXE = "#191" ;Favorite Yellow Star in ieframe.dll - find it with a resource viewing utility of some kind
;  hIcon3 = LoadImage(hInstOfFile,IconNameinDLLorEXE,1,16,16,0) ;load a DLL or EXE resource icon using the Icon Group resource name it belongs to
;  BMPNameinDLLorEXE = "SEARCH_120.BMP" ;Magnifying glass picture in ieframe.dll
;  hBMP3 = LoadImage(hInstOfFile,BMPNameinDLLorEXE,0,0,0,64) ;load a resource BMP at default size by it's Name (if number use a hash eg. "#1234") - Note uType = 0 for BMP
;  CURNameinDLLorEXE = "#103"
;  hCursor1 = LoadImage(hInstOfFile,CURNameinDLLorEXE,2,0,0,64) ;get a handle to a cursor resource in the EXE or DLL - Note uType for Cursor = 2
;Note: If you used LoadLibraryA, then it's probably a good idea to free the loaded file after using it with:
;  DllCall(hKernel32,long:"FreeLibrary", long:hInstOfFile)
;-----------------------------------------------------------------------------------------------------------------------------------------------
; All those usage notes really come down to this... (feel free to delete the notes if you think you don't need them!)
#DefineFunction LoadImage(hinst,lpszName,uType,cxDesired,cyDesired,fuLoad)
User32=StrCat(DirWindows(1),"user32.dll")
Return DllCall(User32,long:"LoadImageA",long:hinst,lpstr:lpszName,long:uType,long:cxDesired,long:cyDesired,long:fuLoad)
#EndFunction
;##########################################################################################################################################




;Example to change a running window's icon using various icons via my LoadImage UDF        ....IFICantBYTE
Wnd = AskItemlist("Pick a window to change the running icon of",WinItemizeEx("",@TRUE,@FALSE,0),@TAB,@SORTED,@SINGLE,@TRUE) ; Returns the full name of window(s) matching a partial window name.
WinShow(Wnd)
WinActivate(Wnd)
Message("OK - we are ready","We didn't have to activate the window, we just did in this example so we can see it in action - Watch the window's icon and push OK to continue")

hWnd = DllHwnd(Wnd) ; Obtains a window handle for use in DllCall’s when required.
;hWnd = DllHwnd("") ; "" is this WinBatch window

;Msg_ID
WM_SETICON = 128 ; H80

;wParam
;The system displays the large icon in the ALT+TAB dialog, and the small icon in the window caption.
ICONSMALL = 0 ; the small icon for the window.

User32 = StrCat(DirWindows(1),"User32.dll")
Kernel32 = StrCat(DirWindows(1),"Kernel32.dll")

OIC_WARNING = "#32515"

DLLorEXEfile = "C:\Program Files\WinBatch\System\Winbatch Studio.exe" ; We will pick on the WinBatch Studio exe in this example - it has a few icons in it
IconNameInDLLorEXE = "#20521" ; (ordinal with a # in front) - use a resource viewer of some kind to find the information

hInstOfFile = DllCall(Kernel32,long:"LoadLibraryA",lpstr:DLLorEXEfile); load the file into RAM and get a reference to it - we should free it later (is there a simpler WinBatch way for all this?)

;use LoadImage UDF to load the icons
hNewIcon1 = LoadImage(0,OIC_WARNING,1,16,16,32768);Load a system icon
hNewIcon2 = LoadImage(0,"C:\Program Files\WinBatch\Icons\grayowl2.ico",1,16,16,16);load a valid ico file
hNewIcon3 = LoadImage(hInstOfFile,IconNameinDLLorEXE,1,16,16,0);load a dll or exe resource icon using the Icon Group resource name it belongs to

; Alternate the icon a few times for effect
For Loop = 1 To 5
   DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:hNewIcon1) ;Change the icon
   TimeDelay(0.5)
   DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:hNewIcon2) ;Change it again
   TimeDelay(0.5)
   DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:hNewIcon3) ;Change it again
   TimeDelay(0.5)
Next Loop

;Unload the DLL or EXE file we used - just for good measure - it probably gets freed when the script exits anyway?
DllCall(Kernel32,long:"FreeLibrary", long:hInstOfFile)

;The following is a quick solution to reset the window and it's taskbar window back to what it originally was - I'm sure there are more elegant ways, but this works on XP at least.
;Sending a NULL icon handle seems to reset the window back to the "default" icon it had (without using some other dll call to work it out in the first place)
DllCall(User32,long:"SendMessageA",long:hWnd,long:WM_SETICON,long:ICONSMALL,long:0)
;Hiding and Showing the window makes the taskbar redraw the window's icon here too.
WinHide(Wnd)
WinShow(Wnd)

Article ID:   W17799
Filename:   WM_SetIcon Message to Change Icon.txt
File Created: 2011:05:13:08:21:32
Last Updated: 2011:05:13:08:21:32