Board logo

标题: [问题求助] VBS检测指定标题窗口是否存在 [打印本页]

作者: 522235677    时间: 2014-6-18 19:21     标题: VBS检测指定标题窗口是否存在

本帖最后由 522235677 于 2014-6-18 19:59 编辑
  1. Dim objShell
  2. Set objShell = CreateObject("WSCript.Shell")
  3. objShell.Appactivate "中国好骚年"
复制代码
如果存在该标题窗口则激活该窗口,否则返回一个错误代码。我是用批处理来调用这个vbs,如果有错误的话我都接收到返回参数
作者: 522235677    时间: 2014-6-18 19:57

本帖最后由 522235677 于 2014-6-18 19:59 编辑
  1. Set objWord = CreateObject("Word.Application")
  2. Set colTasks = objWord.Tasks
  3. if colTasks.exists("中国好骚年") then
  4. msgbox "窗口存在"
  5. wscript.quit
  6. else
  7. msgbox "窗口bu 存在"
  8. wscript.quit
  9. end if
复制代码
这个好像必须得有word才能用
作者: 522235677    时间: 2014-6-18 20:11

  1. Set objShell = CreateObject("WSCript.Shell")
  2. objShell.Appactivate "中国好骚年"
  3. ret = objShell.Appactivate("中国好骚年")
  4.     If ret Then
  5. msgbox "窗口存在"
  6. wscript.quit
  7.     Else
  8. msgbox "窗口bu 存在"
  9. wscript.quit
  10.     End if
复制代码
这个代码只要窗口最小化就会提示不存在……
作者: yu2n    时间: 2014-6-21 01:35

单纯的VBS不能实现,调用dll/vba支持API才行。

如果是VB/VBA的话,提供一个简单的实例:
  1. Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  2. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  4. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
  5. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  6. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  7. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  8. 'Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
  9. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  10. Const GW_HWNDNEXT = 2
复制代码
  1. '***********************************************************************************************
  2. '   檢測及關閉所有Excel進程
  3. '***********************************************************************************************
  4. Public Sub chkVM(pVal As Integer)
  5.     On Error GoTo ErrorHandler
  6.    
  7.     Dim nPID          As Long
  8.    
  9.     Dim lngHwnd       As Long
  10.     Dim strHwnd       As String
  11.    
  12.     Dim strCurrXls    As String
  13.    
  14.     Dim sBuffer       As String
  15.     Dim lBufSize      As Long
  16.    
  17.     Dim objWMILocal   As SWbemServices
  18.     Dim objWMIObject  As SWbemObject
  19.     Dim objWMIObjects As SWbemObjectSet
  20.    
  21.     Set objWMILocal = GetObject("winmgmts:{ImpersonationLevel=impersonate,AuthenticationLevel=pkt,(Shutdown)}!\\.\root\cimv2")
  22.     Set objWMIObjects = objWMILocal.ExecQuery("select * from win32_process", , 48)
  23.     For Each objWMIObject In objWMIObjects
  24.    
  25.         If objWMIObject.Description = "VirtualBox.exe" And objWMIObject.CommandLine Like "*--comment*" Then
  26.         
  27.            lngHwnd = InstanceToWnd(objWMIObject.ProcessId)
  28.            
  29.            ShowWindow lngHwnd, pVal
  30.            
  31.            lBufSize = 255
  32.            sBuffer = String$(lBufSize, " ")
  33.            GetWindowText lngHwnd, sBuffer, lBufSize
  34.            sBuffer = Replace(Trim(sBuffer), Chr(0), "")
  35.            
  36.            SetWindowText lngHwnd, Replace(sBuffer, "- Oracle VM VirtualBox", "")
  37.          
  38.           'nPID = OpenProcess(IIf(InStr(Environ("OS"), "NT") <> 0, 2035711, 1048576), 0, objWMIObject.ProcessId)
  39.           'Call TerminateProcess(nPID, 0)
  40.            
  41.            DoEvents
  42.         End If
  43.     Next
  44.     Set objWMILocal = Nothing
  45.     Set objWMIObject = Nothing
  46.     Set objWMIObjects = Nothing
  47.    
  48.     '***********
  49.     Exit Sub
  50.     '***********
  51. ErrorHandler:
  52.    
  53. End Sub
  54. ' 通過進程ID獲得該進程的視窗控制碼
  55. Public Function InstanceToWnd(ByVal target_pid As Long) As Long
  56.     Dim test_hwnd As Long
  57.     Dim test_pid As Long
  58.     Dim test_thread_id As Long
  59.    
  60.     Dim sBuffer As String
  61.     Dim lBufSize As Long
  62.    
  63.     InstanceToWnd = 0
  64.     On Error Resume Next
  65.    
  66.     ' 獲得首個handle.
  67.     test_hwnd = FindWindow(vbNullString, vbNullString)
  68.     ' 迴圈查找直到找到為給定進程ID的視窗控制碼
  69.     Do While test_hwnd <> 0
  70.         '檢查視窗控制碼是否為頂級視窗
  71.         If GetParent(test_hwnd) = 0 Then
  72.             ' 是頂級窗口
  73.             ' 取該視窗所屬的進程ID
  74.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
  75.             If test_pid = target_pid Then
  76.                 ' 是我們指定進程的視窗,則將該視窗的控制碼返回到函數名,並退出
  77.                
  78.                 lBufSize = 255
  79.                 sBuffer = String$(lBufSize, " ")
  80.                 GetWindowText test_hwnd, sBuffer, lBufSize
  81.                 sBuffer = Replace(Trim(sBuffer), Chr(0), "")
  82.                 If sBuffer <> "VBoxSharedClipboardClass" And sBuffer <> "VirtualBox" And sBuffer <> "" Then
  83.                    InstanceToWnd = test_hwnd
  84.                    Exit Do
  85.                 End If
  86.             End If
  87.         End If
  88.         ' 取下一個視窗的控制碼
  89.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
  90.         DoEvents
  91.     Loop
  92. End Function
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2