ZDS

[R] Icon in the Windows taskbar (for non-AutoIt windows)

15 posts in this topic

#1 ·  Posted (edited)

Bonjour, Hello, My name is Glass Joe !
 
[FYI, I'm under Windows 7 Pro & Windows 8.1 Family Edition] I based my code on the WinSetIcon function brought to me by Mikell on the french AutoIt forum ( here is the [link] ). This is the example code I used (with the _WinAPI_LoadImage function in order to force the icon size) :

#include <WinAPI.au3>
ShellExecute(StringRegExpReplace(@AutoItExe, '(.+)\\[^\\]+', "$1") & "\AutoIt.chm")
Do
  Sleep(10)
Until WinExists("AutoIt Help")
$handle = WinGetHandle("AutoIt Help")
WinSetIcon($handle, @ScriptDir&"\Icone.ico")
 
Func WinSetIcon($hwnd, $file)
    Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 32, 32, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
    If Not $icon Then Return False
   _SendMessage($handle, 0x0080, 1, $icon) ; $WM_SETICON = 0x0080
   Sleep(200) ; [[Fix]]
   _WinAPI_DestroyIcon($icon)
EndFunc

And this is the screenshot concerning my problem :
http://www.autoitscript.fr/forum/download/file.php?id=3612&mode=view

As you can see, the icon I created has several sizes : 48x48, 32x32 and 16x16. All my taskbar icons are 32x32, but when the code above is executed, the icon is pixellated. The one in the upperleft corner of the window is correct, but it's the one is the taskbar that matters to me. Could you help me please !?

Thanks in advance for your help, and see you soon !
 
PS: When using a single-sized icon 48/32/16, I've got the same result : pixellated.
PS2: The green arrow icon in Mikell's example is also pixellated on each computer I used.

Attachment: The icon used with this sample code

EDIT: Problem solved, see the line [[Fix]] in the code

Icone.ico

Edited by ZDS

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

When viewed at a normal 100% view, I don't see any pixelation.  I only see pixelation when zooming in.  That is to be expected.

Your screenshot appears to be the same.

Am I missing what the problem is?

Edited by willichan

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I think you missed the problem : The icon in the taskbar at the bottom of the screen is pixellated (but when I create a GUI with GUICreate and GUISetIcon, there is not pixellisation; only when I set a new icon on a not-AutoIt window). Please zoom in to see properly the icon issue :

post-44903-0-36181200-1427413709_thumb.p

Edited by ZDS

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Yes I did (even with 256x256), the icon is set like a 16x16 in the taskbar anyway.

post-44903-0-80725100-1427446040_thumb.p

Edited by ZDS

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Next time search before post there is another thread with a possible solution and the same subject:

Edited by MyEarth

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@MyEarth : Next time, try to read what I took the trouble to write ^^

I searched, and my code is based on the very same code from Mikell (the one in your link, see the first line of the code ; Author: Mikell ).

Also, this example in your link has the same result : pixellated

Please, is there anyone with an idea of where the problem is?

Edited by ZDS

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

It looks like you are specifying that the image resource shoudl be 32x32.

 

What happens if you change

Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 32, 32, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
to

Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
---edit---

You also need to set the small icon image as well. Try this

#include <WinAPI.au3>
ShellExecute(StringRegExpReplace(@AutoItExe, '(.+)\\[^\\]+', "$1") & "\AutoIt.chm")
Do
  Sleep(10)
Until WinExists("AutoIt Help")
$handle = WinGetHandle("AutoIt Help")
WinSetIcon($handle, @ScriptDir&"\Icone.ico")

Func WinSetIcon($hwnd, $file)
    Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
    If Not $icon Then Return False
   _SendMessage($hwnd, 0x0080, 0, $icon) ; $WM_SETICON = 0x0080, $ICON_SMALL = 0
   _SendMessage($hwnd, 0x0080, 1, $icon) ; $WM_SETICON = 0x0080, $ICON_BIG = 1
   _WinAPI_DestroyIcon($icon)
EndFunc
No more pixelation on my end. Edited by willichan

Share this post


Link to post
Share on other sites

@willichan : your code works, but sometimes the icon is applied to the window only, not to the taskbar. Strange...

It is still Mikell's code. I just changed the image resource sizes to zero to use the .ICO file's specs, and added the second _SendMessage() call.

Share this post


Link to post
Share on other sites

@jguinch : You are awesome ! I just had (in my example code) to wait before calling _WinAPI_DestroyIcon($icon) ! Adding the Sleep(200) is enough to fix the problem :)

Thanks for all ! Problem solved

Share this post


Link to post
Share on other sites

Ahh neat idea changing the icon, I am using the (fixed) version of ZDS code but it is only changing the window icon not the taskbar icon.

Share this post


Link to post
Share on other sites

@ViciousXUSMC : try with willichan's code in #8 and add a Sleep(100) (or more than 100) between the 2 _SendMessage lines .

Maybe you can add $LR_SHARED is _WinAPI_LoadImage and then delete _WinAPI_DestroyIcon line (see remarks in https://msdn.microsoft.com/en-us/library/windows/desktop/ms648063%28v=vs.85%29.aspx)

Some GPI/WinApi gurus could confirm...

Share this post


Link to post
Share on other sites

@ViciousXUSMC : try with willichan's code in #8 and add a Sleep(100) (or more than 100) between the 2 _SendMessage lines .

Maybe you can add $LR_SHARED is _WinAPI_LoadImage and then delete _WinAPI_DestroyIcon line (see remarks in https://msdn.microsoft.com/en-us/library/windows/desktop/ms648063%28v=vs.85%29.aspx)

Some GPI/WinApi gurus could confirm...

 

Seems to work if I open notepad with Autoit but if I already have Notepad Open and only want to change the icon it does not work.

I experienced the same with the original script opening Autoit Help it was working and most likely because Autoit is opening it.

So This works:

#include <WinAPI.au3>
ShellExecute("notepad.exe")
Do
  Sleep(10)
Until WinExists("Untitled - Notepad")
$handle = WinGetHandle("Untitled - Notepad")
WinSetIcon($handle, @ScriptDir&"\Icone.ico")

Func WinSetIcon($hwnd, $file)
    Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
    If Not $icon Then Return False
   _SendMessage($hwnd, 0x0080, 0, $icon) ; $WM_SETICON = 0x0080, $ICON_SMALL = 0
   sleep(200)
   _SendMessage($hwnd, 0x0080, 1, $icon) ; $WM_SETICON = 0x0080, $ICON_BIG = 1
   _WinAPI_DestroyIcon($icon)
EndFunc

But if I open Notepad and then run this, it only changes the window icon.

#include <WinAPI.au3>
$handle = WinGetHandle("Untitled - Notepad")
WinSetIcon($handle, @ScriptDir&"\Icone.ico")

Func WinSetIcon($hwnd, $file)
    Local $icon = _WinAPI_LoadImage(0, $file, $IMAGE_ICON, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
    If Not $icon Then Return False
   _SendMessage($hwnd, 0x0080, 0, $icon) ; $WM_SETICON = 0x0080, $ICON_SMALL = 0
   sleep(200)
   _SendMessage($hwnd, 0x0080, 1, $icon) ; $WM_SETICON = 0x0080, $ICON_BIG = 1
   _WinAPI_DestroyIcon($icon)
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now