My first attempt at a tutorial/code.
Resource Files
Theory
Resource files are files of any type that can be added to your project.
They are most commonly used for storing bitmaps, strings, cursors, icons and sounds for use in the application.
Reasons for storing are usually for security or to keep the application as a single file or just reduce the number of files of distribution needed.
Method
In Add-in/Add-in manager, select resource editor after which you should see a green icon appear in the toolbar. Clicking this will bring up the resource editor GUI. There are 4 standard file types namely, cursors, icons, string and bitmaps. Any other file type must be added as a ‘custom’ resource. So basically just add your file(s) then save which will link it to the project.
Usage
To add a bitmap to a picturebox, for example, from the resource file, simply add,
SimilarlyVB Code:
Picture1.Picture = LoadResPicture(101, vbResBitmap)
The 101 is just an identifier number, 101 is the default starting number for each initial file added in any particular group. The second parameter is obviously the resource file type.VB Code:
Image1.Picture = LoadResPicture(101, vbResBitmap)
To load a string use,
For cursors and icons,VB Code:
Text1.Text = LoadResString(101)
These all assume that the file types are saved in the right section in the resource editor.VB Code:
Picture1.Picture = LoadResPicture(101, vbResIcon) Picture1.Picture = LoadResPicture(101, vbResCursor)
Other file types
To load a different file type than these you must save your file as a custom resource, again in the editor.
For example to load a jpg into a picture box first save the jpg to the hard drive and then load it into the picture box using LoadPicture.
VB Code:
Private Sub Command1_Click() Dim b() As Byte Open App.Path & "\b.jpg" For Binary Access Write As #1 b = LoadResData(101, "custom") Put #1, , b Close #1 Picture1.Picture = LoadPicture(App.Path & "\b.jpg") 'kill App.Path & "\b.jpg" End Sub
Examples
Load and register an OCX from a resource file.
In a module,
VB Code:
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long Private Sub main() LoadandregOcx "comdlg32", "MyOcx" End Sub Private Sub LoadandregOcx(ByVal OcxName As String,ByVal resID As String) Dim b() As Byte Open SysDir & "\" & OcxName & ".ocx" For Binary Access Write As #1 b = LoadResData(resID, "custom") Put #1, , b Close #1 Shell "regsvr32 /s " & OcxName & ".ocx" frmMain.Show End Sub Private Function SysDir() As String Dim slen As String slen = String$(145, vbNullChar) SysDir = Left$(slen, GetSystemDirectory(slen, Len(slen))) End Function
To play an AVI file in using the animation control,
VB Code:
Option Explicit Private Const WM_USER = &H400& Private Const ACM_OPEN = WM_USER + 100& Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long Private Sub Command1_Click() Dim ResID As Long ResID = 101 SendMessage Animation1.hwnd, ACM_OPEN, App.hInstance, ResID End Sub
To play a Wav,
VB Code:
Option Explicit Private Const SND_ASYNC As Long = &H1 Private Const SND_MEMORY As Long = &H4 Private Const SND_NODEFAULT = &H2 Private Const Flags& = SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _ ByVal uFlags As Long) As Long Private Sub Command1_Click() Dim b As String b = StrConv(LoadResData(101, "custom"), vbUnicode) sndPlaySound b, Flags& End Sub
to load a text file into say a listbox, text file being saved as a custom file and in the form
line1
line2
line3 etc..
VB Code:
Private Sub command1_click() Dim StrString() As String StrString = Split(StrConv(LoadResData(101, "custom"), vbUnicode), vbNewLine) For i = LBound(StrString) To UBound(StrString) List1.AddItem StrString(i) Next i 'Erase StrString End Sub




Reply With Quote