用vb实现编程离不开函数调用及windows api函数的调用,以下是笔者收集的一些实用的小例程,它们可以直接用在你的实际编程中,也可以根据实际应用加以扩充完善。其中涉及windows api函数调用的代码你可以从vb5.0系统的api函数查看器中复制函数定义内容,以避免出错。
1.系统型表单
系统型意味着用户完成当前表单操作之前无法进行其它操作,这对于编制系统口令保护界面尤为重要。如果你希望当前表单系统型表单,需如下定义api函数:
declare function setsysmodalwindow lib "user" (byval hwnd as integer) as integer
然后调用:oldsysmodal = setsysmodalwindow([form].hwnd)
2.获取驱动器类型
代码如后:
declare function getdrivetype lib “kernel" (byval ndrive as integer) as integer
global const drive_removeable% = 2, drive_fixed% = 3
global const drive_remote% = 4
3.表单在对中
本子程序功能使表单定位在屏幕中央,在表单中任何需要表单对中的地方只需加入一行代码:
“centerwindow.me”即可成功调用。
public sub centerwindow(f as form)
f.top = (screen.height * .5) - (f.height * .5)
f.left = (screen.width * .5) - (f.width * .5)
end sub
4. 定义变量
许多的程序员习惯于如下定义变量:
dim inum, inextnum, ilastnum as integer
实际上只有最后一个变量被设为了整型,前两个变量则是系统的缺省的variant 数据类型,而variant 数据类型可用来替换任何数据类型,显然对于精练的程序设计是不利的。 正确的方法如下:
dim inum as integer
dim inextnum as integer
dim ilastnum as integer
5. 使文本高亮
本子程序使被触发的诸如文本,标签等控件的文本被选中
public sub setselected()
screen.activecontrol.selstart = 0
screen.activecontrol.sellength = len(screen.activecontrol.text)
end sub
6. 关闭其它程序
下面的代码可关闭内存中的其它程序
title = "myapp" '定义你需关闭的程序窗口的标题
ihwnd = findwindow(0&, title)
ihtask = getwindowtask (ihwnd)
iret = postappmessage(ihtask, wm_quit, 0, 0&)
7. 文件存在否?
本函数返回查找的文件是否存在。
function fileexist(filename as string) as boolean
fileexist = iif(dir(filename) <> "", true, false)
end function
8. 主程序唯一
用下面提供的代码作你的主程序可防止应用程序的多重执行,你应当将它放在确信需要它的代码模块内。
public sub main()
if app.previnstance then
bringwindowtotop frmmain.hwnd
else
load frmmain
end if
end sub
上面这些精悍的代码对于专业程序员来说非常有实用价值,希望你能从中获得启发。
以上代码来自: 源代码数据库(sourcedatabase)
当前版本: 1.0.392
作者: shawls
个人主页: http://shawls.yeah.net
e-mail: shawfile@163.net
qq: 9181729