Resource File (Accessing)

Category: Application Features

Date: 02-16-2022

Return to Index


 
'This snippet shows how to access information from a resource file. It
'builds on the snippet at http://gbl_00330 .
 
'More information on resource files can be found at MSDN,
'at http://msdn.microsoft.com/en-us/library/aa381042%28VS.85%29.aspx
 
'Resource Script File
'The gbsnippets.pbr contains the following resource script lines. The compilable
'snippet below shows how to access the resources defined by these lines.
 
//resource script file
texas     Icon    "texas.ico"                                //Icon
cowgirl   Bitmap  "cowgirl.bmp"                              //Bitmap
cash      wave    "cash.wav"                                 //User-defined
camera    rcdata  "camera.wav"                               //Pre-defined RCDATA
mydata    rcdata  {5L, 72L, "A string", "Another string"}    //Pre-defined RCDATA
 
STRINGTABLE
Begin
   001, "Gary Beene"
   002, "Gary Beene's Information Center"
End
 
VS_VERSION_INFO    VERSIONINFO
 FileVersion          1,6,0,0
 ProductVersion       1,6,0,0
 FileOS               VOS__WINDOWS32
 FileType             VFT_APP
 FileSubType          VFT2_UNKNOWN
BEGIN
  Block "StringFileInfo"
  BEGIN
    Block "040904E4"
    BEGIN
      Value "Owner Name",    "Gary Beene"
      Value "ProductName",   "gbSnippets"
      Value "ProductVersion",  "1.6"
      Value "LegalCopyright",  "Copyright 2009 �, Gary L. Beene"
    End
   End
  End
//resource script file
 
'Primary Code:
'Using an bitmap is usually handled by the Control Add statement
'Using an icon can be handled with DDT statements such as Dialog Set Icon.
'PowerBASIC Graphic statements may also be used to place a resource icon/bitmap
'onto a graphic target (graphic control, memory bitmap, or graphic window.
pbr  --->  ImageList------------> (Commands specific to each Control)
pbr  --------------------------> Render ------------> Any Graphic target
pbr  --->  ImageList------------> Graphic ImageList  ---> Any Graphic target
pbr  --->  Graphic Bitmap Load -----------------------> Memory Bitmap
 
'To play a sound from a resource file
      iReturn = sndPlaySound(resourceID, %SND_ASYNC Or %SND_RESOURCE)
      'see the snippet http://gbl_00350 for more information
'To save a resource (of type rcdata). See http://gbl_00351 for more details
      lRet1 = FindResource(hDlg, szName, ByVal %RT_RCDATA)
      If IsFalse lRet1 Then Function = 0:Exit Function   'could not find the res so exit
      dRet1 = SizeofResource(hDlg,lRet1)
      lRet2 = LoadResource(hDlg,lRet1)
      sBuff = Peek$(dRet2, dRet1)    ' load sBuff with the info we just put in memory
      Put$ #lFile, sBuff      write the buffer to the file (Open/Close Code Not Shown)
'To get a string from a string table:
      LoadString GetModuleHandle(ByVal 0&), stringID&, buf$, SizeOf(buf))
 
'Compilable Example:  (Jose Includes)
'This snippets shows how to access the resources found in the resource file.
'When a resource is needed from a resource file, it is read from the file into memory.
'The entire resource file is NOT loaded into memory when the application starts.
'Note that no buttons are needed to demo icon/bitmap use. The Dialog Set Icon
'and Control Add Image show how to access images (the method of accessing an image
'can vary from control type to control type).
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg as Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Dialog Set Icon hDlg, "texas"    'accesses an icon in the resource file
   Control Add Button, hDlg, 100,"Get String", 10,10,80,20
   Control Add Button, hDlg, 300,"Play Sound", 110,10,80,20
   Control Add Image, hDlg, 200,"cowgirl", 50,50,100,100, %SS_Notify   'accesses bitmap in the resource file
   Control Add Button, hDlg, 400,"Get Version Info", 10,160,80,20
   Control Add Button, hDlg, 500,"Save Resource", 110,160,80,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
      Local buf As Asciiz * %Max_Path
      'use LOADSTRING API to get a string from a resource file stringtable
      LoadString GetModuleHandle(ByVal 0&), 200, buf$, SizeOf(buf)
      MsgBox Buf
   End If
   If CB.Msg = %WM_Command AND CB.Ctl = 300 AND CB.Ctlmsg = %BN_Clicked Then
      'play sound once (in the background)
      Local iReturn as Long, SndResource as Asciiz * %Max_Path
      SndResource = "cash"
      iReturn = sndPlaySound(SndResource, %SND_ASYNC Or %SND_RESOURCE)
   End If
   If CB.Msg = %WM_Command AND CB.Ctl = 500 AND CB.Ctlmsg = %BN_Clicked Then
      MakeFile "camera", "extracted.wav"
   End If
End Function
 
Function MakeFile(ResName AS String, NewFile AS StringAs Long
   'this function is discussed in snippet http://gbl_00351
   Local lRet1 As Long,lRet2 As Long, dRet1 AS Dword,dRet2 AS Dword
   Local sBuff AS String, szName As AsciiZ * 40, lFile As Long, hProcess As Long
   hProcess = 0                     'use null for current process
   szName = ResName             'change string to asciiz for api calls
   lRet1 = FindResource(hProcess, szName, ByVal %RT_RCDATA)
   If IsFalse lRet1 Then Function = 0:Exit Function   'could not find the res so exit
   dRet1 = SizeofResource(hProcess,lRet1)
   lRet2 = LoadResource(hProcess,lRet1)
   If IsFalse lRet2 Then Function = 0:Exit Function   'could not load res to mem so exit
   dRet2 = LockResource(lRet2)
   sBuff = Peek$(dRet2, dRet1)    ' load sBuff with the info we just put in memory
   'now save the item from memory to a file
   lFile = FreeFile                            'find next open file number
   Open NewFile For BINARY AS #lFile           'create the new file
   Put$ #lFile, sBuff                      'write the buffer to the file
   Close #lFile                                'close the file
   MsgBox "Resource Saved to file"
   Function = 1                                'worked so return a 1
End Function
'gbs_00331
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm