Results 1 to 4 of 4

Thread: Using Resource files

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    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:
    1. Picture1.Picture = LoadResPicture(101, vbResBitmap)
    Similarly
    VB Code:
    1. 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:
    1. Text1.Text = LoadResString(101)
    For cursors and icons,
    VB Code:
    1. Picture1.Picture = LoadResPicture(101, vbResIcon)
    2. 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:
    1. Private Sub Command1_Click()
    2. Dim b()  As Byte
    3. Open App.Path & "\b.jpg" For Binary Access Write As #1
    4.     b = LoadResData(101, "custom")
    5.     Put #1, , b
    6.     Close #1
    7. Picture1.Picture = LoadPicture(App.Path & "\b.jpg")
    8. 'kill App.Path & "\b.jpg"
    9. End Sub


    Examples

    Load and register an OCX from a resource file.

    In a module,

    VB Code:
    1. Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    2.                                                                                         ByVal nSize As Long) As Long
    3.  
    4. Private Sub main()
    5.  
    6.     LoadandregOcx "comdlg32", "MyOcx"
    7.  
    8. End Sub
    9.  
    10. Private Sub LoadandregOcx(ByVal OcxName As String,ByVal resID As String)
    11.  
    12. Dim b() As Byte
    13.  
    14.     Open SysDir & "\" & OcxName & ".ocx" For Binary Access Write As #1
    15.     b = LoadResData(resID, "custom")
    16.     Put #1, , b
    17.     Close #1
    18.     Shell "regsvr32 /s " & OcxName & ".ocx"
    19.     frmMain.Show
    20.  
    21. End Sub
    22.  
    23. Private Function SysDir() As String
    24.  
    25. Dim slen As String
    26.  
    27.     slen = String$(145, vbNullChar)
    28.     SysDir = Left$(slen, GetSystemDirectory(slen, Len(slen)))
    29.  
    30. End Function


    To play an AVI file in using the animation control,

    VB Code:
    1. Option Explicit
    2. Private Const WM_USER = &H400&
    3. Private Const ACM_OPEN = WM_USER + 100&
    4. 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
    5. Private Sub Command1_Click()
    6.  
    7.     Dim ResID As Long
    8.     ResID = 101
    9.  
    10.  SendMessage Animation1.hwnd, ACM_OPEN, App.hInstance, ResID
    11.  
    12. End Sub


    To play a Wav,

    VB Code:
    1. Option Explicit
    2. Private Const SND_ASYNC     As Long = &H1
    3. Private Const SND_MEMORY    As Long = &H4
    4. Private Const SND_NODEFAULT = &H2
    5. Private Const Flags& = SND_ASYNC Or SND_NODEFAULT Or SND_MEMORY
    6. Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
    7.                                                                              ByVal uFlags As Long) As Long
    8. Private Sub Command1_Click()
    9.  
    10. Dim b As String
    11.      b = StrConv(LoadResData(101, "custom"), vbUnicode)
    12.     sndPlaySound b, Flags&
    13.    
    14. 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:
    1. Private Sub command1_click()
    2. Dim StrString()   As String
    3.     StrString = Split(StrConv(LoadResData(101, "custom"), vbUnicode), vbNewLine)
    4.     For i = LBound(StrString) To UBound(StrString)
    5.         List1.AddItem StrString(i)
    6.     Next i
    7.     'Erase StrString
    8. End Sub
    Last edited by Jmacp; Jan 7th, 2006 at 02:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width