Jump to content

Strange behaviour using GUICtrlCreateIcon


 Share

Recommended Posts

Recently I was working on a script with icons using GuiCtrkCreatIcon.
I decided to change the sub folder name of the icons to a more meaning name, however made a typo.

I tested the .exe on my test computer and it worked flawlessly (because both icon folder where on my test computer) 😁
But after I installed the script on the intended computers , I got chaos!😵

Zooming into the problem, I discovered, that because the icons could not be found, the ControlID were returned with a value of 0
and thus played havoc within the GuiGetMsg() switch/case statement.
I have been able to reproduce this  (see example)

#include <GUIConstantsEx.au3>

;============================================================================================================
; PLEASE, do not save this example in the example folder:  C:\Program Files (x86)\AutoIt3\Examples\Helpfile
;============================================================================================================

Example()

Func Example()
    GUICreate(" My GUI Icons", 250, 250)

    $Icon1 = GUICtrlCreateIcon("shell32.dll", 10, 20, 20)
    $Icon2 = GUICtrlCreateIcon(@ScriptDir & '\Extras\horse.ani', -1, 20, 40, 32, 32)
    $Icon3 = GUICtrlCreateIcon("shell32.dll", 7, 20, 75, 32, 32)
    GUISetState(@SW_SHOW)

    ;$Icon2 = -1        ; ==> When this line is uncommented the script "works", so -1 could be a potential fix.
    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $Icon2
                Beep (500,500)

        EndSwitch
    WEnd

    GUIDelete()
EndFunc   ;==>Example

If you save the above script outside the Autoit example folder and run it, it will keep beeping because GuiCtrlCreatIcon did not find horse.ani and return $Icon2=0.
At the moment GUICtrlCreateIcon () only returns the conntrolID on success and 0 on failure.
I would like to propose a return of -1 on failure, so a existing and working script won't go awry when the icon can not be found.
 

Link to comment
Share on other sites

  • Developers

I honestly think there should be a test in the example/script for success at control creation time in stead of making this "workaround" proposal, which goes against to general way AutoIt3 works as generically a return of 0 is failure and <> 1 is success. 

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Yes, you are right. This the Autoit way....😊

However I feel that when an icon is not found, it should not have such a big impact on the working of the script.
My script had several other GUIs behind the connected to the icons which all got triggered after each other.
And every time those GUIs were canceled they would popup again, so actually creating an infinite loop.

But enough about me, what would be your hans-on take and proposal on this ?

Groetjes, Martijn.

Edited by Jemboy
Link to comment
Share on other sites

Here a good way to add icons to a .exe file that can be exported anywhere thereafter :

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Icon_Add=C:\Apps\AutoIt\ZZTemp\icon_wWw_icon.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <Constants.au3>

If Not @Compiled Then Exit MsgBox ($MB_SYSTEMMODAL, "Error", "This script must be compiled")

Example()

Func Example()
    GUICreate(" My GUI Icons", 250, 250)

    $Icon1 = GUICtrlCreateIcon("shell32.dll", 10, 20, 20)
    $Icon2 = GUICtrlCreateIcon(@ScriptFullPath, 201, 20, 40, 32, 32)
    If Not $Icon2 Then $Icon2 = -1
    $Icon3 = GUICtrlCreateIcon("shell32.dll", 7, 20, 75, 32, 32)
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $Icon2
                Beep (500,500)
        EndSwitch
    WEnd
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Yes @Nine this would add minimum extra lines to check to the script and not clutter the script so much.
I hate those multi line If-Thens 😉

But would the Autoit format/protocol/design rules allow us to add this  -1 to the GUICtrlCreateIcon function and
should we want to do that ?

 

Random remark: I always found it  strange that the Autoit developpers choose use the 1 for success and 0 for failure.
his was confusing at first, having programmed with other languages 🙂
Also using a 0 based numbering (e.g. with array) has made my life difficult in the past and probaly will do so too in the future 😁

Link to comment
Share on other sites

  • Developers
45 minutes ago, Jemboy said:

However I feel that when an icon is not found, it should not have such a big impact on the working of the script.

Agree, that is why you as programmer should add the appropriate checks in your script to ensure it does function as it should.
There are a zillion ways to create mistakes like that and have Autoit3 go Crazy and one can't expect AutoIt3 to fix all for you. ;) 

Jos 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jemboy 

I don't know why for some ppl it is such an ugly style to check for errors.  Because in any languages, you must always try to create bullet proof applications.  All good programmers adopt this best practice.

Link to comment
Share on other sites

@NineIn theory I agree with you 100%.

In any case, the problem has been brought up, so hopefully other people will find this post helpfull when they experience this same GUICtrlCreateIcon problem.
Thanks for all your insights.

Link to comment
Share on other sites

  • Developers

A "simple" fix for any "wrong guimsg" woud be to add those at the top of the messageloop:

While 1
        Switch GUIGetMsg()
            Case 0
                ; skipped guimsgs.. more can be added.          
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $Icon2
                Beep (500,500)

        EndSwitch
    WEnd

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

I know you all like those variables, but I hardly use them. In this case the 0 makes it utterly clear what we are testing for. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I like using variable with meaninfull names, especially when looking up an old script.
I consider them like some extra remarks (REM)

However, @Jos very nice find to put the 0 case at the top of the switch!
Many roads lead to Rome ! 🙂

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

×
×
  • Create New...