Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing frmProcessSpy.vb (18.86 KB)

Option Explicit On

Imports javaSpy.clsWindowObject
Imports System.Diagnostics

Public Class frmProcessSpy

    Private _thisWindow As clsWindowObject = Nothing
    Private _thisProcess As Process = Nothing
    Private _thisThread As ProcessThread = Nothing
    Private _intProcessCount As Integer = 0
    Private _intThreadCount As Integer = 0
    Private _intWindowCount As Integer = 0

    Public Sub New(Optional ByVal objWindow As clsWindowObject = Nothing, _
        Optional ByVal objProcess As Process = Nothing, _
        Optional ByVal objThread As ProcessThread = Nothing)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _thisWindow = objWindow
        _thisProcess = objProcess
        _thisThread = objThread

    End Sub

    Public Function windowEnumFunc(ByVal thisWindow As clsWindowObject) As Integer

        Dim thisNode As TreeNode = _findNodeByWindow(thisWindow)

        If Not (thisNode Is Nothing) Then

            Dim newNode As TreeNode = thisNode.Nodes.Add( _
                thisWindow.hWnd.ToString, _displayText(thisWindow))

            newNode.ContextMenuStrip = menuWindowPopup
            newNode.Tag = thisWindow

            If thisWindow.Visible Then
                newNode.StateImageIndex = 0
            Else
                newNode.StateImageIndex = 1
            End If

            If Not (_thisWindow Is Nothing) AndAlso _
                _thisWindow.hWnd = thisWindow.hWnd Then

                newNode.EnsureVisible()
                tvSpy.SelectedNode = newNode

            End If

            _intWindowCount += 1

            clsWindowObject.BeginChildWindowEnum(thisWindow.hWnd, AddressOf windowEnumChildFunc)

        End If

        Return 1

    End Function

    Public Function windowEnumChildFunc(ByVal thisWindow As clsWindowObject) As Integer

        Dim thisNode As TreeNode = _findNodeByWindow(thisWindow)

        If Not (thisNode Is Nothing) Then

            thisNode = thisNode.Nodes(thisWindow.hWndParent.ToString)

            If Not (thisNode Is Nothing) Then

                Dim newNode As TreeNode = thisNode.Nodes.Add( _
                    thisWindow.hWnd.ToString, _displayText(thisWindow))

                newNode.ContextMenuStrip = menuWindowPopup
                newNode.Tag = thisWindow

                If thisWindow.Visible Then
                    newNode.StateImageIndex = 0
                Else
                    newNode.StateImageIndex = 1
                End If

                If Not (_thisWindow Is Nothing) AndAlso _
                    _thisWindow.hWnd = thisWindow.hWnd Then

                    newNode.EnsureVisible()
                    tvSpy.SelectedNode = newNode

                End If

                _intWindowCount += 1

                windowFindWindowEnum(thisWindow, newNode)

            End If

        End If

        Return 1

    End Function

    Public Sub windowFindWindowEnum(ByVal parentWindow As clsWindowObject, _
        ByVal parentNode As TreeNode)

        Dim childWindow As clsWindowObject = parentWindow.GetFirstChild()

        Do While childWindow.IsWindow

            Dim newNode As TreeNode = parentNode.Nodes.Add( _
                childWindow.hWnd.ToString, _displayText(childWindow))

            newNode.ContextMenuStrip = menuWindowPopup
            newNode.Tag = childWindow

            If childWindow.Visible Then
                newNode.StateImageIndex = 0
            Else
                newNode.StateImageIndex = 1
            End If

            If Not (_thisWindow Is Nothing) AndAlso _
                _thisWindow.hWnd = childWindow.hWnd Then

                newNode.EnsureVisible()
                tvSpy.SelectedNode = newNode

            End If

            _intWindowCount += 1

            If childWindow.HasChildren Then

                ' recursive enum children
                windowFindWindowEnum(childWindow, newNode)

            End If

            childWindow = childWindow.GetNextSibling()

        Loop

    End Sub

    Private Function _displayText(ByVal thisWindow As clsWindowObject) As String

        If Not (thisWindow Is Nothing) AndAlso _
            thisWindow.IsWindow Then

            Return "Window " + FormatHandle(thisWindow.hWnd) + _
                " """ + thisWindow.Text + """ " + _
                thisWindow.ClassName

        Else

            Return "[invalid window]"

        End If

    End Function

    Private Function _findNodeByWindow(ByVal thisWindow As clsWindowObject) As TreeNode

        Dim thisNode As TreeNode = tvSpy.Nodes(thisWindow.ProcessId.ToString)

        If Not (thisNode Is Nothing) Then _
            thisNode = thisNode.Nodes(thisWindow.ThreadId.ToString)

        Return thisNode

    End Function

    Private Function _findNodeText(ByVal nodeText As String, _
        Optional ByVal nodeParent As TreeNode = Nothing) As TreeNode

        Dim nodeRet As TreeNode = Nothing
        Dim nodeList As TreeNodeCollection = Nothing

        If nodeParent Is Nothing Then
            nodeList = tvSpy.Nodes
        Else
            nodeList = nodeParent.Nodes
        End If

        Dim bIgnoreNodes As Boolean = nodeList.Contains(tvSpy.SelectedNode)

        For Each thisNode As TreeNode In nodeList

            If Not bIgnoreNodes Then

                If thisNode.Text.Contains(nodeText) Then

                    nodeRet = thisNode
                    Exit For

                End If

                If thisNode.GetNodeCount(False) > 0 Then

                    nodeRet = _findNodeText(nodeText, thisNode)
                    If Not (nodeRet Is Nothing) Then Exit For

                End If

            End If

            If bIgnoreNodes AndAlso _
                thisNode.Equals(tvSpy.SelectedNode) Then _
                bIgnoreNodes = False

        Next ' thisNode

        Return nodeRet

    End Function

    Private Sub _showWaitCursor(ByVal bShow As Boolean)

        ' show/hide the hourglass
        If bShow Then
            Me.Cursor = Cursors.WaitCursor
            Me.UseWaitCursor = True
        Else
            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Arrow
        End If

    End Sub

    Private Sub frmProcessSpy_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        For Each thisProcess As Process In Process.GetProcesses()

            Dim newNode As TreeNode = tvSpy.Nodes.Add( _
                    thisProcess.Id.ToString, _
                    "Process " + FormatHandle(thisProcess.Id) + _
                    " " + thisProcess.ProcessName)

            newNode.ContextMenuStrip = menuProcessPopup
            newNode.Tag = thisProcess
            newNode.StateImageIndex = 2

            If (Not (_thisProcess Is Nothing) And _
                _thisThread Is Nothing) AndAlso _
                _thisProcess.Id = thisProcess.Id Then

                newNode.EnsureVisible()
                tvSpy.SelectedNode = newNode

            End If

            _intProcessCount += 1

            For Each thisThread As ProcessThread In thisProcess.Threads

                Dim childNode As TreeNode = newNode.Nodes.Add(thisThread.Id.ToString, _
                    "Thread " + FormatHandle(thisThread.Id) + _
                    " " + thisProcess.ProcessName)

                childNode.ContextMenuStrip = menuThreadPopup
                childNode.Tag = thisProcess
                childNode.StateImageIndex = 3

                If (Not (_thisProcess Is Nothing) And _
                    Not (_thisThread Is Nothing)) AndAlso _
                    (_thisProcess.Id = thisProcess.Id And _
                    _thisThread.Id = thisThread.Id) Then

                    childNode.EnsureVisible()
                    tvSpy.SelectedNode = childNode

                End If

                _intThreadCount += 1

            Next ' thisThread

        Next ' thisProcess

        clsWindowObject.BeginWindowEnum(AddressOf windowEnumFunc)

    End Sub

    Private Sub frmProcessSpy_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        lblStatus.Text = _intProcessCount.ToString("###,##0") + " processes; " + _
            _intThreadCount.ToString("###,##0") + " threads; " + _
            _intWindowCount.ToString("###,##0") + " windows"

    End Sub

    Private Sub frmProcessSpy_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        ssMain.ShowItemToolTips = Not (Me.WindowState = FormWindowState.Maximized)

    End Sub

    Private Sub tvSpy_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvSpy.DoubleClick

        Dim objHitTest As TreeViewHitTestInfo = tvSpy.HitTest( _
            tvSpy.PointToClient(Control.MousePosition))

        If tvSpy.SelectedNode.Equals(objHitTest.Node) AndAlso _
            (tvSpy.SelectedNode.GetNodeCount(False) = 0 And _
            Not (tvSpy.SelectedNode.Tag Is Nothing)) Then

            Select Case tvSpy.SelectedNode.StateImageIndex

                Case 0, 1 : mnuProperties.PerformClick() ' window
                Case 2 : mnuProcessProps.PerformClick() ' process
                Case 3 : mnuThreadProps.PerformClick() ' thread

            End Select

        End If

    End Sub

    Private Sub tvSpy_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tvSpy.KeyUp

        If e.KeyCode = Keys.F3 Then lblFind_Click(sender, Nothing)

    End Sub

    Private Sub tvSpy_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvSpy.NodeMouseClick

        If (e.Button = Windows.Forms.MouseButtons.Left And _
            SystemInformation.MouseButtonsSwapped) Or _
            (e.Button = Windows.Forms.MouseButtons.Right And _
            Not SystemInformation.MouseButtonsSwapped) Then

            tvSpy.SelectedNode = e.Node

        End If

    End Sub

    Private Sub mnuUnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUnload.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.Close()

    End Sub

    Private Sub mnuClick0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick0.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_DEFAULT)

    End Sub

    Private Sub mnuClick1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick1.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_LBTN_UP)

    End Sub

    Private Sub mnuClick2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick2.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_LBTN_DOWN)

    End Sub

    Private Sub mnuClick3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick3.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_LBTN_DBLCLICK)

    End Sub

    Private Sub mnuClick4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick4.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_RBTN_UP)

    End Sub

    Private Sub mnuClick5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick5.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_RBTN_DOWN)

    End Sub

    Private Sub mnuClick6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClick6.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.ClickButton(ClickButtonMethods.BTN_RBTN_DBLCLICK)

    End Sub

    Private Sub mnuShowWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowWindow.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_SHOW

    End Sub

    Private Sub mnuHideWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHideWindow.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_HIDE

    End Sub

    Private Sub mnuRestoreWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRestoreWindow.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_RESTORE

    End Sub

    Private Sub mnuEnableWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnableWindow.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.Enabled = True

    End Sub

    Private Sub mnuDisableWindow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDisableWindow.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.Enabled = False

    End Sub

    Private Sub mnuNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNormal.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_NORMAL

    End Sub

    Private Sub mnuMinimized_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMinimized.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_MINIMIZE

    End Sub

    Private Sub mnuMaximized_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMaximized.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.WindowState = WindowStates.SW_MAXIMIZE

    End Sub

    Private Sub mnuTopTrue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuTopTrue.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.AlwaysOnTop = True

    End Sub

    Private Sub mnuTopFalse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuTopFalse.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Tag.AlwaysOnTop = False

    End Sub

    Private Sub mnuMenuSpy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMenuSpy.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        _showWaitCursor(True)

        With New frmMenuSpy(tvSpy.SelectedNode.Tag)
            .Show(Me)
        End With

        _showWaitCursor(False)

    End Sub

    Private Sub mnuProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProperties.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        _showWaitCursor(True)

        With New frmWindowProps(tvSpy.SelectedNode.Tag)
            .Show(Me)
        End With

        _showWaitCursor(False)

    End Sub

    Private Sub menuWindowPopup_Opened(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuWindowPopup.Opened

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        If TypeOf tvSpy.SelectedNode.Tag Is clsWindowObject Then _
            mnuMenuSpy.Enabled = tvSpy.SelectedNode.Tag.HasMenu

    End Sub

    Private Sub mnuProcessProps_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProcessProps.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        _showWaitCursor(True)

        With New frmProcessProps(CType(tvSpy.SelectedNode.Tag, Process))
            .Show(Me)
        End With

        _showWaitCursor(False)

    End Sub

    Private Sub mnuTerminate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuTerminate.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        If MessageBox.Show("Are you sure you want to terminate this process?", _
            Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

            Try

                tvSpy.SelectedNode.Tag.Kill()

            Catch ex As Exception

                MessageBox.Show("Failure!" + vbCrLf + vbCrLf + "Reason: " + ex.Message, _
                    Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        End If

    End Sub

    Private Sub mnuThreadProps_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuThreadProps.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        _showWaitCursor(True)

        With New frmThreadProps(CType(tvSpy.SelectedNode.Tag, Process), CInt(tvSpy.SelectedNode.Name))
            .Show(Me)
        End With

        _showWaitCursor(False)

    End Sub

    Private Sub mnuReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReload.Click

        If tvSpy.SelectedNode.Tag Is Nothing Then Exit Sub

        tvSpy.SelectedNode.Text = _displayText(tvSpy.SelectedNode.Tag)
        tvSpy.SelectedNode.Nodes.Clear()

        windowFindWindowEnum(tvSpy.SelectedNode.Tag, tvSpy.SelectedNode)

    End Sub

    Private Sub lblFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFind.Click

        Static lastFindText As String = ""
        Dim findText As String = ""

        If e Is Nothing And lastFindText <> "" Then
            findText = lastFindText
        Else
            findText = InputBox("Enter the text to find:", Application.ProductName, lastFindText)
        End If

        If findText <> "" Then

            Dim startNode As TreeNode = tvSpy.SelectedNode.Parent

            _showWaitCursor(True)
            Dim findNode As TreeNode = _findNodeText(findText, startNode)
            _showWaitCursor(False)

            If Not (findNode Is Nothing) Then

                findNode.EnsureVisible()
                tvSpy.SelectedNode = findNode

            Else

                MessageBox.Show("Cannot find """ + findText + """", Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)

                findText = ""

            End If

        End If

        lastFindText = findText

    End Sub

End Class

Download frmProcessSpy.vb

Back to file list


Back to project page