Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #75
Online Version at http://www.codeoftheweek.com/membersonly/bi/0075.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Cool Software

Download a free copy of NetMon - Your Internet Performance Monitor at http://www.codeoftheweek.com/netmon/index.html

Special Deal extended for one more week

Subscribe to Code of the Week for only $14.95 (regularly $19.95). Just jump to our SECRET order page at http://www.codeoftheweek.com/orderforms/annual1495.html

In this Issue

In this issue we are introducing a class module that shows an how to retrieve some window information, such as the class name and caption.

This source code is designed for VB 4.0 32-bit and up. Questions? Email us at questions@codeoftheweek.com.

cWindowInfo

The cWindowInfo class provides an easy way to gather some important information about a window.

If you have any questions about using or creating this control, let us know at questions@codeoftheweek.com

Properties

Public Property Let Handle(lHandle As Long)
Public Property Get Handle() As Long

Every window has a unique handle that identifies it. When you set this property is causes the class to gather all the information it can about the window.

Public Caption As String

The caption of the window specified by Handle.

Public ClassName As String

The class name of the window specified by Handle. A class name is used to create all windows. Finding the class name is sometimes useful for the FindWindow call.

Public Style As Long

The style bits of the window specified by Handle.

Public IDNumber As Long

The id number of the window specified by Handle.

Public ParentHandle As Long

The handle of the parent window. You can use this handle to retrieve additional information about the parent window by creating another instance of this class and setting the Handle property.

Methods

Public Function WindowByMouse() As Boolean

Retrieves the handle that is located under the mouse pointer. It returns True is the window below the cursor is different than the last time this function was called. It returns False when the same window is below the cursor as the last call. This is useful to put into a timer function (see the sample code below).

Returns

No return values in this class except for the WindowByMouse method (see above).

Sample Usage

The below sample describes how to use the cWindowInfo class. It assumes you have a form with a Timer control called tmrMouse.

Dim WinInfo As New cWindowInfo
Dim ParentWin As New cWindowInfo

Sub tmrMouse_Timer()
    If WinInfo.WindowByMouse Then
        Cls
        Print "Window Handle: &H"; Hex(WinInfo.Handle)
        Print "Window Text: "; WinInfo.Caption
        Print "Window Class Name: "; WinInfo.ClassName
        Print "Window Style: &H"; Hex(WinInfo.Style)

        ParentWin.Handle = WinInfo.ParentHandle
        If ParentWin.Handle <> 0 Then
            Print "Parent Window Handle: &H"; Hex(WinInfo.ParentHandle)
            Print "Window ID Number: &H"; Hex(WinInfo.IDNumber)
            Print "Parent Window Text: " & ParentWin.Caption
            Print "Parent Window Class Name: "; ParentWin.ClassName
        Else
            Print "Window ID Number: N/A"
            Print "Parent Window Handle: N/A"
            Print "Parent Window Text : N/A"
            Print "Parent Window Class Name: N/A"
        End If
    End If
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0075.html


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com