Fenster Hook (Shellhook)

    • VB.NET

      Fenster Hook (Shellhook)

      Hallo,
      habe nach längerer Suche ein Snippet gefunden, mit dem man herausfindet, wenn ein Fenster geöffnet wird, aktiviert wird etc pp.

      VB.NET-Quellcode

      1. 'Credits: TreyJ
      2. Imports System.Runtime.InteropServices
      3. Public Class Form1
      4. ' Shell Events Constants
      5. Public Enum ShellEvents
      6. HSHELL_WINDOWCREATED = 1
      7. HSHELL_WINDOWDESTROYED = 2
      8. HSHELL_ACTIVATESHELLWINDOW = 3
      9. HSHELL_WINDOWACTIVATED = 4
      10. HSHELL_GETMINRECT = 5
      11. HSHELL_REDRAW = 6
      12. HSHELL_TASKMAN = 7
      13. HSHELL_LANGUAGE = 8
      14. HSHELL_ACCESSIBILITYSTATE = 11
      15. End Enum
      16. ' API Declares
      17. Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
      18. Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
      19. Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
      20. Private uMsgNotify As Integer
      21. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
      22. Me.Text = Me.Handle
      23. ' This will register the ShellHook event messages between the shell and our application.
      24. ' The uMsgNotify is then used to communicate between the shell and our application whenever any
      25. ' of the shell events are fired such as an app starting up, shutting down, activating, minimizing, etc
      26. uMsgNotify = RegisterWindowMessage("SHELLHOOK")
      27. ' This basically registers our window to receive the shell events
      28. Call RegisterShellHookWindow(Me.Handle)
      29. End Sub
      30. Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      31. DeregisterShellHookWindow(Me.Handle)
      32. End Sub
      33. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
      34. If m.Msg = uMsgNotify Then
      35. Select Case m.WParam.ToInt32
      36. Case ShellEvents.HSHELL_WINDOWACTIVATED 'Remove space after dot
      37. Debug.WriteLine("window activated: " & m.LParam.ToInt32)
      38. Case ShellEvents.HSHELL_WINDOWCREATED
      39. Debug.WriteLine("window created: " & m.LParam.ToInt32)
      40. '...
      41. End Select
      42. End If
      43. MyBase.WndProc(m)
      44. End Sub
      45. End Class
      Für ein Mindestmaß an Rechtschreibung, Interpunktion und Majuskeln!