When using CreateIconFromResource
What should I do about the Icon data. Is it the whole Icon??? All 318 bytes(16x16 & 16 colours), or does it only need the image part of it, or maybe something else? Any help would be greatly appreciated :D
Posted on 2004-06-22 19:46:44 by tix
Assuming that you have found an identifier for the icon from the GRPICONDIR it is associated with or by some other means this is how to create an icon from it :

invoke FindResource, [hModule], ADDR IcoResName, RT_ICON

mov [hFindRes], eax
invoke SizeofResource, [hModule], [hFindRes]
mov [ResSize],eax
invoke LoadResource, [hModule], [hFindRes]
mov [hResData],eax
invoke LockResource, [hResData]
mov [pResData],eax

or eax,eax

invoke CreateIconFromResource, [pResData], [ResSize], TRUE, 030000h
push eax
invoke FreeResource, [hResData]
pop eax

Posted on 2004-06-22 21:54:12 by donkey
Thanks donkey.
hmmm... I think I understand. But I have a variable which will store the Icon data (the icon will be changing -- counting) and I want to use the icon in the variable. Is there anyway to use an icon stored as a variable? I read the description of FindResource, and it only wants an integer identifier, or a pointer to a string with the identifier number :(
Posted on 2004-06-23 21:39:19 by tix
Sure, you have only to store the icon data in the variable, there is no requirement that it comes from resources, only that you supply a pointer to the data and it's size, just use offset IconData instead and get rid of all the LoadResource stuff.
Posted on 2004-06-23 22:21:30 by donkey
Thanks a lot donkey :)

EDIT: I also found this on the MSDN Library, pretty helpful: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
Posted on 2004-06-24 18:53:26 by tix
Am I doing something wrong? Just trying to see how it works. I made an Icon, then opened it up in notepad, copied pasted it as a variable, made sure the number of characters was the same the size of the icon, and tried using it with:
CreateIconFromResource,offset TestIcon,318,TRUE,030000h
mov note.hIcon,eax
and it just shows up blank (no icon).. ? Am I not suppose to use the whole Icon? Sorry for all the questions.
Posted on 2004-06-24 19:50:58 by tix
It is not as simple as just copying the icon data to the data section and using it. You must supply the routine with the proper data. An icon is made of of 1 ICONDIR structure and n ICONDIRENTRY structures where n is the number of icons in the file. You must pass the actual raw data pointer not pointers to these structures. SO if you have only 1 icon in the file it is done like this....

mov edx,offset IconInData

mov eax,[edx+18] ; offset of icon data
add eax,edx
mov ecx,[edx+14] ; dwBytes of raw icon data
invoke CreateIconFromResource,eax,ecx,TRUE,030000h
invoke SendDlgItemMessage, [hwnd], 1001, STM_SETIMAGE, IMAGE_ICON, eax

In GoAsm you can just include the icon data in your data section as follows...


IconInData INCBIN "TestIco.ico"

However in MASM you will be required to include it as HEX.
Posted on 2004-06-24 20:09:42 by donkey
Thank you so so much. Works perfectly :D
Posted on 2004-06-24 20:36:58 by tix
Hi tix,

I played around with the idea a bit and came up with a generic function. This will allow you to include multiple icons in a data block (ie file) and get their handles by index. You pass the pointer to your icon data and a zero based index of the icon you wish to extract...

CreateIconFromData FRAME pIconData, iIcon

LOCAL sz[2] :D

Creates an icon from icon data stored in the DATA or CONST SECTION
(The icon data is an ICO file stored directly in the executable)

pIconData = Pointer to the ico file data
iIcon = zero based index of the icon to load

If successful will return an icon handle, this handle must be freed
using DestroyIcon when it is no longer needed. The size of the icon
is returned in EDX, the high order word contains the width and the
low order word the height.

Returns 0 if there is an error.
If the index is greater than the number of icons in the file EDX will
be set to the number of icons available otherwise EDX is 0. To find
the number of available icons set the index to -1

xor eax, eax
mov edx, [pIconData]
or edx, edx
jz >.ERROR

movzx eax, W[edx+4]
cmp eax, [iIcon]
ja >
push eax
pop edx
xor eax, eax

mov eax, [iIcon]
shl eax, 4
add edx, eax
add edx, 6

movzx eax, B[edx]
mov [sz], eax
movzx eax, B[edx+1]
mov [sz+4], eax

mov eax, [edx+12]
add eax, [pIconData]
mov edx, [edx+8]

invoke CreateIconFromResourceEx, eax, edx, 1, 030000h, [sz], [sz+4], 0

mov edx,[sz]
shl edx,16
mov dx,[sz+4]

Posted on 2004-06-24 22:04:23 by donkey