Jump to content

Recommended Posts

Posted

GUISetIcon seems to have a bug with regard to DPI. When a windows application utilizes an icon, it is supposed to choose the icon size which is closest to the size needed by the system from the .ICO file. TraySetIcon does not seem to have this bug.

I have created an example which shows the problem. The example creates a GUI with system DPI awareness and sets the same icon file with GUISetIcon and TraySetIcon.

You will notice that the icon in the GUI is blurry and stretched, while the same icon in the system tray is sharp as expected. 

Example: (app.ico is attached to this message)

#include <GUIConstantsEx.au3>

; System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 400)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; set GUI icon
    GUISetIcon(@ScriptDir & "\app.ico")

    ; set tray icon
    TraySetIcon(@ScriptDir & '\app.ico')

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

 

On a side note, does anyone know of any WinAPI or DLL calls that we can use to load a GUI icon so that I can avoid this bug?

app.ico

  • Solution
Posted (edited)
1 hour ago, argumentum said:

there should be one on the _WinAPI_* side of things

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WinAPIIcons.au3> ; _WinAPI_ExtractIcon
#include <SendMessage.au3> ; _SendMessage


; System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Example() ; https://www.autoitscript.com/forum/topic/213458-guiseticon-dpi-bug/#comment-1549810

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 400, 400)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; set GUI icon
;~     GUISetIcon(@ScriptDir & "\app.ico")
    _WinSetIcon($hGUI, @ScriptDir & "\app.ico")

    ; set tray icon
    TraySetIcon(@ScriptDir & '\app.ico')

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _WinSetIcon($hWnd, $sFile, $iIndex = 0, $bSmall = False) ; https://www.autoitscript.com/forum/topic/168698-changing-a-windows-icon/#findComment-1461109
  Local $WM_SETICON = 128, $ICON_SMALL = 0, $ICON_BIG = 1, $hIcon = _WinAPI_ExtractIcon($sFile, $iIndex, $bSmall)
  If Not $hIcon Then Return SetError(1, 0, 1) ; https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-seticon
  _SendMessage($hWnd, $WM_SETICON, Int(Not $bSmall), $hIcon)
  _WinAPI_DestroyIcon($hIcon)
EndFunc   ;==>_WinSetIcon

Google is your friend. Actually @Nine is :)

 image.png  vs this:  image.png.ad98671367a508b1503b3274304f2d9b.png

Edit:

#include <windows.h>

// ... inside your code, e.g., after CreateWindow or in WM_CREATE handler ...

HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON)); // Load your icon resource
if (hIcon)
{
    // Set the large icon (appears in Alt+Tab, etc.)
    SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
    
    // Set the small icon (appears in the title bar)
    SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}

 

Edited by argumentum
better ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

That is so much better. I’m just going by your screenshots right now because I won’t be able to test it until maybe an hour from now.

I am always amazed at the versatility of AutoIt. So many ways to do just about anything. Or as I believe @Nine had wrote before , “different ways to skin a cat”. :)

 

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
×
×
  • Create New...