How to load icon resources

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

How to load icon resources

Post by Mistrel »

Here is how to load an icon resource that is part of an icon group:

Code: Select all

OpenWindow(0,200,200,320,240,"",#PB_Window_SystemMenu)

Structure GRPICONDIRENTRY
	bWidth.b ;/ Width, in pixels, of the image
	bHeight.b ;/ Height, in pixels, of the image
	bColorCount.b ;/ Number of colors in image (0 if >=8bpp)
	bReserved.b ;/ Reserved
	wPlanes.w ;/ Color Planes
	wBitCount.w ;/ Bits per pixel
	dwBytesInRes.l ;/ how many bytes in this resource?
	nID.w ;/ the ID
EndStructure

Structure GRPICONDIR
	idReserved.w ;/ Reserved (must be 0)
	idType.w ;/ Resource type (1 for icons)
	idCount.w ;/ How many images?
	idEntries.GRPICONDIRENTRY[0] ;/ The entries for each image
EndStructure

Structure ICONIMAGE
	icHeader.BITMAPINFOHEADER ;/ DIB header
	icColors.RGBQUAD[0] ;/ Color table
	icXOR.b[0] ;/ DIB bits for XOR mask
	icAND.b[0] ;/ DIB bits for AND mask
EndStructure

#LOAD_LIBRARY_AS_DATAFILE=2
Lib=LoadLibraryEx_("C:\WINDOWS\system32\progman.exe",0,#LOAD_LIBRARY_AS_DATAFILE)
*lpGrpIconDir.GRPICONDIR=LoadResource_(Lib,FindResource_(Lib,147,#RT_GROUP_ICON))

;/ Load large MSDOS icon
*lpIconImage.ICONIMAGE=LoadResource_(Lib,FindResource_(Lib,*lpGrpIconDir\idEntries[3]\nID,#RT_ICON))
hIcon=CreateIconFromResource_(*lpIconImage,*lpIconImage\icHeader\biBitCount,1,$00030000)
PostMessage_(WindowID(0),#WM_SETICON,1,hIcon)

;/ Load small MSDOS icon
*lpIconImage.ICONIMAGE=LoadResource_(Lib,FindResource_(Lib,*lpGrpIconDir\idEntries[7]\nID,#RT_ICON))
hIcon=CreateIconFromResource_(*lpIconImage,*lpIconImage\icHeader\biBitCount,1,$00030000)
PostMessage_(WindowID(0),#WM_SETICON,0,hIcon)

Repeat
Until WaitWindowEvent()=#WM_CLOSE
End
And here is how to load a straight icon resource that is not part of a group:

Code: Select all

*lpIconImage.ICONIMAGE=LoadResource_(0,FindResource_(0,1,#RT_ICON))
hIcon=CreateIconFromResource_(*lpIconImage,*lpIconImage\icHeader\biBitCount,1,$00030000)
Does anyone know if it's possible to load an entire icon group as a single icon handle?
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Cool 8)
Thanks for shared.

I don't know if is my fault, but i have an error at this line 39, with PB 4.20 or 4.30 . :cry:
But i have an old W2000 :oops:
It's perhaps the reason

Code: Select all

hIcon=CreateIconFromResource_(*lpIconImage,*lpIconImage\icHeader\biBitCount,1,$00030000) 
Nice code
ImageThe happiness is a road...
Not a destination
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

The second example requires an icon resource to already be present in the running executable. Add an icon from the compiler options for it to compile correctly or load an external dll/exe with an icon resource to load.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Ok MISTREL, i have understand
Thanks for your quick answer and again for your code 8)
ImageThe happiness is a road...
Not a destination
Post Reply