Jump to content

#AutoIt3Wrapper_Res_Icon_Add trouble


Recommended Posts

Hi, everyone!

I have been trying to get #AutoIt3Wrapper_Res_Icon_Add to work for over an hour and so far unsuccessfully. I tried the example script from the docs section of this website. The example script does not work for me, either. The icons are in the correct location on my computer. Does the script below work for you guys/gals?

; Add icons to the file resource table
; As no resource names are specified, they will be set automatically to 201+
#AutoIt3Wrapper_Res_Icon_Add=C:\Program Files (x86)\AutoIt3\Icons\au3.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\Program Files (x86)\AutoIt3\Icons\au3script_v10.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\Program Files (x86)\AutoIt3\Icons\au3script_v9.ico
#AutoIt3Wrapper_Res_Icon_Add=C:\Program Files (x86)\AutoIt3\Icons\filetype-blank.ico

#include <ButtonConstants.au3>

; Create the GUI
$hGUI = GUICreate("Demo icon resources")
$cButton = GUICtrlCreateButton("", 10, 10, 40, 40, $BS_ICON)
$cLabel = GUICtrlCreateLabel("", 70, 25, 200, 20)
GUISetState(@SW_SHOW, $hGUI)

; Now run through the added icons in the resource table
For $x = 201 To 204
      ; Change the tray icon
      $rc1 = TraySetIcon(@ScriptFullPath, $x)
      ; Change the icon in the button
      $rc2 = GUICtrlSetImage($cButton, @ScriptFullPath, $x)
      GUICtrlSetData($cLabel, "Icon Name: " & $x)

      TrayTip("Added icon: " & $x, "TraySetIcon rc: " & $rc1 & @CRLF & "GUICtrlSetImage rc: " & $rc2, 3)

      ; Allow time for icon to be seen
      Sleep(5000)
Next

; Delete the GUI
GUIDelete($hGUI)

On my test project (not the above code), I used reshacker and saw the icon I want to use is icon group 201. Setting the image to 201 did not work. Then, I set the name to exit.ico with the wrapper and now it does not have 201 in reshacker, now it uses the name EXIT.ICO instead. So, I used the following, which also did not work:

GUICtrlSetImage($MenuItemExit, @ScriptFullPath, "EXIT.ICO")

By the way, this is for a menu item. Perhaps I need to use something other than GuiCtrlSetImage?

Thanks for any advice!

Simplified version of my script:

#AutoIt3Wrapper_Res_Icon_Add=Y:\Exit.ico, exit.ico

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 379, 202)
$MenuItem1 = GUICtrlCreateMenu("File")
$MenuItemExit = GUICtrlCreateMenuItem("Exit", $MenuItem1)
GUICtrlSetImage($MenuItemExit, @ScriptFullPath, "EXIT.ICO")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Edited by abberration
Link to comment
Share on other sites

Specifically, the second to last post.

The function referenced in the post is now in the WinAPIGdi.au3 UDF which is in the AutoIt Include folder.

Edited by TheXman
Link to comment
Share on other sites

TheXman and Zedna, thank you for your suggestions. I have been playing with the ideas, but have run into a couple problems.

TheXman's suggestion uses _WinAPI_Create32BitHBITMAP() that requires the handle of the source icon. I do not know how to get the handle of a wrapper added icon.

Zedna's suggestion seems to use _GUIImageList_Create and _GUIImageList_AddIcon, which I have successfully used before in the past, but that was with a ListView. I decided to try using the Base64 encoder to hard code the icon into the script. I have used this before in the past with a .png, but for this project, I don't see anything like (fictional -> _GuiCtrlMenu_SetItemIcon) to set the icon and I could not find a way to do it.

I really do appreciate the feedback, but I found a simpler solution. If I cannot get a self-contained code solution, I have created a .dll with the icons (thanks, @Tlem in the French AutoIt forum! So awesome... Link) and can use ModernMenuRaw.au3 to apply them. I will continue researching ways to incorporate icons without external files, but if I cannot figure it out, the following code is working for me and is a good solution. I don't like having a lot of stray files and this reduces them to only 2 extra ones. Thanks again for everyone's help.

If NOT FileExists(@ScriptDir & "\ModernMenuRaw.au3") Then ; This file must exist on compile but not on execute.
    FileInstall("Y:\DL\AutoIt Projects\Easy Mp3\menu\ModernMenuRaw.au3", "ModernMenuRaw.au3")
EndIf

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ModernMenuRaw.au3>

$Form1 = GUICreate("Form1", 379, 202)
$MenuItem1 = GUICtrlCreateMenu("File")
$MenuItemExit = _GUICtrlCreateODMenuItem("Exit", $MenuItem1, "resources.dll", -3) ; If resources.dll does not exist, no errors - just no icons.
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Edit: In the past, I have successfully incorporated a huge UDF into a script without having to do a #include, and if I have to, I will do that with ModernMenuRaw.au3 - and if I do so, I will definitely give 100% credit to Holger for the code. I will also give credit to anyone else that contributes code or functionality that helps my project. 

Edited by abberration
Added recognition to Holger for his GUI/Tray Menu UDF and pledge to do the same for anyone else who contributes to my project's success.
Link to comment
Share on other sites

On 10/22/2020 at 5:52 PM, abberration said:

TheXman's suggestion uses _WinAPI_Create32BitHBITMAP() that requires the handle of the source icon. I do not know how to get the handle of a wrapper added icon.

The compiled example below does not use any external ico files.  They're all embedded in the the executable's resource data, like your original example.  You will notice that the example is almost exactly like the one I referred you to.  The only difference is that I refer to the embedded icons by their resource IDs in the exe's resource data.

Note: You will most likely need to change the path of the AutoIt ico files and recompile the example.  You MUST compile the example to see the embedded icons, you will not see the ico images by running the script in the IDE.

;NOTE:  To see the embedded icons, the script MUST BE COMPILED.

#NoTrayIcon

#AutoIt3Wrapper_Res_Icon_Add="C:\AutoIt3\Icons\MyAutoIt3_Red.ico"   ;resource id will be -201
#AutoIt3Wrapper_Res_Icon_Add="C:\AutoIt3\Icons\MyAutoIt3_Blue.ico"  ;resource id will be -202
#AutoIt3Wrapper_Res_Icon_Add="C:\AutoIt3\Icons\MyAutoIt3_Green.ico" ;resource id will be -203

#include <GUIMenu.au3>
#include <Constants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIShellEx.au3>
#include <WinAPISys.au3>

Opt('TrayMenuMode', 3)

example()

Func example()
    ;Build tray menu
    Local $hMenu  = TrayItemGetHandle(0)
    Local $iRed   = TrayCreateItem('Red Item')
    Local $iBlue  = TrayCreateItem('Blue Item')
    Local $iGreen = TrayCreateItem('Green Item')
    TrayCreateItem('')
    Local $idExit = TrayCreateItem('Exit')

    ;Assign ico images to the tray menu items
    _GUICtrlMenu_SetItemBmp($hMenu, 0, _WinAPI_Create32BitHBITMAP(_WinAPI_ShellExtractIcon(@ScriptFullPath, -201, 16, 16), 1, 1))
    _GUICtrlMenu_SetItemBmp($hMenu, 1, _WinAPI_Create32BitHBITMAP(_WinAPI_ShellExtractIcon(@ScriptFullPath, -202, 16, 16), 1, 1))
    _GUICtrlMenu_SetItemBmp($hMenu, 2, _WinAPI_Create32BitHBITMAP(_WinAPI_ShellExtractIcon(@ScriptFullPath, -203, 16, 16), 1, 1))

    TraySetState()

    Do
    Until TrayGetMsg() = $idExit
EndFunc

image.png.de1e61067834f8e0fadd77af22625ff3.png

On 10/22/2020 at 5:52 PM, abberration said:

I will also give credit to anyone else that contributes code or functionality that helps my project.

For the record, there's no need to give me any credit for helping you with your project.  If you do, I'm okay with it, but it is not something that I seek.  I don't help other to get any sort of credit or "likes".  When I help others it is because I choose to and because helping others helps me.  All I look for is a simple "thank you".  :)

Edited by TheXman
Link to comment
Share on other sites

TheXman,

Thank you for the code. _WinAPI_ShellExtractIcon was the missing link that I couldn't figure out. I also discovered something else that might help others in the future... If I right-click the script and go to "Compile Script", it does not show the icons. I had to use "Compile with Options", which has the Res Add Files tab. I tried a lot of stuff these past couple days and a lot of my testing wasn't using Compile with Options. I wonder if any of my attempt failed because of that. Now that I'm on the right path, I will look at incorporating this again.

As for giving credit, I just like to do so because it shows appreciation for the help you and others give. I understand not desiring it, I am the same way.

On another note, the last code I posted was for others looking for solutions in the future. I have seen many posts where the person had the exact problem I was having and their last post was simply "nevermind, I found a solution". I don't like to do that. I will try to always post my solution.

Thanks again for your help. Great code, I will add it to my own personal snippets folder.

Cheers! 🥂

Link to comment
Share on other sites

You're welcome!  I'm glad that you were able to get it all sorted out.  :thumbsup:

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...