|
|
Home » Visual Basic » Article
Beginning Resource Files in Visual Basic
|
| Article by: | Kenneth Ives (3/30/2003) |
|
| Summary: | Have you ever wanted to use graphics, such as icons, bitmaps, cursors, and AVI files? How about sound or even message box text? This can be an enormous amount of overhead. These are all examples of more files to keep track of when you distribute an application, DLL, or OCX. Lets find out how resource files can help.... |
|
| Viewed: 97759 times |
Rating (43 votes): |
|
4.6 out of 5 |
|
|
|
Beginning Resource Files in Visual Basic
Introduction
Have you ever wanted to use graphics, such as icons, bitmaps, cursors, and AVI files? How about sound or even message box text? This can be an enormous amount of overhead. These are all examples of more files to keep track of when you distribute an application, DLL, or OCX. Lets find out how resource files can help....
Getting Started
Now, let’s get started. First thing we want to do is start Visual Basic 6.0. We will begin a new project. Now select the "Add-Ins" option on the menu bar, and then the "Add-In Manager…". Scroll down the list and find "VB 6 Resource Editor".
Figure1
Highlight and place a check mark in the "Loaded/Unloaded" box, if this is the only time you are going to access the editor. If you are going to access the editor on more than one occasion, then also place a check mark in the box "Load on Startup". Click the "OK" button. Now every time you start Visual Basic, you will see a new item at the bottom of the drop down menu under "Projects" named "Add New Resource File".
To begin with, we have to set up a place to save this new project. Select "File", "Save Project". Create a folder to save this project. At this time you may want to name your form or project.
Go to the menu bar and select "Project", "Add New Resource File". A window will open, prompting you for the name of a resource file. (See figure 2) Navigate to where you saved your new project and type on the filename line the name of your new resource file. (ex: MyFile.res) Select "Open". You will prompted with a verification screen to create the file, select "Yes".
Figure 2
In the project window, usually in the upper right corner, there is now a new item named "Related Documents". Click on the "+" and you will see a new entry named "MyFile.res".
Double click this file ("MyFile.res") and the resource editor window will open. If you drag your cursor slowly over the buttons, you will see a brief explanation of what each does. Before we go any further, you must understand that the resource file is just that, a resource (repository, storage location, or permanent holding area). Whatever is easier for you to remember.
Adding some resources.
First, we will add a couple of icons. Select the icon button, as shown in figure 3. Navigate to where you have some icons stored. (ex: C:\Program Files\Microsoft Visual Studio\ Common\ Graphics\Icons) Find one that represents STOP and another to represent START
Figure 3
One thing you have probably noticed is the names of these two icons have been changed. They are under the heading "Icons" and named 101 and 102. If you want to make sure as to what they look like, right mouse click one of them and select properties. These new names are known as their "Name ID". This is important information to be used later. Close out of the resource editor window.
Add a command button to the form. Highlight the command button and go to the properties window. Find the "STYLE" property and change it to "1 – Graphical". Without this property being set, we cannot change the button color or place an icon on the button, both would be ignored. Set the height and width of this control to 1000. Figure 4 shows what you are looking at. Adjust accordingly.
>
Figure 4
Get coding!
Open the code window for this form and paste the following code:
Private Sub Command1_Click()
' Here is where we alternate the icons and the captions
If Command1.Caption = "Start" Then
Command1.Picture = LoadResPicture(101, vbResIcon)
Command1.Caption = "Stop"
Else
Command1.Picture = LoadResPicture(102, vbResIcon)
Command1.Caption = "Start"
End If
End Sub
Private Sub Form_Load()
' paste the icon on the face of the command button
' and set the caption. Look in the online help for
' additional information on the second parameter
Command1.Picture = LoadResPicture(102, vbResIcon)
Command1.Caption = "Start"
Show
End Sub
Press F5 and run it. Click on the button a few times.
I bet you feel that this is an awful lot of running around just to change an icon. Well, it isn’t.
Conclusion
Consider this, you have written a wonderful application with multiple forms. Each of which have two or more command buttons, a picture control or two, image controls, etc. Let your imagination run with this. You decide that instead of a green button to represent START on all of your forms, you want to use the little car icon and you want to use a stop sign icon to represent STOP. You also found a better icon to represent exiting. You also want to change those message boxes to say something different.
How do you make these changes?
Do we open ALL the forms, highlight each command button and change the picture? Or do we change the icon in the resource file and recompile? Tough decision? I don’t think so. I prefer the second alternative.
The commands to remember are:
LoadResData - For loading AVI and WAV files. These are stored as binary arrays.
LoadResPicture – For loading the icons, bitmaps, and cursors
LoadResString – For loading those unique message box texts that are so meaningful.
How about a change of languages? After all, everything in the resource file is indexed.
Read your online help for each of these commands. I have kept this as simple as possible. Only you and your imagination can limit your abilities.
Please write if you have any questions.
Kenneth Ives kenaso@attbi.com
|
|
View highlighted Comments
User Comments on ' Beginning Resource Files in Visual Basic'
|
Posted by :
Archive Import (Sumbit) at 16:42 on Monday, May 19, 2003
|
Curious, I'm trying to use these Resource IDs in the registry to associate an icon with a particular file extension that my app uses. However, these VB resources don't seem to be accessible the way other resources are to the registry's DefaultIcon function.
Specifically, I'm setting DefaultIcon to:
"C:\myapp.exe,101"
but the icon doesn't work.
When I set it to:
"C:\myapp.exe" it shows the first icon just fine.
Any suggestions?
| |
Posted by :
Archive Import (Sumbit) at 16:53 on Monday, May 19, 2003
|
To answer my own question...
It would seem that the registry reference to DefaultIcon uses an ordinal reference to the icon in the resource and not the cardinal '101','102',... that VB uses. Therefore, to reference the default icon 101 in the registry the key value would be:
"C:\myapp.exe,1"
| |
Posted by :
Archive Import (Bradley S.) at 12:07 on Wednesday, May 28, 2003
|
HOw do i create a source code for a visual basic program ... i want to use a wave file in my Visual basic Program.
Can u help me... any recomended websites?
| |
Posted by :
Archive Import (ching) at 15:55 on Thursday, July 24, 2003
|
how to create a email system and the coding regarding to the inventory system. to inform that the user that the stock is low.
| |
Posted by :
Archive Import (David) at 05:50 on Tuesday, August 19, 2003
|
I assume that to open a .WAV file, you first go into your Resource Editor and select the icon with four grey squares, second from the right, and then select the .WAV you wish to use. I have got this far. I need to use the line LoadResData but where? In the example programmes its dealing with pictures
Command1.Picture = LoadResPicture(102, vbResIcon)
I want to type in:
x?x?x?x = LoadResData (103, "CUSTOM) to load the .WAV file - someone help me!!
| |
Posted by :
Archive Import (asif) at 00:29 on Monday, September 08, 2003
|
Hello
Can any one help me in specifying different icons of different sizes for different places for an application. Like icon in start menu, in the windows explorer, in the status bar, in the title bar. etc.
please help
| |
Posted by :
Archive Import (dave) at 23:49 on Sunday, September 14, 2003
|
Hey asif, about the different icon sizes at different places of the application. look into snico edit.
http://snidesoft.cjb.net/
Check this program out and you will be efficiently use the mysterious icon...
| |
Posted by :
spiketool at 03:33 on Saturday, July 17, 2004
|
'This is kind of a cludge but...it works
'I store binary files (even other executables) in some of my resource files
'and with the old school Put function you can write them back
'to the disk and read them, like I did in this example (with Windows Media Player 9 ocx)
'with other applications, controls or plugins.
Private Sub Command1_Click()
Dim snd() As Byte
Dim myWav As String
myWav = App.Path & "\temp" & FreeFile() & ".wav" 'make a sequential file name
snd = LoadResData(101, "CUSTOM") 'load the raw bytes into an array
WindowsMediaPlayer1.URL = "" 'reset the player
Open myWav For Binary As 1 'open, put, and close the .wav file
Put #1, , snd
Close #1
WindowsMediaPlayer1.URL = myWav 'play it
WindowsMediaPlayer1.Controls.play
End Sub
| |
Posted by :
ludmila at 15:25 on Thursday, May 12, 2005
|
Is possible to insert an icon in a application (a window) developed in VBA into Excel?
How can I do it?
| |
Posted by :
abouelela at 17:10 on Saturday, March 08, 2008
|
It is very interseting codes and i learned much ,thanks
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
Generate License Keys (such as CD keys on Microsoft software) by Brian Gillham
This class allows you to generate license keys, such as the CD keys on the case of most Microsoft software |
 |
Communicating with the Database (Using ADO) by Chaudhary Pradeep K. Roy
Using ADO to get connected with the database. |
 |
Visual Basic Read and Modify the Registry by Kenneth Ives
Perform the four basic functions to the Windows registry. Add, change, delete, and query. Allows you to to read registry values, and modify both keys and values.
|
 |
Generate an Array of Unique Random Numbers by Brian Gillham
This function will generate an ARRAY of TRULY random numbers. |
 |
Beginning Resource Files in Visual Basic by Kenneth Ives
Have you ever wanted to use graphics, such as icons, bitmaps, cursors, and AVI files? How about sound or even message box text? This can be an enormous amount of overhead. These are all examples of more files to keep track of when you distribute an application, DLL, or OCX. Lets find out how resource files can help.... |
 |
Visual Basic Compact/Repair Access Database Utility by Kenneth Ives
This utility will compact and repair the access database you select. It is actually more convenient to use this utility than do the same task with Access, because you don't have to save the database with a different name and then delete the old database when you are done: here, a temporary copy of the database is automatically created and deleted. |
 |
Creating a watermark in Excel with VBA by Kenneth Ives
Adding a watermark to an Excel spreadsheet using VBA. |
 |
Change Cursor to Hour Glass by Brian Gillham
A simple script to change the cursor to an hour glass. |
 |
Generate your own Random Numbers by Thaha Hussain
This simple program generates random numbers using the basic mathematical methods. |
 |
Analog Clock by Thaha Hussain
This program demonstrates Thaha Hussain's Clock Work Formula to paint an Analogue Clock. |
 |
| |