VB下几个非常有用的函数

发布时间:2010年04月28日      浏览次数:330 次
vb下几个非常有用的函数
————————(1)————————————
获得指定ini文件中某个节下面的所有键值 truezq,,需要下面的api声明
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
返回一个字符串数组
调用举例:
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 = instr(nstart, strreturn, vbnullchar)
strtmp = mid$(strreturn, nstart, nend - nstart)
if len(strtmp) > 0 then
redim preserve sarray(1 to i)
sarray(i) = strtmp
i = i + 1
end if

loop
getinfosection = sarray
end function
————————(2)————————————
作用:去掉字符串中的首尾空格、所有无效字符
测试用例
dim strres as string
dim strsour as string
strsour = " " & vbnullchar & vbnullchar & " ab cd" & vbnullchar
strres = zqtrim(strsour)
msgbox " 长度=" & len(strsour) & "值=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 当前字符
strvalid 有效字符
strnew 最后生成的新字符

strtmp = trim$(strsour)
nlen = len(strtmp)
if nlen < 1 then
zqtrim = ""
exit function
end if
j = 0
for i = 1 to nlen
strnow = mid(strtmp, i, 1) 每次读取一个字符
msgbox asc(strnow)
if strnow <> vbnullchar and asc(strnow) <> 9 then 如果有效,则存入有效数组
redim preserve strvalid(j)
strvalid(j) = strnow
j = j + 1
end if

next i

strnew = join(strvalid, "") 将所有有效字符连接起来
zqtrim = trim$(strnew) 去掉字符串中的首尾空格
end function
————————(3)————————————
检查文件是否存在,存在返回 true,否则返回false
public function checkfileexist(strfile as string) as boolean

if dir(strfile, vbdirectory) <> "" then
checkfileexist = true
else
checkfileexist = false
end if
end function
————————(4)————————————
获得指定ini文件中某个节下面某个子键的键值,需要下面的api声明
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
返回一个字符串
调用举例:
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)————————————
获得windows目录 ,需要下面的api声明
private declare function getwindowsdirectory lib "kernel32" alias "getwindowsdirectorya" (byval lpbuffer as string, byval nsize as long) as long
返回一个字符串,如“c:\windows”、“c:\winnt”
调用举例:
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
————————(6)————————————
获得windows系统目录,需要下面的api声明
private declare function getsystemdirectory lib "kernel32" alias "getsystemdirectorya" (byval lpbuffer as string, byval nsize as long) as long
返回一个字符串,如“c:\windows\system”、“c:\winnt\system32”
调用举例:
dim strsysdir as string
strsysdir = getsystemdir()
private function getsystemdir()
dim strsysdir as string * 100
call getsystemdirectory(strsysdir, 100)
getsystemdir = left$(strsysdir, instr(strsysdir, vbnullchar) - 1)

end function
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!