SPY++ style application in VB.NET or C#

Pber
Pber used Ask the Experts™
on
I building on the sample given to me by Idle_Mind in the related question.  I'm trying to only get it to highlight and display main forms and dialogs and not the controls within a dialog.
i.e.  If I were to spy on wordpad.  I only want to see the main frame, but not the toolbar, ruler or the richtext window.  

Is this possible, if so how?

I presume I would just do a check in the picturebox1_Movemove event and do a check in the following routine:
If Not handle.Equals(prevHandle) Then
                ' get new rectangle
                GetWindowRect(handle, prevRC)
 
                ' draw new rectangle
                ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
            End If

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Given the handle of the control you are currently over, you can get the main window by using the GetAncestor() API with the GA_ROOT flag:

    Public Const GA_ROOT As Integer = 2
    Public Declare Function GetAncestor Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal gaFlags As Integer) As IntPtr

Example:

    Dim MainWindow As IntPtr = GetAncestor(handle, GA_ROOT)

Now you can use GetWindowRect() with "MainWindow" just like in the previous code....
PberSolutions Architect

Author

Commented:
Thanks.  This is what I got?

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim pt As New PointAPI(Cursor.Position.X, Cursor.Position.Y)
            Dim handle As IntPtr = WindowFromPoint(pt)
            Dim MainWindow As IntPtr = GetAncestor(handle, GA_ROOT)
 
 
            Label1.Text = MainWindow.ToString("X")
 
            If Not MainWindow.Equals(prevHandle) AndAlso Not prevHandle.Equals(IntPtr.Zero) Then
                ' erase previous rectanlge
                ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
            End If
 
            If Not MainWindow.Equals(prevHandle) Then
                ' get new rectangle
                GetWindowRect(MainWindow, prevRC)
 
                ' draw new rectangle
                ControlPaint.DrawReversibleFrame(New Rectangle(prevRC.Left, prevRC.Top, prevRC.Right - prevRC.Left, prevRC.Bottom - prevRC.Top), Color.Black, FrameStyle.Thick)
            End If
 
            ' store new handle
            prevHandle = MainWindow
        End If
    End Sub

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Looks good...does it work?   =)
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

PberSolutions Architect

Author

Commented:
Yeah, it works awesome.  I also added a check to ignore itself:

If MainWindow <> Me.Handle Then
end if
 
Thanks again.
PberSolutions Architect

Author

Commented:
Thanks again.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
BTW...I like the way you did your profile.   ;)
PberSolutions Architect

Author

Commented:
Thanks.  (:
Just noticed a bug.  The Blog enum doesn't compile.  Fixed it with a constant.
(:
 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial