Page 1 of 1
Icon display messed up
Posted: Thu Mar 21, 2024 8:24 am
by tatanas
Hi,
I'm adding icons to Menu items through "SHDefExtractIconW" function.
But sometimes the icons aren't what they should be.
I can't put the full code (too big), so here is the function and the call :
Code: Select all
Prototype.i ExtractArbitrarySizeIcon(pszIconFile.s, iIndex.i, uFlags.i, phiconLarge.i, phiconSmall.i, nIconSize.i)
Procedure MyCustomExtractIcon(file$, index, size)
If OpenLibrary(0, "Shell32.dll")
Protected hIcon
Protected CustomIcon.ExtractArbitrarySizeIcon
CustomIcon = GetFunction(0, "SHDefExtractIconW")
CustomIcon(file$, index, 0, @hIcon, #Null, size)
CloseLibrary(0)
ProcedureReturn hIcon
Else
ProcedureReturn 0
EndIf
EndProcedure
...
MenuItem(#contextMenu1, "example", MyCustomExtractIcon("shell32.dll", 55, 16))
...
Where do I make a mistake ?
Thanks for your time.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 8:51 am
by STARGĂ…TE
tatanas wrote: Thu Mar 21, 2024 8:24 am
I can't put the full code (too big), so here is the function and the call :
Where do I make a mistake ?
The prototype ExtractArbitrarySizeIcon is not defined, #contextMenu1 is not defined and the menu with number #contextMenu1 is not created.
At least you need a window to create a menu.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 9:09 am
by tatanas
Sorry I didn't put all the code but of course I wrote everything you've noticed.
(I added the prototype def in the first post)
Re: Icon display messed up
Posted: Thu Mar 21, 2024 9:17 am
by Fred
Why not taking the time to write a small working snippet showing the issue ? It's hard guessing what's wrong when you can't test it.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 9:28 am
by tatanas
Because I can't reproduce the bug. It happens one time out of 50 maybe.
I was just wondering if the function and its use was right.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 12:58 pm
by BarryG
Write a small snippet that does what you want to achieve, and when that works, compare it to your full other code and you'll spot your bug. I've done this so many times that it's not funny.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 3:03 pm
by tatanas
Sorry but I'm going to repeat myself :
I was just wondering if the function and its use was right.
If there is no error with the use of this function, I will investigate other part of my code.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 3:17 pm
by fryquez
The function is more or less right.
I would not use OpenLibrary() with hard coded number unless written for a short forum example code.
You do not keep track of the hIcon's you create, ideally you would free them if no longer needed.
However the problem is most likely with the Windows Icon cache:
I seeing messed up icons on some systems every couple of months, others never show these problems.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 3:58 pm
by Axolotl
I have a few spontaneous questions about the function used:
What if the icons are not included as a 16x16Px version?
Why don't you check the return value for success?
--> you can use the available constants (#S_OK, #S_FALSE, #E_FAIL) for the possible return values here.
Perhaps the answers will help you to get to the bottom of the problem.
You can also use a different function to test the file (example shows dll, exe or ico are ok as well)
Code: Select all
NbIcons = 2
Dim hIcon(NbIcons)
Debug ExtractIconEx_(GetSystemDirectory()+"Shell32.dll", 130, 0, @hIcon(0), NbIcons)
For n = 0 To NbIcons
Debug Str(n) + " = 0x" + Hex(hIcon(n))
Next
; remark: You must destroy all icons extracted by ExtractIconEx by calling the DestroyIcon function.
Another hint from MSDN:
To retrieve the dimensions of the large and small icons, use the GetSystemMetrics function with the SM_CXICON, SM_CYICON, SM_CXSMICON, and SM_CYSMICON flags.
Re: Icon display messed up
Posted: Thu Mar 21, 2024 6:57 pm
by RASHAD
Most of my problems comes from Time and Timing specially these days with SSD devices
Check that and give the CPU and other components time to breeze
Re: Icon display messed up
Posted: Fri Mar 22, 2024 9:09 am
by tatanas
Thank you everyone for your tips.
I will add some error check inside the function.
RASHAD : I'm intrigued by your comment. Do you mean I should add some delay between each call of this function ?
Re: Icon display messed up
Posted: Fri Mar 22, 2024 11:10 am
by RASHAD
Hi tatanas
It depends
Delay is not always the good solution
But you can use [While WindowEvent():Wend ] when needed
Be sure that every repeated process start on clean ground
Try and error is your answer
