步骤是 :
'Module Module1 //模块中声明findwindow
Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'End Module
'Public Class Form1 //以下是主程序
Private Sub Button1_Click(ByVal sender As System.object, ByVal e As System.EventArgs) 'Handles Button1.Click
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "Calculator")
If hwnd = 0 Then
Label1.Text = "程序未运行"
Exit Sub
End If
If hwnd <> 0 Then
Label1.Text = "程序已运行"
End If
End Sub
'End Class
不知道为什么,明明电脑中没有运行calculator(计算器)这个程序,但都提示"程序已运行"....打开可计算器也是提示"程序已运行"....不知道哪错了呢
因为VB.net和VB6的数据类型LONG的定义已经不一样了,在VB.NET里long是64位,而窗口句柄仍然是32位, 所以得不到正确的返回值,你只要将long改成integer就好了,两处:
Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
摘自:网络整理
★VB部分相关文章推荐:★
☆关于VB中Shell及ShellExecute的总结与记录
☆[vb]On Error GoTo 0和On Error resume区别
☆VB抓图