这个小例子就是用来演示如何得到windows桌面上处于活动状态的窗口的句柄的。
使用一个timer控件就可以搞定。在本例中再通过getwindowtext函数来处理得到句柄后的操作。
1。新建一个标准vb6的exe工程,加入timer控件
2。api函数的声明
private declare function getforegroundwindow lib "user32" () as long
private declare function getwindowtext lib "user32" _
alias "getwindowtexta" (byval hwnd as long, _
byval lpstring as string, byval cch as long) as long
3。在窗体的load事件中加入代码:
private sub form_load()
timer1.interval = 100 '设置间隔时间
end sub
4。在timer控件中的timer事件中加入代码:
private sub timer1_timer()
static currenthwnd as long
dim foregroundwindowhwnd as long
dim stext as string * 255
foregroundwindowhwnd = getforegroundwindow
if foregroundwindowhwnd = currenthwnd then exit sub
currenthwnd = foregroundwindowhwnd
if currenthwnd <> hwnd then
caption = "activewidow's caption: " & left$(stext, getwindowtext(currenthwnd, stext, 255))
else
caption = "activewindow's caption: form1"
end if
end sub
本程序在win2000 + vb6 中调试通过