How to Create and Use Resources in Visual Basic 6

Create and Use Resources, After Visual Basic students study all approximately loops and conditional statements and subroutines. And so forth, one of the subsequent matters that they regularly ask approximately is.

“How do I add a bitmap, a wav record, a custom cursor or a few different unique effect?” One answer is useful resource files. When you upload a document using Visual Studio resource documents.

They may be included without delay into your Visual Basic undertaking for max execution speed and minimum trouble packaging and deploying your application.

Resource documents are available in each VB 6 and VB.NET, however the way they may be used, like the whole lot else, is quite a bit unique among the 2 structures.

Keep in mind that this is not the best manner to apply files in a VB challenge, but it has actual advantages. For instance, you can encompass a bitmap in a PictureBox control or use the mciSendString Win32 API.

“MCI” is a prefix that typically suggests a Multimedia Command String.

Creating a Resource File in VB 6

You can see the sources in a venture in each VB 6 and VB.NET within the Project Explorer window (Solution Explorer in VB.NET — they needed to make it just a little bit distinctive).

A new mission won’t have any due to the fact that resources aren’t a default device in VB 6. So let’s add a easy useful resource to a venture and notice how that is achieved.

Step one is to start VB 6 by means of deciding on a Standard EXE task at the New tab inside the startup conversation.

Now pick the Add-Ins option on the menu bar, after which the Add-In Manager. This will open the Add-In Manager dialog window.

Scroll down the list and find VB 6 Resource Editor. You can just double-click it or you may positioned a check mark inside the Loaded/Unloaded container to add this device on your VB 6 surroundings.

If you believe you studied you are going to use the Resource Editor a lot, then you may additionally region a check mark within the box Load on Startup and also you may not should undergo this step again in the destiny.

Click “OK” and the Resources Editor pops open. You’re equipped to begin including resources in your undertaking!

Use Resources in Visual Basic

Go to the menu bar and select Project then Add New Resource File or simply right-click inside the Resource Editor and pick out “Open” from the context menu that pops up.

A window will open, prompting you for the call and region of a useful resource report. The default location will probable now not be what you need, so navigate to your undertaking folder and enter the name of your new resource report into the File name box.

In this newsletter, I’ll use the call “AboutVB.RES” for this file. You’ll have to confirm the advent of the record in a verification window, and the a “AboutVB.RES” file can be created and crammed into the Resource Editor.

Create and Use Resources, VB6 Supports

VB6 supports the subsequent:

  • A string desk editor(“Edit String Tables…”)
  • Custom cursors – “CUR” documents(“Add Cursor…”)
  • User icons – “ICO” files(“Add Icon…”)
  • Custom bitmaps – “BMP” documents(“Add Bitmap…”)
  • Programmer defined assets
  • (“Add Custom Resource…”)

VB 6 presents a easy editor for strings but you need to have a file created in some other tool for all the different alternatives. For instance, you may create a BMP report the usage of the easy Windows Paint application.

Create and Use Resources, Each aid in the resource record is recognized to VB 6 by way of an Id and a call inside the Resource Editor.

To make a aid available for your program, you add them within the Resource Editor after which use the Id and the resource “Type” to point to them to your application. Let’s upload 4 icons to the resource report and use them in the application.

When you upload a aid, the real record itself is copied into your task. Visual Studio 6 provides an entire series of icons within the folder…

C:Program FilesMicrosoft Visual StudioCommonGraphicsIcons

To go along with subculture, we’ll pick out the Greek logician Aristotle’s 4 “elements” — Earth, Water, Air, and Fire — from the Elements subdirectory. When you add them, the Id is assigned by using Visual Studio (a hundred and one, 102, 103, and 104) automatically.

To use the icons in a program, we use a VB 6 “Load Resource” characteristic. There are numerous of these features to pick from:

Create and Use Resources, LoadResPicture(index, format) for bitmaps, icons, and cursors

Create and Use Resources, Visual Basic 6

Use the VB predefined constants vbResBitmap for bitmaps, vbResIcon for icons, and vbResCursor for cursors for the “format” parameter. This characteristic returns a photo that you can use directly. LoadResData (defined below) returns a string containing the real bits inside the document. We’ll see a way to use that when we reveal icons.

LoadResString(index) for strings LoadResData(index, format) for something as much as 64K

As referred to earlier, this function returns a string with the actual bits within the aid. These are the values that can be used for format parameter here:

  1. Cursor useful resource
  2. Bitmap aid
  3. Icon useful resource
  4. Menu aid
  5. Dialog container
  6. String resource
  7. Font listing aid
  8. Font aid
  9. Accelerator desk
  10. User-described aid
  11. Group cursor
  12. Group icon

Since we’ve four icons in our AboutVB.RES useful resource record, allow’s use LoadResPicture(index, format) to assign these to the Picture assets of a CommandButton in VB 6.

I created an software with four OptionButton additives classified Earth, Water, Air and Fire and four Click activities – one for every alternative.

Then I introduced a CommandButton and modified the Style property to “1- Graphical”.

Create and Use Resources, This is important to be able to upload a custom icon to the CommandButton.

The code for each OptionButton (and the Form Load event — to initialize it) seems like this (with the Id and Caption modified accordingly for the opposite OptionButton Click activities):

Private Sub Option1_Click()
Command1.Picture = _
      LoadResPicture(101, vbResIcon)
   Command1.Caption = _
      "Earth"
End Sub

Create and Use Resources, Custom Resources

The “big deal” with custom assets is that you usually have to provide a way to process them on your software code. As Microsoft states it, “this usually requires using Windows API calls.” That’s what we’ll do.

The instance we’ll use is a fast manner to load an array with a series of regular values. Remember that the aid record is protected into your task, so if the values which you want to load exchange, you’ll have to use a more conventional technique consisting of a sequential file that you open and study.

The Windows API we will use is the CopyMemory API. CopyMemory copies block of reminiscence to a exclusive block of memory with out regard to the records kind this is stored there. This method is widely recognized to VB 6’ers as an ultra fast manner to replicate information inner a application.

This program is a chunk extra involved because first we ought to create the a useful resource report containing a series of long values.

Create and Use Resources, I certainly assigned values to an array:

Dim longs(10) As Long
longs(1) = 123456
longs(2) = 654321

… and so forth.

Create and Use Resources, Create and Use

Then the values can be written to a file called MyLongs.longs using the VB 6 “Put” statement.

Dim hFile As Long
hFile = FreeFile()
Open _
   "C:\your file path\MyLongs.longs" _
   For Binary As #hFile
Put #hFile, , longs
Close #hFile

It’s an excellent concept to understand that the aid document does not alternate unless you delete the antique one and add a brand new one.

So, using this method, you will need to replace the program to change the values. To encompass the document MyLongs.Longs into your software as a aid, add it to a aid record the use of the identical steps defined above, but click on the Add Custom Resource..

Instead of Add Icon… Then pick the MyLongs.Longs file because the file to add. You also must exchange the “Type” of the resource by means of proper clicking that aid, deciding on “Properties”, and converting the Type to “longs”. Note that that is the report sort of your MyLongs.Longs document.

Create and Use Resources, To use the aid file you’ve got created to create a brand new array, first declare the Win32 CopyMemory API call:

Private Declare Sub CopyMemory
Lib "kernel32" Alias _
   "RtlMoveMemory" (Destination As Any, _
   Source As Any, ByVal Length As Long)

Then read the resource file:

Create and Use Resources, Visual Basic 6

Dim bytes() As Byte
bytes = LoadResData(101, "longs")

Next, flow the facts from the bytes array to an array of long values. Allocate an array for the longs values the usage of the integer value of the length of the string of bytes divided via four (that is, four bytes in keeping with long):

ReDim longs(1 To (UBound(bytes)) \ 4) As Long  
CopyMemory longs(1), bytes(0), UBound(bytes) - 1

Now, this could look like an entire lot of trouble when you may just initialize the array in the Form Load event. But it does show a way to use a custom resource.

If you had a big set of constants that you needed to initialize the array with. It might run quicker than some other method I can think of. And you would not ought to have a separate record protected with your application to do it.