Using Resource Files (RC / RES)

Just starting out? Need help? Post your questions and find answers here.
andreasahlen
New User
New User
Posts: 7
Joined: Fri Jul 06, 2018 10:55 am
Location: Wasserburg am Inn (BY / DE)

Using Resource Files (RC / RES)

Post by andreasahlen »

Code: Select all

// TOOLBAR.RC
#define PUBLISHERS_TOOLBAR_PNG	1001
#define SHUTDOWN_TOOLBAR_PNG	1002
#define TASKS_TOOLBAR_PNG  		1003

PUBLISHERS_TOOLBAR_PNG IMAGE "..\\images\\png\\publishers.png"
SHUTDOWN_TOOLBAR_PNG IMAGE "..\\images\\png\\shutdown.png"
TASKS_TOOLBAR_PNG IMAGE "..\\images\\png\\tasks.png"
Compiling to RES works.

How to use the RES / RC file in my project? I already added the *.RC file to the compiler's Project Options (Resources)

Resource loading method:

Code: Select all

Procedure GetResource(ResName.s, ResType.l, hModule.i = 0, *ResSize.long = 0)
  Protected hFind.i, hLoad.i
  If Not hModule  : hModule = GetModuleHandle_(0) : EndIf
  hFind = FindResource_(hModule, ResName, ResType)
  If hFind
    hLoad = LoadResource_(hModule, hFind)
    If *ResSize
      *ResSize\l = SizeofResource_(hModule, hFind)
    EndIf
    ProcedureReturn LockResource_(hLoad)
  EndIf
EndProcedure
Calling from main:

Code: Select all

Img_Window_Main_0 = GetResource(#PUBLISHERS_TOOLBAR_PNG, #RT_BITMAP, 0, 0)
Thanks in advance ;)
-------
Regards | Saluti | Grüße
Andy
-------
Happy coding :)
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Using Resource Files (RC / RES)

Post by Denis »

Hi andreasahlen,

here is how i do in my different projects.

Resource id have to be set to PB file too.
For your example :

Code: Select all

#FirstconToolBar = 1001
Enumeration #FirstconToolBar
      #RC_PUBLISHERS_TOOLBAR_PNG
      #RC_SHUTDOWN_TOOLBAR_PNG
      #RC_TASKS_TOOLBAR_PNG
EndEnumeration
I do prefer put #RC_ before the name to directly understand that there is a resource id too.

So, i put a procedure i use for a while now to catch image from resource, that is the PB ids and functions are still avalaibe.

First, i put PNG as RC_DATA in the rc file (i don't use a res file).
And i use a language (here neutral) because in my projects, i often use at least 2 languages.

The resource file, save it as a text file with an txt editor (notepad etc.).
#define LANG_NEUTRAL 0
#define SUBLANG_NEUTRAL 0



// TOOLBAR.RC
#define PUBLISHERS_TOOLBAR_PNG 1001
#define SHUTDOWN_TOOLBAR_PNG 1002
#define TASKS_TOOLBAR_PNG 1003



LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
PUBLISHERS_TOOLBAR_PNG RCDATA "..\\images\\png\\publishers.png"
SHUTDOWN_TOOLBAR_PNG RCDATA "..\\images\\png\\shutdown.png"
TASKS_TOOLBAR_PNG RCDATA "..\\images\\png\\tasks.png"
If you don't load image, may be it could come from your relative path.

And the PB file i use to do the test.

Code: Select all

;https://www.purebasic.fr/english/viewtopic.php?f=13&t=71021

;- Macros
Macro MAKELANGID(primary, sublang)
      (((sublang)<<10) | (primary))
EndMacro

Macro SUBLANGID(lgid)
      ((lgid)>>10)
EndMacro

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

Macro MAKEINTRESOURCE(Value)
      (Value & $FFFF)
EndMacro

Macro IS_INTRESOURCE(Val)
      Bool(Val & ~$FFFF)
EndMacro


;//  function return value
#Return_Error = #False
#Return_No_Error = #True

;// PNG image resource IDs of the TOOLBAR buttons
#FirstconToolBar = 1001
Enumeration #FirstconToolBar
      #RC_PUBLISHERS_TOOLBAR_PNG
      #RC_SHUTDOWN_TOOLBAR_PNG
      #RC_TASKS_TOOLBAR_PNG
EndEnumeration
;// PNG image PureBasic IDs if you dont want to use #PB_Any
Enumeration
      #PNG_PUBLISHERS_TOOLBAR_PNG
      #PNG_SHUTDOWN_TOOLBAR_PNG
      #PNG_TASKS_TOOLBAR_PNG
EndEnumeration

Global PUBLISHERS_TOOLBAR_IMG
Global SHUTDOWN_TOOLBAR_IMG
Global TASKS_TOOLBAR_IMG

UsePNGImageDecoder()

Procedure.i CatchPngImageFromResource(id, StaticPNG_ID, hModule = #Null)
      ;///////////////////////////////////////////////////////////////////////////////////////////////////
      ;// FONCTION/FUNCTION : CatchPngImageFromResource()
      ;//
      ;// BUT/PURPOSE :  Create PB image with PNG as RC_DATA resource
      ;//
      ;// PARAMETRES/PARAMETERS :  id - Static identifier of the RC_DATA resource
      ;//                          StaticPNG_ID - ID number of PB image, could be #PB_Any
      ;//                          hModule - A handle to the module whose portable executable file or
      ;//                                    an accompanying MUI file contains the resource.
      ;//                                    If this parameter is #NULL, the function searches the Module used to create the current process.
      ;//
      ;// RETOURNE/RETURNS : Return no Null value in successful, if StaticPNG_ID = #PB_Any, it returns static PB id of image
      ;//                    #Return_Error in case of failure
      ;///////////////////////////////////////////////////////////////////////////////////////////////////
      ;// hModule
      Protected Local_hModule
      ;// handle to the specified resource's information block.
      ;// To obtain a handle To the resource, pass this handle To the LoadResource function.
      ;// https://docs.microsoft.com/fr-fr/windows/desktop/api/winbase/nf-winbase-findresourceexa
      Protected Find_Resource
      ;// handle to the data associated with the resource.
      Protected LoadResource_RT_RCDATA
      ;// pointer to the first byte of the resource
      Protected *LockResource
      ;// number of bytes in the resource
      Protected SizeofResource
      ;// PB image Id
      Protected img
      
      Local_hModule = hModule
      Find_Resource = FindResourceEx_(Local_hModule, MAKEINTRESOURCE(#RT_RCDATA), MAKEINTRESOURCE(id), MAKELANGID(#LANG_NEUTRAL, #SUBLANG_NEUTRAL))
      If Find_Resource = #Return_Error
            Debug "Find_Resource = " + Str(Find_Resource)
            ProcedureReturn #Return_Error
      EndIf
      
      LoadResource_RT_RCDATA = LoadResource_(Local_hModule, Find_Resource)
      If  LoadResource_RT_RCDATA = #Return_Error
            Debug "LoadResource_RT_RCDATA = " + Str(LoadResource_RT_RCDATA)
            ProcedureReturn #Return_Error
      EndIf
      
      ;// LockResource does not actually lock memory;
      ;// it is just used to obtain a pointer to the memory containing the resource Data.
      ;// The name of the function comes from versions prior to Windows XP, when it was used to lock a Global memory block allocated by LoadResource.
      ;// https://msdn.microsoft.com/en-us/library/windows/desktop/ms648047(v=vs.85).aspx
      *LockResource = LockResource_(LoadResource_RT_RCDATA)
      If *LockResource = #Return_Error
            Debug "*LockResource = " + Str(*LockResource)
            ProcedureReturn #Return_Error
      EndIf
      
      SizeofResource = SizeofResource_(Local_hModule, Find_Resource)
      If SizeofResource = 0
            Debug "SizeofResource = " + Str(SizeofResource)
            ProcedureReturn #Return_Error
      EndIf
      
      ;// Load PNG From memory
      ProcedureReturn CatchImage(StaticPNG_ID, *LockResource, SizeofResource)
EndProcedure

; First way using #PB_Any
PUBLISHERS_TOOLBAR_IMG = CatchPngImageFromResource(#RC_PUBLISHERS_TOOLBAR_PNG, #PB_Any)
If PUBLISHERS_TOOLBAR_IMG
      Debug "ImageID(PUBLISHERS_TOOLBAR_IMG) = " + Str(ImageID(PUBLISHERS_TOOLBAR_IMG))
EndIf
SHUTDOWN_TOOLBAR_IMG = CatchPngImageFromResource(#RC_SHUTDOWN_TOOLBAR_PNG, #PB_Any)
If SHUTDOWN_TOOLBAR_IMG
      Debug "ImageID(SHUTDOWN_TOOLBAR_IMG) = " + Str(ImageID(SHUTDOWN_TOOLBAR_IMG))
EndIf
TASKS_TOOLBAR_IMG = CatchPngImageFromResource(#RC_TASKS_TOOLBAR_PNG, #PB_Any)
If TASKS_TOOLBAR_IMG
      Debug "ImageID(TASKS_TOOLBAR_IMG) = " + Str(ImageID(TASKS_TOOLBAR_IMG))
EndIf
Debug ""
Debug "PUBLISHERS_TOOLBAR_IMG = " + Str(PUBLISHERS_TOOLBAR_IMG)
Debug "SHUTDOWN_TOOLBAR_IMG = " + Str(SHUTDOWN_TOOLBAR_IMG)
Debug "TASKS_TOOLBAR_IMG = " + Str(TASKS_TOOLBAR_IMG)
Debug ""

If PUBLISHERS_TOOLBAR_IMG : FreeImage(PUBLISHERS_TOOLBAR_IMG) : EndIf
If SHUTDOWN_TOOLBAR_IMG : FreeImage(SHUTDOWN_TOOLBAR_IMG) : EndIf
If TASKS_TOOLBAR_IMG : FreeImage(TASKS_TOOLBAR_IMG) : EndIf

; Another way using predefined PB identifiers.
PUBLISHERS_TOOLBAR_IMG = CatchPngImageFromResource(#RC_PUBLISHERS_TOOLBAR_PNG, #PNG_PUBLISHERS_TOOLBAR_PNG)
If PUBLISHERS_TOOLBAR_IMG
      Debug "ImageID(#PNG_PUBLISHERS_TOOLBAR_PNG) = " + Str(ImageID(#PNG_PUBLISHERS_TOOLBAR_PNG))
EndIf
SHUTDOWN_TOOLBAR_IMG = CatchPngImageFromResource(#RC_SHUTDOWN_TOOLBAR_PNG, #PNG_SHUTDOWN_TOOLBAR_PNG)
If SHUTDOWN_TOOLBAR_IMG
      Debug "ImageID(#PNG_SHUTDOWN_TOOLBAR_PNG) = " + Str(ImageID(#PNG_SHUTDOWN_TOOLBAR_PNG))
EndIf
TASKS_TOOLBAR_IMG = CatchPngImageFromResource(#RC_TASKS_TOOLBAR_PNG,  #PNG_TASKS_TOOLBAR_PNG)
If TASKS_TOOLBAR_IMG
      Debug "ImageID(#PNG_TASKS_TOOLBAR_PNG) = " + Str(ImageID(#PNG_TASKS_TOOLBAR_PNG))
EndIf

Debug ""
Debug "PUBLISHERS_TOOLBAR_IMG = " + Str(PUBLISHERS_TOOLBAR_IMG)
Debug "SHUTDOWN_TOOLBAR_IMG = " + Str(SHUTDOWN_TOOLBAR_IMG)
Debug "TASKS_TOOLBAR_IMG = " + Str(TASKS_TOOLBAR_IMG)

If IsImage(#PNG_PUBLISHERS_TOOLBAR_PNG) : FreeImage(#PNG_PUBLISHERS_TOOLBAR_PNG) : EndIf
If IsImage(#PNG_SHUTDOWN_TOOLBAR_PNG) : FreeImage(#PNG_SHUTDOWN_TOOLBAR_PNG) : EndIf
If IsImage(#PNG_TASKS_TOOLBAR_PNG) : FreeImage(#PNG_TASKS_TOOLBAR_PNG) : EndIf

A+
Denis
andreasahlen
New User
New User
Posts: 7
Joined: Fri Jul 06, 2018 10:55 am
Location: Wasserburg am Inn (BY / DE)

Re: Using Resource Files (RC / RES)

Post by andreasahlen »

Great post. Thank you :) I will try it.
-------
Regards | Saluti | Grüße
Andy
-------
Happy coding :)
andreasahlen
New User
New User
Posts: 7
Joined: Fri Jul 06, 2018 10:55 am
Location: Wasserburg am Inn (BY / DE)

Re: Using Resource Files (RC / RES)

Post by andreasahlen »

applied changes, it works! Thanks a lot. Even the localization feature is very useful!
-------
Regards | Saluti | Grüße
Andy
-------
Happy coding :)
Post Reply