Option Explicit Public InifileName As String Declare Function GetPrivateProfileString Lib "kernel32" Alias _ "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal _ lpReturnedString As String, ByVal nSize As Long, _ ByVal lpFileName As String) As LongDeclare Function WritePrivateProfileString Lib "kernel32" Alias _ "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, ByVal lpString As Any, _ ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long '从ini文件里读取字符串 Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, _ ByVal DefString As String) As String Dim ResultString As String * 256 Dim Temp As Integer Temp = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 256, InifileName) If Temp > 0 Then ResultString = Trim(ResultString) Else ResultString = DefString End If GetIniS = delspace(ResultString) End Function '从ini文件里读取数字 Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, _ ByVal DefValue As Integer) As Integer GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, InifileName) End Function '写字符串入ini文件里 Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, _ ByVal ValStr As String) Dim res As Integer res = WritePrivateProfileString(SectionName, KeyWord, ValStr, InifileName) End Sub '写数字入ini文件里 Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, _ ByVal ValInt As Integer) Dim res As Integer, s As String s = Str$(ValInt) res = WritePrivateProfileString(SectionName, KeyWord, s$, InifileName) End Sub '例 '用ini文件保存窗体属性 Public Sub SFP(frmname As Form) On Error Resume Next SetIniN frmname.name, "top", frmname.Top SetIniN frmname.name, "left", frmname.Left SetIniN frmname.name, "width", frmname.width SetIniN frmname.name, "height", frmname.Height SetIniN frmname.name, "windowstate", frmname.WindowState End Sub '从ini文件中读取窗体属性 Public Sub GfP(frmname As Form) On Error Resume Next Dim itemp As Integer
itemp = GetIniN(frmname.name, "width", 0) If itemp <> 0 Then frmname.width = GetIniN(frmname.name, "width", 0) End If itemp = GetIniN(frmname.name, "height", 0) If itemp <> 0 Then frmname.Height = GetIniN(frmname.name, "height", 0) End If frmname.Top = GetIniN(frmname.name, "top", (Screen.Height - frmname.Height) / 2) frmname.Left = GetIniN(frmname.name, "left", (Screen.width - frmname.width) / 2) frmname.WindowState = GetIniN(frmname.name, "windowstate", 0) End Sub
Public InifileName As String
Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal _
lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As LongDeclare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
'从ini文件里读取字符串
Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, _
ByVal DefString As String) As String
Dim ResultString As String * 256
Dim Temp As Integer
Temp = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 256, InifileName)
If Temp > 0 Then
ResultString = Trim(ResultString)
Else
ResultString = DefString
End If
GetIniS = delspace(ResultString)
End Function
'从ini文件里读取数字
Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, _
ByVal DefValue As Integer) As Integer
GetIniN = GetPrivateProfileInt(SectionName, KeyWord, DefValue, InifileName)
End Function
'写字符串入ini文件里
Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, _
ByVal ValStr As String)
Dim res As Integer
res = WritePrivateProfileString(SectionName, KeyWord, ValStr, InifileName)
End Sub
'写数字入ini文件里
Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, _
ByVal ValInt As Integer)
Dim res As Integer, s As String
s = Str$(ValInt)
res = WritePrivateProfileString(SectionName, KeyWord, s$, InifileName)
End Sub
'例
'用ini文件保存窗体属性
Public Sub SFP(frmname As Form)
On Error Resume Next
SetIniN frmname.name, "top", frmname.Top
SetIniN frmname.name, "left", frmname.Left
SetIniN frmname.name, "width", frmname.width
SetIniN frmname.name, "height", frmname.Height
SetIniN frmname.name, "windowstate", frmname.WindowState
End Sub
'从ini文件中读取窗体属性
Public Sub GfP(frmname As Form)
On Error Resume Next
Dim itemp As Integer
itemp = GetIniN(frmname.name, "width", 0)
If itemp <> 0 Then
frmname.width = GetIniN(frmname.name, "width", 0)
End If
itemp = GetIniN(frmname.name, "height", 0)
If itemp <> 0 Then
frmname.Height = GetIniN(frmname.name, "height", 0)
End If
frmname.Top = GetIniN(frmname.name, "top", (Screen.Height - frmname.Height) / 2)
frmname.Left = GetIniN(frmname.name, "left", (Screen.width - frmname.width) / 2)
frmname.WindowState = GetIniN(frmname.name, "windowstate", 0)
End Sub