SetIcon in Menu does not display icon

Issue #29 closed
Winter Laite created an issue

Code to test:

FileMenu := Menu()
FileMenu.Add("Script Icon", "MenuHandler")
FileMenu.Add("Suspend Icon", "MenuHandler")
FileMenu.Add("Pause Icon", "MenuHandler")
FileMenu.SetIcon("Script Icon", A_AhkPath, 2) ; 2nd icon group from the file
FileMenu.SetIcon("Suspend Icon", A_AhkPath, -206) ; icon with resource ID 206
FileMenu.SetIcon("Pause Icon", A_AhkPath, -207) ; icon with resource ID 207

MyMenuBar := MenuBar()
MyMenuBar.Add("&File", FileMenu)
MyGui := Gui()
MyGui.MenuBar := MyMenuBar
MyGui.Add("Edit", "w200 h200")
btn := MyGui.Add("Button",, "Exit This Example")
btn.OnEvent("Click", "MyGui_Close")
MyGui.Show()

MenuHandler() {
    MsgBox("You clicked an item.")
    ; For this example, the menu items don't do anything.
}

MyGui_Close() {
    ExitApp()
}

Comments (6)

  1. Matt Feemster repo owner

    The problem here is that you are trying to get an icon from a .NET DLL using the same method (icon index) we use to retrieve from native DLLs.

    I have added the ability to pass a string for the 3rd parameter in SetIcon(). Internally, when it sees you’ve passed a string, it assumes you are trying to look at a .NET DLL and tries to find an icon resource with that name in the DLL.

    I have also added a new accessor named A_KeysharpCorePath, which gives the full path to the Keysharp.Core.dll. This is the file you want to retrieve the icons from.

    Please test and let me know if you think this is an acceptable workaround.

    Also, please verify that I didn’t break the existing icon loading functionality.

  2. Matt Feemster repo owner

    Here is some test code where you can get an icon resource from the Keysharp.Core.dll file using the string name of the resource.

    FileMenu := Menu()
    FileMenu.Add("Script Icon", "MenuHandler")
    FileMenu.Add("Suspend Icon", "MenuHandler")
    FileMenu.Add("Pause Icon", "MenuHandler")
    FileMenu.SetIcon("Script Icon", A_KeysharpCorePath, "Keysharp.ico")
    FileMenu.SetIcon("Suspend Icon", A_KeysharpCorePath, "Keysharp_s.ico")
    FileMenu.SetIcon("Pause Icon", A_KeysharpCorePath, "Keysharp_p.ico")
    
    MyMenuBar := MenuBar()
    MyMenuBar.Add("&File", FileMenu)
    MyGui := Gui()
    MyGui.MenuBar := MyMenuBar
    MyGui.Add("Edit", "w200 h200")
    btn := MyGui.Add("Button",, "Exit This Example")
    btn.OnEvent("Click", "MyGui_Close")
    MyGui.Show()
    
    MenuHandler() {
        MsgBox("You clicked an item.")
        ; For this example, the menu items don't do anything.
    }
    
    MyGui_Close() {
        ExitApp()
    }
    

  3. Winter Laite reporter

    Working, also working with system icons, see below.

    FileMenu := Menu()
    FileMenu.Add("System", "MenuHandler")
    FileMenu.Add("Script Icon", "MenuHandler")
    FileMenu.Add("Suspend Icon", "MenuHandler")
    FileMenu.Add("Pause Icon", "MenuHandler")
    FileMenu.SetIcon("System", "Shell32.dll", 174) ; 2nd icon group from the file
    FileMenu.SetIcon("Script Icon", A_KeysharpCorePath, "Keysharp.ico")
    FileMenu.SetIcon("Suspend Icon", A_KeysharpCorePath, "Keysharp_s.ico")
    FileMenu.SetIcon("Pause Icon", A_KeysharpCorePath, "Keysharp_p.ico")
    
    MyMenuBar := MenuBar()
    MyMenuBar.Add("&File", FileMenu)
    MyGui := Gui()
    MyGui.MenuBar := MyMenuBar
    MyGui.Add("Edit", "w200 h200")
    btn := MyGui.Add("Button",, "Exit This Example")
    btn.OnEvent("Click", "MyGui_Close")
    MyGui.Show()
    
    MenuHandler() {
        MsgBox("You clicked an item.", "Icon Test")
        ; For this example, the menu items don't do anything.
    }
    
    MyGui_Close() {
        ExitApp()
    }
    

    Please mark as resolved and close. Thank you!

  4. Matt Feemster repo owner

    Marking as resolved/closed. Please including something like this in your test suite so that we can verify that retrieving icons from native and .NET DLLs works.

  5. Log in to comment