VB6.0读取INI参数,解决了中文后面的多出空格的问题,测试OK - 洛阳翼展电脑


VB6.0读取INI参数,解决了中文后面的多出空格的问题,测试OK

2025-2-20 乱云飞

Option Explicit

' API函数声明,用于读取INI文件
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 Sub Form_Load()
    Me.Hide
    Dim iniFilePath As String
    Dim 文本 As String
    Dim 坐标X As String
    Dim 坐标Y As String
    Dim 字体 As String
    Dim 颜色 As String
    Dim 字号 As String
    Dim 透明 As String
    Dim 时间 As String
    
    ' 指定INI文件路径
    iniFilePath = App.Path & "\logo.ini"
    
    ' 读取INI文件中的参数
    文本 = ReadIniValue("logo", "文本", iniFilePath)
    坐标X = ReadIniValue("logo", "坐标X", iniFilePath)
    坐标Y = ReadIniValue("logo", "坐标Y", iniFilePath)
    字体 = ReadIniValue("logo", "字体", iniFilePath)
    颜色 = ReadIniValue("logo", "颜色", iniFilePath)
    字号 = ReadIniValue("logo", "字号", iniFilePath)
    透明 = ReadIniValue("logo", "透明", iniFilePath)
    时间 = ReadIniValue("logo", "时间", iniFilePath)
    
    ' 生成完整的命令行
    Dim command As String
    command = "start text.dll" & _
              " /m:" & 文本 & _
              " /x:" & 坐标X & _
              " /y:" & 坐标Y & _
              " /f:" & 字体 & _
              " /fh:" & 字号 & _
              " /ftc:""" & 颜色 & """" & _
              " /o:" & 透明 & _
              " /t:" & 时间 & _
              " /fi:0 /fbc:""255 255 255"" /bc:""0 0 0"""
    
    '在窗口上显示生成的命令
    'Me.Caption = "生成命令行"
    'Me.AutoRedraw = True
    'Me.Print "生成的命令:"
    'Me.Print command
    
    ' 将生成的命令保存到1.bat文件中
    Dim FilePath As String
    FilePath = App.Path & "\1.bat"
    
    Open FilePath For Output As #1
    Print #1, command
    Close #1
    
    'MsgBox "命令已保存到 " & FilePath, vbInformation, "保存成功"
    Shell FilePath, vbHide '执行命令并显示命令行窗口
    End
End Sub

' 读取INI文件中的指定键值
Private Function ReadIniValue(ByVal Section As String, ByVal Key As String, ByVal FilePath As String) As String
    Dim Buffer As String
    Dim BufferSize As Long
    Dim RetVal As Long
    Buffer = String$(255, 0)  ' 初始化缓冲区
    BufferSize = Len(Buffer)
    '调用API函数读取INI文件中的值
    RetVal = GetPrivateProfileString(Section, Key, "", Buffer, BufferSize, FilePath)
    '根据chr(0)的位置来截取字符串
    ReadIniValue = Left$(Buffer, InStr(Buffer, Chr(0)) - 1)
End Function

本文链接:http://80c.cc/ez/812.html
0

评论:

乱云飞 发表于:2025-02-20 21:42
API函数是按ANSI编码来处理中文字符串的,而VB内部却是按Unicode编码来处理中文的,比如“欧阳飞”在API中认为是6个字符,而VB则认为是3个字符,这样VB如果按6来截取字符串,就会把“欧阳飞”后面的三个chr(0)也一起截出来咯!

所以,不能根据API函数返回的字符数来截取字符串,而应该自己检测chr(0)首次出现的位置来截取:

Public Function GetFromINI(AppName As String, KeyName As String, FileName As String) As String
    Dim RetStr As String
    RetStr = String(255, Chr(0))
    GetPrivateProfileString AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName
    GetFromINI = Left(RetStr, InStr(RetStr, Chr(0)) - 1)
End Function

发表评论:

VirtualCamera:在录像机中虚拟一个通道显示电脑桌面、视频文件、USB摄像头~
预ICP备10086-001号 © 翼展网/80C.CC 技术支持/洛阳翼展科技
TEL / 13213610060 QQ / 345794501
Powered by emlog