Page 1 of 1
How to get directory icon & Byte Size?
Posted: Thu Jun 05, 2003 1:15 pm
by Inner
I'm stumped, I could get the icon of a file farely easy but how do you get hold of an directory icon, under xp you can modify your folders so that they can each have a diferent icon for each folder, anyone know how to obtain this icon please ?
Posted: Fri Jun 06, 2003 12:03 am
by freak
There's a fairly simple command for that... just that i am too tired right now ...
I'll write some example for you tomorrow.
Timo
Posted: Fri Jun 06, 2003 9:12 am
by freak
Here it comes...
Code: Select all
; SHFILEINFO structure is incorrect in PB, got to go fix that...
Structure MySHFILEINFO
hIcon.l
iIcon.l
dwAttributes.l
szDisplayName.b[#MAX_PATH]
szTypeName.b[80]
EndStructure
; The String can be any file on your disk, or a directory name. You will receive
; the same Icon, the M$ Explorer displays for this folder/file.
If SHGetFileInfo_("C:\RECYCLED\", 0, @Info.MySHFILEINFO, SizeOf(MySHFILEINFO), #SHGFI_ICON|#SHGFI_LARGEICON)
IconHandle = Info\hIcon
; display the icon
If OpenWindow(0,0,0,100,100,#PB_Window_Screencentered|#PB_Window_SystemMenu, "Icon")
If CreateGadgetList(WindowID())
ImageGadget(0, 10, 10, 32, 32, IconHandle)
Repeat
Until WaitWindowEvent() = #PB_EventCloseWindow
EndIf
EndIf
; Free the Icon handle:
; When your app ends, windows will free all icon handles, so you don't
; necessary need to do it. However, if you load a lot of these icons, it
; might be a good deal to free all those you don't need anymore, to keep
; the system recources free.
DestroyIcon_(IconHandle)
Else
MessageRequester("Error!","Could not get File/Folder Info",0)
EndIf
End
The SHGetFileInfo_() command is also usefull for other things, for example
getting a the file description text Explorer displays.
See all the possible flags of this command here for more info...
http://msdn.microsoft.com/library/defau ... leinfo.asp
Timo
Posted: Sat Jun 07, 2003 8:56 pm
by Inner
Thanks freak
I just have one problem left, how do you know the byte size of the icon pointed to by hicon, do you have to get the width,height & depth and do some maths with that ?
Posted: Sun Sep 16, 2007 7:50 pm
by Seymour Clufley
I've adapted the code above for use in a project. My goal is to save the icon as a bitmap file.
Unfortunately, SaveImage doesn't work like this:
SaveImage(IconHandle,bmpfile$)
because IconHandle is a handle, not an image number.
The workaround is pretty clumsy. It involves taking a snapshot of the window (using an ImageGadget etc.) and saving that with SaveImage. This means the window is temporarily visible - just for a split second but it's not the best.
So basically what I'm asking is: is there a way to get the image number from the image handle? Or "how to adapt IconHandle for use with SaveImage"??
Thanks in advance!
Seymour.
Posted: Sun Sep 16, 2007 10:33 pm
by srod
Here's one way; just draw the icon onto a PB image :
Code: Select all
;Convert icon to bitmap. (You will lose the transparency with this method.)
;Returns a PB Image# of the image containing a copy of the icon.
;Returns zero if an error.
Procedure.l ConvertIconToBitmap(hIcon)
Protected iconinfo.ICONINFO, bmap.BITMAP, image, hdc
;Check icon.
If GetIconInfo_(hIcon, iconinfo)
If GetObject_(iconinfo\hbmMask, SizeOf(BITMAP), bmap)
image = CreateImage(#PB_Any, bmap\bmWidth, bmap\bmHeight, 32)
If image
;Draw a copy of the icon onto the bitmap.
If StartDrawing(ImageOutput(image))
Box(0,0,bmap\bmWidth, bmap\bmHeight,#White)
DrawImage(hIcon, 0, 0)
StopDrawing()
Else
FreeImage(image)
image=0
EndIf
EndIf
EndIf
EndIf
ProcedureReturn image
EndProcedure
;/////////////////////////////////////////////////////////////
;Test.
LoadImage(1, "open.ico") ;Replace with your own icon.
image = ConvertIconToBitmap(ImageID(1))
;Let us now save the bitmap.
SaveImage(image, "test.bmp")
;Tidy up.
FreeImage(image)
End
Posted: Sun Sep 30, 2007 11:53 am
by Seymour Clufley
I meant to thank you for the code, Srod. It works perfectly.

Re: How to get directory icon & Byte Size?
Posted: Fri Feb 15, 2013 10:50 am
by Kwai chang caine
My goal is to save the icon as a bitmap file.
And mine is to save the Icon to a .ICO file....
Me to i have try SaveImage(GetDlgCtrlID_(IconHandle), IconFile$, #PB_ImagePlugin_PNG) but obviously.... that not works because PNG is transparent yes, but ICO is not PNG format, i believe ...

Re: How to get directory icon & Byte Size?
Posted: Fri Feb 15, 2013 11:35 am
by Denis
Salut KCK
may be it's what you're looking for (a good code from SROD)
http://www.purebasic.fr/english/viewtop ... 12&t=23387
Re: How to get directory icon & Byte Size?
Posted: Fri Feb 15, 2013 12:27 pm
by Kwai chang caine
Re: How to get directory icon & Byte Size?
Posted: Fri Feb 15, 2013 12:49 pm
by Kwai chang caine
I have try the splendid code of SROD, and that works very well
I have forgotten

to thanks also the great SROD for this good and splendid code

I have take a look to this jewel....and i'm surprising

to have understand something inside it, and can make the same easily now
It's already a beginning no ????
(Say yes ..pleaaaase !!!)
Again thanks at FREAK, SROD and you DENIS
