Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Const WM_CLOSE = &H10 Private Const xxx As String = "我的电脑"
Private Sub Form_Load() Timer1.Interval = 2000 End Sub
Private Sub Timer1_Timer() Dim hWindow As Long hWindow = FindWindow(vbNullString, xxx) If hWindow Then PostMessage hWindow, WM_CLOSE, 0&, 0& End Sub
程序运行后,打开我的电脑,我的电脑会被自动关闭,你把“我的电脑”换成你想关闭的窗口标题就行了
vb.net:For Each pro As Process in Process.GetProcesses() if pro.Name <> "自己窗体" and pro.Name <> "System" and pro.Name <> "Explorer.exe" then pro.kill() Next
定时检查可以用timer控件实现
窗口可以用FindWindows这个API枚举
对于每一个找到的窗口, 判断它为无关时就关闭它
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_CLOSE = &H10
Private Const xxx As String = "我的电脑"
Private Sub Form_Load()
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Timer()
Dim hWindow As Long
hWindow = FindWindow(vbNullString, xxx)
If hWindow Then PostMessage hWindow, WM_CLOSE, 0&, 0&
End Sub
程序运行后,打开我的电脑,我的电脑会被自动关闭,你把“我的电脑”换成你想关闭的窗口标题就行了
if pro.Name <> "自己窗体" and pro.Name <> "System" and pro.Name <> "Explorer.exe" then pro.kill()
Next