⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cinifile.cls

📁 CELL-ID技术是目前最简单的定位技术
💻 CLS
字号:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CIniFile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-------------读写 INI 文件可能用到的API 函数声明---------

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

Private 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 Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

'其中 WritePrivateProfileString 是用来向 INI 文件写信息的,
'而 GetPrivateProfileInt 和 GetPrivateProfileString 则是用来从 INI 文件中读信息的,
'前者用于读取整型数据,后者则用于读取字符串型数据

'---------------------------------------------------------


'----为类添加属性

Public IniFileName As String   '属性之一就是文件名

Public ErrorMsg As String  '保存错误信息的变量作为 CIniFile 的第二个属性




Private Sub Class_Initialize()   '对属性进行初始化
  IniFileName = vbNullString
  ErrorMsg = vbNullString
End Sub



Public Sub SpecifyIni(FilePathName)   '为了指定 INI 文件名给 CIniFile,需要定义一个方法
  IniFileName = Trim(FilePathName)
End Sub



Private Function NoIniFile() As Boolean   '在每次读写值之前还需要先判断是否已经指定了 INI 文件名
  NoIniFile = True
  If IniFileName = vbNullString Then
    ErrorMsg = "没有指定 INI 文件"
    Exit Function
  End If
  ErrorMsg = vbNullString
  NoIniFile = False
End Function



Public Function WriteString(Section As String, key As String, Value As String) As Boolean    '写 INI 文件
WriteString = False
If NoIniFile() Then
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
ErrorMsg = "写入失败"
Exit Function
End If
WriteString = True
End Function



Public Function ReadString(Section As String, key As String, Size As Long) As String  '读取字符串数据 INI 文件
Dim ReturnStr As String
Dim ReturnLng As Long
ReadString = vbNullString
If NoIniFile() Then
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, IniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function



Public Function ReadInt(Section As String, key As String) As Long   '读取整型数据 INI 文件
Dim ReturnLng As Long
ReadInt = 0
ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
If ReturnLng = 1 Then
ErrorMsg = "不能读取"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -