回覆列表
  • 1 # 使用者4153841429888

    利用系統API函式 GetPrivateProfileString 可以方便地讀取ini檔案。使用方法如下

    (1)MyApp.INI檔案的內容為

    VB程式讀取這個ini檔案,將視窗的標題換為Title指定的字串

    (2)新建一個VB工程

    (3)Form1窗體程式碼

    Option Explicit

    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" _

    (ByVal lpApplicationName As Long, _

    ByVal lpKeyName As Long, _

    ByVal lpDefault As Long, _

    ByVal lpReturnedString As Long, _

    ByVal nSize As Long, _

    ByVal lpFileName As Long) As Long

    "------------

    "讀INI檔案

    "------------

    Private Function GetValueFromINIFile(ByVal SectionName As String, _

    ByVal KeyName As String, _

    ByVal IniFileName As String) As String

    Dim strBuf As String

    "128個字元,初始化時用 0 填充

    strBuf = String(128, 0)

    GetPrivateProfileString StrPtr(SectionName), _

    StrPtr(KeyName), _

    StrPtr(""), _

    StrPtr(strBuf), _

    128, _

    StrPtr(IniFileName)

    "去除多餘的 0

    strBuf = Replace(strBuf, Chr(0), "")

    GetValueFromINIFile = strBuf

    End Function

    Private Sub Form_Load()

    Dim title As String

    "讀取INI檔案中指定的節和節/鍵

    "節的名稱:AppName

    "鍵名稱:Title

    title = GetValueFromINIFile("AppName", "Title", "c:\MyApp.INI")

    Me.Caption = title

    End Sub

    (4)執行效果

    視窗的標題被設定Ini檔案指定的字串!

  • 中秋節和大豐收的關聯?
  • 為什麼彩虹是拱形的?