|
-
Jul 20th, 2004, 10:02 AM
#1
Thread Starter
Frenzied Member
Using Resource files
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,
VB Code:
Picture1.Picture = LoadResPicture(101, vbResBitmap)
Similarly
VB Code:
Image1.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.
To load a string use,
VB Code:
Text1.Text = LoadResString(101)
For cursors and icons,
VB Code:
Picture1.Picture = LoadResPicture(101, vbResIcon)
Picture1.Picture = LoadResPicture(101, vbResCursor)
These all assume that the file types are saved in the right section in the resource editor.
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
Last edited by Jmacp; Jan 7th, 2006 at 02:15 PM.
-
Jan 14th, 2006, 01:25 PM
#2
Hyperactive Member
Re: Using Resource files
Thanx 4 ur response, but how will i work with a font file ?
Nobody is smarter than all of us!
-
May 7th, 2008, 06:26 PM
#3
Junior Member
Re: Using Resource files
yeh and how does this work with a SWF FLASH file?
i have tried everything
-
Sep 8th, 2009, 03:47 PM
#4
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|