Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: How to get hWnd of a textbox from another application


Message #1 by "Ozanhan Anaç" <oanac@t...> on Fri, 15 Nov 2002 11:34:52
SWEET!

Thanks, Ian!

Pete

-----Original Message-----
From: Ian Bambury - JASWW [mailto:email.address@v...]
Sent: Friday, November 15, 2002 8:07 AM
To: professional vb
Subject: [pro_vb] Re: How to get hWnd of a textbox from another
application


Hi,

This code will get you all the windows on your machine.

Put a timer, three checkboxes, a command button, a list box and a label
(accept default names) on a form and add the following code.

I don't think it will word-wrap

Good luck,

Ian

=========================================================
Option Explicit

    Private Declare Function GetDesktopWindow _
        Lib "User32" _
            ( _
            ) _
        As Long

   Private Declare Function GetWindow _
        Lib "User32" _
        ( _
            ByVal hWnd As Long _
            , ByVal wCmd As Long _
        ) _
        As Long

    Private Declare Function GetWindowText _
        Lib "User32" _
        Alias "GetWindowTextA" _
        ( _
            ByVal hWnd As Long _
            , ByVal lpString As String _
            , ByVal cch As Long _
        ) _
        As Long

    Private Declare Function GetClassName _
        Lib "User32" _
        Alias "GetClassNameA" _
        ( _
            ByVal hWnd As Long _
            , ByVal lpString As String _
            , ByVal cch As Long _
        ) _
        As Long
    Private Declare Function GetParent _
        Lib "User32" _
        ( _
            ByVal hWnd As Long _
        ) _
        As Long

    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2
    Const GW_CHILD = 5

    Dim nParentWnd As Long
    Dim nTextLen  As Long
    Dim sText As String
    Dim nClassLen  As Long
    Dim sClass As String
    Dim nLevel As Integer
    Dim nDisplayCount As Integer

Private Sub Check1_Click()
    Command1_Click
End Sub

Private Sub Check2_Click()
    Command1_Click
End Sub

Private Sub Check3_Click()
    Timer1.Enabled = Check3.Value
End Sub

Private Sub Command1_Click()
    List1.Visible = False

    List1.Clear

    List1.AddItem _
        "Owner   TaskID  Level   Class" _
        & Space(36) _
        & "Text" _
        & Space(10) _
        & "(Last Updated: " _
        & Format _
            ( _
            Now _
            , "hh:nn:ss" _
            ) _
        & ")"
    List1.AddItem _
        String _
            ( _
            120 _
            , "~" _
            )
    Dim hWnd As Long
    hWnd = GetDesktopWindow
    nLevel = -1
    nDisplayCount = 0
    GetChildren hWnd
    Label1 = _
        nDisplayCount _
        & " items"
    List1.Visible = True
End Sub

Private Sub GetChildren(nParentWnd)
    Dim hWnd
    Dim hOwnerWnd
    nLevel = nLevel + 1
'------
    Do While nParentWnd <> 0
'--
        sText = Space(255)
'--
        nTextLen = _
            GetWindowText _
            ( _
            nParentWnd _
            , sText _
            , 255 _
            )
'--
        If nTextLen > 0 Then sText = _
            Left(sText, nTextLen)

        sClass = Space(255)
'--
        nClassLen = _
            GetClassName _
            ( _
            nParentWnd _
            , sClass _
            , 255 _
            )
'--
        sClass = _
            Left _
            ( _
            sClass _
            , nClassLen _
            )
'--
        hOwnerWnd = _
            GetParent _
            ( _
            nParentWnd _
            )

        If nTextLen > 0 _
        Or Check2.Value = vbChecked Then

            List1.AddItem _
                hOwnerWnd _
                & vbTab _
                & nParentWnd _
                & vbTab _
                & String(nLevel, "+") _
                & vbTab _
                & Left(sClass & Space(42), 42) _
                & " " _
                & sText

            nDisplayCount = _
                    nDisplayCount + 1
        End If
'--
        hWnd = GetWindow _
                    ( _
                    nParentWnd _
                    , GW_CHILD _
                    )
'--
        If hWnd > 0 _
        And ( _
            Check1.Value = vbChecked _
            Or nLevel < 1 _
            ) _
            Then _
                GetChildren (hWnd)
'--
        nParentWnd = _
            GetWindow _
                ( _
                nParentWnd _
                , GW_HWNDNEXT _
                )
'--
    Loop
'-------
    nLevel = nLevel - 1
End Sub

Private Sub Form_Load()
    Me.Caption = "Task List"
    Me.BorderStyle = 4
    Command1.Caption = "Refresh"
    List1.FontName = "Courier New"
    Check1.Caption = "Children"
    Check2.Caption = "Blank"
    Check3.Caption = "Auto"
    Timer1.Interval = 1000
    Timer1.Enabled = False
    Me.Move _
            0 _
            , 0 _
            , Screen.Width / 2 _
            , Screen.Height / 2
    Command1_Click
End Sub

Private Sub Form_Resize()
    If Me.WindowState = vbMaximized _
    Or Me.WindowState = vbMinimized Then _
        Me.WindowState = vbNormal

    List1.Move _
            120 _
            , 120 _
            , (Me.Width - 1500) _
            , Me.Height - 540
    Command1.Move _
            Me.Width - 1260 _
            , 120 _
            , 1100 _
            , 400
    Check1.Move _
            Me.Width - 1260 _
            , 600 _
            , 1100 _
            , 240
    Check2.Move _
            Me.Width - 1260 _
            , 900 _
            , 1100 _
            , 240
    Check3.Move _
            Me.Width - 1260 _
            , 1200 _
            , 1100 _
            , 240
    Label1.Move _
            Me.Width - 1260 _
            , 1500 _
            , 1100 _
            , 240

End Sub

Private Sub Timer1_Timer()
    Command1_Click
End Sub




  Return to Index