detect a form name opened by another application.

swu

Active member
Joined
Mar 26, 2005
Messages
33
Programming Experience
1-3
Does any one have a sample or a starting point for vb.net code that can detect when a form is opened by another app and read the name on the form?

I want to create an app that runs in the sys tray and detects when another program opens a particular dialog box/form.

Thanks in advance.
 
The code from this Last activated application post works. It RegisterWindowMessage("SHELLHOOK") and RegisterShellHookWindow, this enables the application to get messages through WndProc where you can detect HSHELL_WINDOWACTIVATED. With GetWindowTextLength and GetWindowText API calls you can get the window caption from the window handle GetForegroundWindow returned.
 
Thought I could post a sample here too, I've posted that link several times here already.
VB.NET:
Private Sub frmDiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    uMsgNotify = RegisterWindowMessage("SHELLHOOK")
    RegisterShellHookWindow(Me.Handle)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    DeregisterShellHookWindow(Me.Handle)
End Sub

' Shell Events Constants
Public Enum ShellEvents
    HSHELL_WINDOWCREATED = 1
    HSHELL_WINDOWDESTROYED = 2
    HSHELL_ACTIVATESHELLWINDOW = 3
    HSHELL_WINDOWACTIVATED = 4
    HSHELL_GETMINRECT = 5
    HSHELL_REDRAW = 6
    HSHELL_TASKMAN = 7
    HSHELL_LANGUAGE = 8
    HSHELL_ACCESSIBILITYSTATE = 11
End Enum

' API Declares
Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer
Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
Public Declare Function GetForegroundWindow Lib "user32" () As IntPtr
Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32
Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32

Private uMsgNotify As Integer

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = uMsgNotify Then
        Select Case m.WParam.ToInt32
            Case ShellEvents.HSHELL_WINDOWACTIVATED
                Dim curWindow As IntPtr = GetForegroundWindow()
                Dim s As New String(" "c, GetWindowTextLength(curWindow))
                GetWindowText(curWindow, s, s.Length + 1)
                'string variable 's' should now hold the window caption
        End Select
    End If
    MyBase.WndProc(m)
End Sub
 
Back
Top