Using Resources

Top  Previous  Next

Resources are files and data that get compiled with your application and embedded into the executable. Resources can only be used with projects and are not available for single file compiling.

Resource functions

success = LOADRESOURCE(ID, Type, Variable)

Loads a resource from the executable and places either a copy of the resource, or pointer to the resource, in the variable specified. LOADRESOURCE is most useful for reading raw data with @RESDATA.  For normal loading of images, icons and cursors you should use the LOADIMAGE function.

ID is either a numeric or string identifier to the resource, TYPE is a numeric or string type and it stores the info in variable. The standard Windows resource types can be specified and loaded in raw form using the following constants:

@RESCURSOR

@RESBITMAP

@RESICON

@RESMENU

@RESDIALOG

@RESSTRING

@RESACCEL

@RESDATA

@RESMESSAGETABLE

@RESGROUPCURSOR

@RESGROUPICON

@RESVERSION

 

If variable is a STRING (or ISTRING) variable the contents of the resource will be copied into the string. Useful for embedded text resources.

 

If variable is an INT or UINT variable then a handle to the locked resource is returned. Useful for API calls.

If variable is a POINTER type then a pointer to the locked resource is returned. The data can be accessed directly with type casting and dereferencing.

 

If variable is a MEMORY variable then memory is allocated for the resource and the resource is copied into it. You can then use READMEM to read the resource data. LEN(variable) will return the length of the resource in this case. You must free the memory returned in this case with the FREEMEM statement. Only use a newly defined MEMORY variable or one that has been freed with FREEMEM. Using a MEMORY variable that has been allocated with ALLOCMEM will result in memory leaks in your program.

handle = LOADIMAGE (filename | resourceID, type)

Loads an image from the resources. Type is a numeric value defining what kind of image to load.

The valid values for type are:

@IMGBITMAP - bitmap (*.bmp)

@IMGICON - Icon (*.ico)

@IMGCURSOR - Cursor (*.cur)

@IMGSCALABLE - scalable bitmap, JPEG (*.jpg) or GIF (*.gif) files.

@IMGOEM - OR ' | '  in with @IMGBITMAP, @IMGICON or @IMGCURSOR to load an OEM (system) resource.

If a filename is specified the image is loaded from disk. LOADIMAGE can also load bitmaps, icons and cursors directly from the executables resources. Enhanced meta files cannot be loaded from the resource table. Scalable images must be added as a 'Scalable Image' in the resource add dialog. Internally this is saved as type "RTIMAGE" if you need to use LOADRESOURCE to access the image data directly.

Resource ID is either the string or integer identifier of an image resource compiled with the project.

When you are finished using an image remember to free the image with the DELETEIMAGE command.

success = LOADMENU(window | dialog, resourceID)

Loads a menu definition from resources and creates the menu bar for the window or dialog specified. ResourceID is the string or integer identifier of the menu. The return value is 0 if the menu could not be loaded from the resources.

For example a menu definition may look like this:

BEGIN

  POPUP "&File"

  BEGIN

      MENUITEM "&New\tCtrl+N", 1

      MENUITEM "&Open...\tCtrl+O",2

      MENUITEM SEPARATOR

      MENUITEM "P&rint Setup...", 3

      MENUITEM SEPARATOR

      MENUITEM "Recent File", 4

      MENUITEM SEPARATOR

      MENUITEM "E&xit", 5

  END

  POPUP "&Help"

  BEGIN

      MENUITEM "&About ChartCraft...", 6

  END

END

 

length = GETRESOURCELENGTH(resourceID, type)

Returns the length of the specified resource in bytes.  When extracting a resource for saving to disk it is best to use this function to return the exact length.