Several very useful functions under VB

zhaozj2021-02-08  236

Several very useful functions under VB '-------- (1) ------------ "Get all key values ​​next to a section in the INI file, need the following API declaration 'Private declare Function GetPrivateProfileSection Lib "kernel32" Alias ​​"GetPrivateProfileSectionA" (ByVal lpAppName As string, ByVal lpReturnedString As string, ByVal nSize As Long, ByVal lpFileName As string) As Long' returns an array of strings' call: 'DIM Arrclass () AS String'Arrclass = GetInfosection ("Class", "D: /TYPE.INI")

Public Function GetInfoSection (strSection As String, strIniFile As String) As String () Dim strReturn As String * 32767 Dim strTmp As String Dim nStart As Integer, nEnd As Integer, i As Integer Dim sArray () As String Call GetPrivateProfileSection (strSection, strReturn , Len (strreturn), STRINIFILE, STRTMP = STRRETURN I = 1 Do WHILE STRTMP <> "" NSTART = NEND 1 Nend = INSTART, STRETURN, VBNULLCHAR) STRTMP = MID $ (Strreturn, NStart, Nend - NStart) Len (stramp)> 0 THEN Redim preserve sarray (1 to i) Sarray (i) = stramp i = i 1 end if loop getInfosection = SarrayEnd Function

'-------- (2) ------------' Role: Remove the first tail space in the string, all invalid characters 'test cases' Dim Strres as string'dim strsour AS String'Srsour = "" & VBNullChar & VBNullChar & "AB CD" & vbnullchar'strres = zqtrim (strsour) 'MSGBox "length =" & len (strsour) & "value = 111" & strres & "222" public function zqTrim (ByVal strSour As String) As String Dim strTmp As String Dim nLen As Integer Dim i As Integer, j As Integer Dim strNow As String, strValid () As String, strNew As String 'strNow current character' strValid valid characters' strNew final Generated New Character STRTMP = TRIM $ (strsour) Nlen = LEN (stratmp) if Nlen <1 Then Zqtrim = "" EXIT FUNCTION END IF J = 0 for i = 1 To Nlen Strnow = MID (Strtmp, I, 1) ' Read a character 'msgbox ASC (STRNOW) if strnow <> vbnullchar and asc (strnow) <> 9 Then' If it is valid, save the valid array Redim Preserve Strvalid (J) Strvalid (j) = Strnow J = J 1 end if next i strnew = join (strValid, ") 'Connect all valid characters ZQTRIM = TRIM $ (strnew)' Remove the first tail space in the string Function '-------- (3) ----------' Check if the file exists, there is returns true, otherwise returns falsepublic function checkfileexist (strfile as string) AS Boolean if Dir Strfile, vbdirectory) <> "" THEN CHECKFILEEXIST = true else checkfileexist = false end ifend function

'-------- (4) ---------- "Get the key value of a subkey next to the specified INI file, you need the following API declaration' Public 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' return A string 'calling example:' Dim strrun as string'strrun = getinivalue ("Windows", "run", "c: /windows/win.ini") public function getinivalue (byval lpkeyname as string, byval strname as string, ByVal strIniFile As String) As String Dim strTmp As String * 255 Call GetPrivateProfileString (lpKeyName, strName, "", _ strTmp, Len (strTmp), strIniFile) GetiniValue = Left $ (strTmp, InStr (strTmp, vbNullChar) - 1) End Function

'-------- (5) ---------- "Get the Windows directory, you need the following API declaration' Private Declare Function GetWindowsDirectory LIB" kernel32 "Alias" getWindowsDirectorya "(Byval LPBuffer As string, byval nsize as long) AS long 'Returns a string, such as "C: / Windows", "C: / Winnt" call example:' Dim strwindir as string'strwindir = getWindir () private function getWindir () DIM WINDIR AS STRING * 100 Call GetWindowsDirectory (WINDIR, 100) GetWindir = Left $ (WINDIR, INSTR (WINDIR, VBNULLCHAR) - 1) End Function

转载请注明原文地址:https://www.9cbs.com/read-3219.html

New Post(0)