光之教堂尺寸:有关vb问题

来源:百度文库 编辑:神马品牌网 时间:2024/05/01 00:15:38
怎么才能用命令关闭一个以打开的程序,文件,或者网页...那位大侠知道.教教我..小弟感激不尽....
你们的例子我都试个了,就是不行,
提示: 编译错误
在 End Sub 、End Function 或 Edn 属性后面只能出现注释

运行程序
Shell "xxx.exe"

闭一个程序

你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "The Calculator is not open."
End If

For this code to work, you must have declared the API functions in a module in your project. You must put the following in the declarations section of the module.

Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10

VB查找窗口是否运行

需要VB API函数:
FindWindow ←寻找窗口列表中第一个符合指定条件的顶级窗口
GetWindowThreadProcessId ←获取与指定窗口关联在一起的一个进程和线程标识符
--------------------------------------------------------------------------------------------------------------------------------------------------------
相关API声明:
FindWindow

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
GetWindowThreadProcessId

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
As Long
--------------------------------------------------------------------------------------------------------------------------------------------------------
需要的控件:Label、Timer
-------------------------------------------------------------------------------------------------------------------------------------------------------- 自定义函数:
Dim hwnd As Long
-------------------------------------------------------------------------------------------------------------------------------------------------------- 源代码:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)As Long
Private Sub Timer1_Timer()
Dim hwnd As Long'' 储存 FindWindow 函数返回的句柄
hwnd = FindWindow(vbNullString, "Windows Media Player")'' 取得进程标识符
''只要把Windows Media Player换成游戏的名称就可了!
If hwnd = 0 Then
Label1.Caption = "游戏未运行"
Else
Label1.Caption = "游戏已运行"
End If
End Sub

编译错误:
“在 End Sub 、End Function 或 Edn 属性后面只能出现注释”就是说有不能识别的代码,你在这些红字前加个'就可以了
至于这些API也是我在找的,谢谢楼上的兄弟们