Results 1 to 7 of 7

Thread: Load files outside the program

  1. #1

    Thread Starter
    Addicted Member Mojtaba's Avatar
    Join Date
    Dec 2020
    Posts
    168

    Load files outside the program

    Hi guys
    I want to call all the files outside the program and in a single file (external resource file)
    I did this with the help of PBag Resources but it doesn't read all the files
    Text files and images are called, but there is a problem with other files
    For example, it cannot read the file to install the font

    HTML Code:
    Option Explicit
    Private Declare Function AddFontResourceEx Lib "gdi32" Alias "AddFontResourceExA" _
    (ByVal sFIleName As String, ByVal lFlags As Long, ByVal lReserved As Long) As Long
    Private Const FR_PRIVATE As Long = &H10
    Public Function InstallFont(pFontPath As String) As Long
    InstallFont = AddFontResourceEx(pFontPath, FR_PRIVATE, 0&)
    End Function
    Private Sub Form_Load()
    Dim BagFile As Integer
    Dim ContentBytes() As Byte
    BagFile = FreeFile(0)
    Open "bag.dat" For Binary Access Read As #BagFile
    ReDim ContentBytes(LOF(BagFile) - 1)
    Get #BagFile, , ContentBytes
    Close #BagFile
    With New PropertyBag
    .Contents = ContentBytes
    lblBurger.Caption = .ReadProperty("Burger.txt")
    lblFries.Caption = .ReadProperty("Fries.txt")
    InstallFont (.ReadProperty("MyFont.ttf"))
    End With
    Text1.Font.Name = "MyFont"
    End Sub
    It gives an error that the data value named "MyFont.ttf" was not found
    How can I call all the files?
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,892

    Re: Load files outside the program

    Did you save it in the property bag properly? You can't just reference another file and have it automatically load.

  3. #3
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,119

    Re: Load files outside the program

    From the property bag, you get the font in binary form and need to save it to a file before you are able to use the AddFontResourceEx API that is called in the InstallFont function.
    It can be a temporary file, but then you should delete it before the programs finishes.

  4. #4
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,747

    Re: Load files outside the program

    u should not "hide" fonts.
    why not just add a folder: "fonts" and place whatever font (that is free to use) there?

    another solution is to use "bitmap-fonts". meaning, u have a picture (.png, .bmp etc) that has all the letters
    and using a "function" (like alphablend) u can render it into a text in any picturebox u want.
    a .png u can store as resource and u can easily load that into memory and use gdi32/gdi+ to render.
    its not hard to create a "custom" bitmap-font-text-render-function.

  5. #5
    Addicted Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    249

    Re: Load files outside the program

    You can also pack the font into a RES as a custom resource. With LoadResData you get the font into a ByteArray. CoTaskMemAlloc(SizeOf ByteArray) -> hMem. CopyMemory ByteArray to hMem. AddFontMemResourceEx(hMem) -> hFont. CoTaskMemFree(hMem). Save the hFont and the font name of the font in a Type or in a TypeArray if there are several fonts. You can then use the font in your application like any other font. If the font is no longer needed, release it with RemoveFontMemResourceEx(hFont). Alternatively, you can also load the font file with AddFontResourceEx. You can also read the font name of TTF fonts from the file or from the byte array -> https://www.codeproject.com/Articles...-from-TTF-File or via GetFontData(hFont)
    Last edited by -Franky-; Apr 18th, 2024 at 06:25 AM.

  6. #6
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,537

    Question Re: Load files outside the program

    What's the point of the additional call to CopyMemory? You should be able to pass VarPtr(ByteArray(0)) directly to AddFontMemResourceEx...

  7. #7
    Addicted Member -Franky-'s Avatar
    Join Date
    Dec 2022
    Location
    Bremen Germany
    Posts
    249

    Re: Load files outside the program

    Quote Originally Posted by VanGoghGaming View Post
    What's the point of the additional call to CopyMemory? You should be able to pass VarPtr(ByteArray(0)) directly to AddFontMemResourceEx...
    My mistake. You're right. Then CoTaskMemAlloc, CopyMemory and CoTaskMemFree are not needed.

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