LoadResData Function

Syntax

LoadResData(resID, resType)


resID

Use: Required

Data Type: Variant

A numeric or string value specifying the resource ID of the data to load.


resType

Use: Required

Data Type: Variant

A numeric or string value denoting the format of the data to load. See the table below for valid values:

ResType Value Meaning
1 Cursor resource
2 Bitmap resource
3 Icon resource
6 String resource
10 User-defined resource
12 Group cursor
14 Group icon

Return Value

A Unicode string.

Description

Returns a binary (Unicode) string containing the specified resource from a resource (.RES ) file included with the project.

Resource files store graphics, strings, and other data inside the application, the contents of the .RES file being compiled into the final EXE. The advantage of storing support files in this manner is that they are permanently available to the application, unlike separate support files that can be accidentally deleted or not transferred should the application be moved. You can also store several localized versions of your resources in the .RES file that loads depending on the locale of the current machine, thereby easily internationalizing your application.

Rules at a Glance

  • You can pass a string value to restype if you are loading a custom or user-defined resource. For example:

    sRTFText  = LoadResData(102, "CUSTOM")

    This is the only case in which a string value is permitted.

  • The maximum string length returned by LoadResData is 64KB.

  • In Visual Basic, there is no advantage to using LoadResData to load a graphic resource because the function returns a Unicode string containing the actual byte data of the resource. When dealing with graphic resources, you should use LoadResPicture.

Programming Tips and Gotchas

  • String data returned from LoadResData is in Unicode format. For the vast majority of current VB controls, you need to convert this string to ANSI before it can be used. The following example shows how to do this.

  • Note that LoadResData is part of the VB Runtime Library and isn't available in VBA applications.

  • Contrary to the documentation for LoadResData in both VB5 and VB6, the following resource types aren't supported in Visual Basic and should therefore be ignored: 4 Menu resource, 5 Dialog box, 7 Font directory resource, 8 Font resource, and 9 Accelerator table.

  • The VB Runtime Library includes a set of intrinsic constants for specifying resource types, called LoadResConstants. However, these don't match the values of the LoadResData resType parameter. The LoadResConstants should be used only with the LoadResPicture function.

Example

In the following example, a richtext format document has been stored using the ID 102 as a custom resource within a .RES file. The custom RTF resource is displayed in a RichTextBox control. However, because LoadResData returns a Unicode string, the returned string must be converted into an ANSI string before it's used.

Private Sub Command1_Click()
   Dim suRTFText As String
   Dim sRTFText  As String
   Dim i         As Integer
    
   suRTFText = LoadResData(102, "CUSTOM")
   'convert to ANSI
   For i = 1 To LenB(suRTFText)
      sRTFText = sRTFText & _
                 Chr(AscB(MidB(suRTFText, i, 1)))
    Next i
    
    RichTextBox1.TextRTF = sRTFText 
    
End Sub

See Also

LoadResPicture Function, LoadResStringFunction

Sidebar 6. The VB6 Resource Editor Add-In

Users of VB6 can load a Resource Editor Add-In that greatly simplifies the task of creating and managing .RES files within a project. For users of VB5, the add-in is a free download from the Owners Area of the Visual Basic web site. Figure 7.5 shows the resource editor loaded with the resource file used for the LoadResData, LoadResPicture, and LoadResString examples.


Figure 7.5. The Visual Basic Resource Editor Add-In

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.105