关于findwindow()的返回值

来源:百度知道 编辑:UC知道 时间:2024/05/16 05:53:11
模块:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
窗体form:
Dim a As Long
Private Sub Form_Load()
a = FindWindow("CabinetWClass", "我的电脑")
If a = 1 Then
Text1.Text = "找到"
ElseIf a = 0 Then
Text1.Text = "未找到"
Else
MsgBox "error"
End If
End Sub

打开我的电脑后,为什么运行程序后是弹出"error",而不是a=1?

FindWindow函数执行成功后,返回值是拥有指定窗口类名或窗口名的窗口的句柄(类型是Long)。也就是说,你每次重新打开一次“我的电脑”,再FindWindow,返回的值其实都不一样,不过它们都不为零就是了,所以判定有没有找到这个窗口就直接用a>0来判定就可以了。

Private Sub Form_Load()
a = FindWindow("CabinetWClass", "我的电脑")
If a > 0 Then
Text1.Text = "找到"
Else
Text1.Text = "未找到"
End If
End Sub

因为找到了句柄,a<>0且a>1,所以自然就跳到Else ...MsgBox "error"了