VB.net:杀死窗口标题更改的事件

时间:2013-06-26 12:23:06

标签: vb.net events window kill titlebar

我想阻止软件在特定时间运行。我使用此代码关闭可执行文件。但有些软件使用“闪屏”(例如Photoshop)这个屏幕的标题似乎没什么。因此,这种解决方案适用于直接设置程序标题的软件,但对于像Adobe Photoshop这样的软件,启动可能需要一段时间,这根本不起作用。

VB.net代码:

Public Sub CreationEventWatcherPolling()
    Dim procWatcher As New Threading.Thread(AddressOf CreationEventWatcherPolling)
    procWatcher.Start()

    ' Create event query to be notified within 1 second of a change in a service
    Dim query As New WqlEventQuery("__InstanceCreationEvent", New TimeSpan(0, 0, 1), "TargetInstance isa ""Win32_Process""")

    ' Initialize an event watcher and subscribe to events that match this query
    Dim watcher As New ManagementEventWatcher(query)

    Do
        Dim e As ManagementBaseObject = watcher.WaitForNextEvent()
        'block Notepad
        If (Process.GetProcessById(CInt(CType(e("TargetInstance"), ManagementBaseObject)("Handle"))).MainWindowTitle.Contains("Notepad")) Then
            Process.GetProcessById(CInt(CType(e("TargetInstance"), ManagementBaseObject)("Handle"))).Kill()
        End If
    Loop

    'Cancel the subscription
    watcher.Stop()
End Sub

我还希望能够检测到应用程序“标题”的更改。这也将解决上述问题。例如,如果我检测到Google Chrome正在运行,并且标题更改为“Facebook”,因为我打开了一个新标签页,我希望能够检测到它。下面的代码我发现网上冲浪检测到对象名称的变化,这很有用。它确实检测到没有标题的启动画面何时更改为“Photoshop CS5”标题,并且它确实检测到在浏览器中打开新标签页,因为这也会导致应用程序的标题更改。但它也会检测其他对象更改,例如窗口调整大小和其他内容。所以,我基本上想要的是将上面的代码和下面的代码结合起来,并仅检测应用程序表单的名称更改。

namechange VB.net代码:

  

Public Sub HookEvents()       如果eHook<> 0然后退出Sub       mEventFunc = New EventFunc(AddressOf WinEventFunc)       eHook = apiSetWinEventHook(OB_NAMECHANGE,OB_NAMECHANGE,0,mEventFunc,0,0,WINEVENT_SKIPOWNPROCESS)End Sub

     

Public Sub UnHookEvents()       如果eHook<> 0然后apiUnhookWinEvent(eHook)End Sub

     

私有函数WinEventFunc(ByVal eHandle As Int32,ByVal lEvent As   Int32,ByVal hWnd As Int32,ByVal objId As Int32,ByVal childId As   Int32,ByVal eThreadId作为Int32,ByVal eTime作为Int32)作为Int32       Dim acc As Global.Accessibility.IAccessible = Nothing       Dim cId As Object = Nothing       Dim sName As String =“”

apiAccessibleObjectFromEvent(hWnd, objId, childId, acc, cId)
If Not acc Is Nothing Then
    If Not cId Is Nothing Then
        Try
            sName = acc.accName(cId)
            Me.Text = sName
            If (sName.Contains("Facebook")) Then Me.Text = "NO FACEBOOK ALLOWED"

        Catch ex As System.Runtime.InteropServices.COMException When ex.ErrorCode = -2146827864  'weird 
        Catch ex As System.Runtime.InteropServices.COMException When ex.ErrorCode = -2147467259  'null
        Catch ex As System.Runtime.InteropServices.COMException When ex.ErrorCode = -2147417848  'rpc disconnected
        Catch ex As ArgumentException 'null
        Catch ex As NullReferenceException
        End Try
    End If
End If
WinEventFunc = 0 End Function

0 个答案:

没有答案