Page 1 of 1

Best way to include .ico files

Posted: Sun Mar 10, 2013 8:54 pm
by SFSxOI
I need to include, by request, some 'niceties' in something i'm working on. They are icons shown at certain places when certain things happen. I'm not a graphics type of person, but I've got the icons (16 x 16) in .ico format. I just don't know how to include them as part of the compiled .exe. I think they may need to be in a data section and read from there but I don't know how to convert them to a data section then display then at the proper times. I don't pay too much attention to graphics attributes because the majority of what I code is base code for inclusion in other things as part of a larger application so the graphics are usually done by someone else if they are needed, or its analyzing code for looking at other things and the graphics simply aren't needed.

So, how do I convert the .ico's into a data section and then show them when I need to do so? Or is a data section even the best way to do it?

Re: Best way to include .ico files

Posted: Sun Mar 10, 2013 9:26 pm
by idle
should be ok for bmp icons

Code: Select all

Procedure CatchIcon(index)
   Select index 
      Case 1 
         icon = CatchImage(#PB_Any,?icon1,icon2-icon1) 
      Case 2 
         icon = CatchImage(#PB_Any,?icon2,icon3-icon2) 
   EndSelect 
   ProcedureReturn icon 
 EndProcedure   

DataSection 
   icon1: 
   IncludeBinary "c:\icon1.ico"
   icon2:
   IncludeBinary "c:\icon2.ico"
   icon3:
EndDataSection   

img = CatchIcon(2)

OpenWindow(1,0,0,200,100,"test")
ImageGadget(1,5,5,32,32,ImageID(img))

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow 

Re: Best way to include .ico files

Posted: Sun Mar 10, 2013 10:03 pm
by SFSxOI
idle,

That did the trick. Perfect. I was thinking it was going to be some long elaborate hoop I needed to jump through with creating big DataSections with lots of drawing and stuff. Simple and direct is best in this case.

Thank you very much. :)

Re: Best way to include .ico files

Posted: Sun Mar 10, 2013 11:25 pm
by idle
SFSxOI wrote:idle,

That did the trick. Perfect. I was thinking it was going to be some long elaborate hoop I needed to jump through with creating big DataSections with lots of drawing and stuff. Simple and direct is best in this case.

Thank you very much. :)
Your welcome, see kcc for lots of hoop jumping and drawing stuff :lol:

Re: Best way to include .ico files

Posted: Tue Mar 12, 2013 11:35 am
by Denis
Hi SFSxOI,

i use resource in my project to load ico (PNG too).

First, you have to create a resource file (.rc) with notepad or notepad++ or with PelleC ide (etc.).

Resource manage language definitions, if you use icons without language managment, declare language as neutral.
Then define icon static id; those id must be the same as PB id to load ico.
Finally, for each id, define the path for ico file (you must put 2 \\, not only 1).

Here is an rc file example :

Code: Select all

// language definition
#define LANG_NEUTRAL 0
#define SUBLANG_NEUTRAL 0

// static ico id, same as PB enumeration
#define Ico_1 = 1
#define Ico_2 = 2
#define Ico_3 = 3
#define Ico_4 = 4
#define Ico_5 = 5

// language used for resource
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL

// ico files to load in resource with static id
Ico_1    RCDATA "D:\\Icone_barre_outils\\Ico1.ico.png"
Ico_2    RCDATA "D:\\Icone_barre_outils\\Ico2.ico.png"
Ico_3    RCDATA "D:\\Icone_barre_outils\\Ico3.ico.png"
Ico_4    RCDATA "D:\\Icone_barre_outils\\Ico4.ico.png"
Ico_5    RCDATA "D:\\Icone_barre_outils\\Ico5.ico.png"
you must attach this rc file to your code with compiler option window, item resources.


following is a piece of code from my project (cleaned up and adapted for this example).
I use this for a while without problem (run on X86 & X64).

You may use Freeimage if needed.

Code: Select all

Define .i
EnableExplicit

Macro MAC_LOWORD(Value)
      Value & $FFFF
EndMacro

Macro MAC_MAKEINTRESOURCE(Value)
      MAC_LOWORD(Value)
EndMacro

Macro MAC_MAKELANGID(primary, sublang)
      (((sublang)<<10) | (primary))
EndMacro

Macro MAC_SUBLANGID(lgid)
      ((lgid)>>10)
EndMacro

Macro MAC_PRIMARYLANGID(lgid)
      ((lgid)&$3FF)
EndMacro


 #Return_Error = #False
;#LANG_NEUTRAL = 0
;#SUBLANG_NEUTRAL = 0


Enumeration 1  ; ico static id
      #Ico_1
      #Ico_2
      #Ico_3
      #Ico_4
      #Ico_5
EndEnumeration


Global GLB_hInstance, i

Procedure InitMain()
      GLB_hInstance = GetModuleHandle_(0)
EndProcedure

Procedure.i Get_RCDATA_Adress(id)
      ;// mémorise le handle du bloc d'informations du groupe d'icônes
      Protected Find_Resource
      ;// mémorise le handle du bloc d'information des données de la ressource string
      Protected LoadResource_RT_RCDATA
      ;// mémorise l'adresse mémoire du block d'informations de la ressource chargée en mémoire
      Protected *LockResource
      ;// mémorise la taille des données de la ressource RT_String pour la langue choisie
      Protected SizeofResource
      
      Find_Resource = FindResourceEx_(GLB_hInstance, #RT_RCDATA, MAC_MAKEINTRESOURCE(id), MAC_MAKELANGID(#LANG_NEUTRAL, #SUBLANG_NEUTRAL))
      If Find_Resource = #Return_Error
            ProcedureReturn #Return_Error
      EndIf
      
      LoadResource_RT_RCDATA = LoadResource_(GLB_hInstance, Find_Resource)
      If  LoadResource_RT_RCDATA = #Return_Error
            ProcedureReturn #Return_Error
      EndIf
      
      *LockResource = LockResource_(LoadResource_RT_RCDATA)
      If *LockResource = #Return_Error
            ProcedureReturn #Return_Error
      EndIf
      
      ProcedureReturn *LockResource
EndProcedure

Procedure.i CatchImageEx(img_static_id)
      Protected *memory
      
      *memory = Get_RCDATA_Adress(img_static_id)
      If *memory <> #Return_Error
            CatchImage(img_static_id, *memory)
      EndIf
EndProcedure

InitMain() 
      If GLB_hInstance = #Null
           MessageRequester("Error", "unable to continue", #PB_MessageRequester_Ok)
            End
      EndIf
OpenWindow(1,0,0,200,100,"test", #PB_Window_ScreenCentered)
;// load the 5 ico in resource
For i = 1 To 5
      CatchImageEx(i)
      ;// check if ok, always check this !
      If IsImage(i) = 0
            MessageRequester("Error", "unable to continue", #PB_MessageRequester_Ok)
            End
      EndIf
Next


ImageGadget(1,5,5,32,32,ImageID(#Ico_1))
;etc.