Results 1 to 5 of 5

Thread: Getting Window Text

  1. #1

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Unhappy Thanx

    Umm... It does what I asked... but it doesn't work. I'm using it on a rich edit text box, class 'RichEdit20A' and it won't pick up the text, but it will on normal text boxes??? very annoying...
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Try:
    VB Code:
    1. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    2. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4.  
    5. Private Const WM_GETTEXT = &HD
    6. Private Const WM_GETTEXTLENGTH = &HE
    7.  
    8. Private Type POINTAPI
    9.     x As Long
    10.     y As Long
    11. End Type
    12.  
    13. Private Sub Form_Load()
    14.     Timer1.Interval = 1
    15. End Sub
    16.  
    17. Private Sub Form_Resize()
    18.     Text1.Move 0, 0, ScaleWidth, ScaleHeight
    19. End Sub
    20.  
    21. Private Sub Timer1_Timer()
    22.     Dim lhWnd As Long
    23.     Dim sText As String
    24.     Dim lLen As Long
    25.     Dim tPOINT As POINTAPI
    26.    
    27.     Call GetCursorPos(tPOINT)
    28.    
    29.     lhWnd = WindowFromPoint(tPOINT.x, tPOINT.y)
    30.     lLen = SendMessage(lhWnd, WM_GETTEXTLENGTH, 0, ByVal 0&) + 1
    31.     sText = Space(lLen)
    32.     Call SendMessage(lhWnd, WM_GETTEXT, lLen, ByVal sText)
    33.    
    34.     Text1 = sText
    35. End Sub

  3. #3

    Thread Starter
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Talking Perfect :)

    How come the GetWindowText wouldn't pick it up though?

    One more question... :S

    What if there are multiple windows with the same class and I'm using FindWindowEx?? It only finds the handle for the window that is at the top of the z-order, how can I get multiple handles so I can find the text for all of the windows??
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  4. #4
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    Isn't EM_STREAMOUT needed to get text from rich text boxes?

    You will have to enumerate all the windows in order to find more than the first from any class. Go here: http://www.mvps.org/vbnet/code/enums...indowsdemo.htm

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    MSDN
    The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.
    You can get the next Window handle with the same class by specifying the previous Child window handle as the Hwnd2 Parameter, which just indicates it should start looking after the specified handle, i.e.

    In a Module:
    VB Code:
    1. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    2.  
    3. Public Function GetAllWindowsByClass(ByVal ParentHwnd As Long, ByVal sClass As String) As Variant
    4.     Dim lHwnd As Long
    5.     Dim aHwnds() As Long
    6.     Dim lCount As Long
    7.    
    8.     Do
    9.         lHwnd = FindWindowEx(ParentHwnd, lHwnd, sClass, vbNullString)
    10.         If lHwnd Then
    11.             ReDim Preserve aHwnds(lCount)
    12.             aHwnds(lCount) = lHwnd
    13.             lCount = lCount + 1
    14.         End If
    15.     Loop While lHwnd
    16.    
    17.     GetAllWindowsByClass = IIf(lCount, aHwnds, Array())
    18. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim aList As Variant
    3.     Dim lIndex As Long
    4.    
    5.     aList = GetAllWindowsByClass(hWnd, "ThunderTextBox")
    6.     For lIndex = 0 To UBound(aList)
    7.         Debug.Print aList(lIndex)
    8.     Next
    9. End Sub
    Alternatively you can use the EnumChildWindows API to enumerate all Child Windows of the specified parent and check the class of each for a match, i.e.

    In a Module:
    VB Code:
    1. Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    2. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    3.  
    4. Private aHwndsEnum() As Long
    5. Private lCountEnum As Long
    6. Private sClassEnum As String
    7.  
    8. Public Function GetAllWindowsByClass_Enum(ByVal ParentHwnd As Long, ByVal sClass As String) As Variant
    9.     ReDim aHwndsEnum(0)
    10.     lCountEnum = 0
    11.     sClassEnum = sClass
    12.     Call EnumChildWindows(ParentHwnd, AddressOf ChildEnumProc, 0)
    13.     GetAllWindowsByClass_Enum = aHwndsEnum
    14. End Function
    15.  
    16. Private Function ChildEnumProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    17.     Dim sClass As String
    18.    
    19.     sClass = Space(255)
    20.     sClass = Left(sClass, GetClassName(hWnd, ByVal sClass, 255))
    21.     If LCase(sClass) = LCase(sClassEnum) Then
    22.         ReDim Preserve aHwndsEnum(lCountEnum)
    23.         aHwndsEnum(lCountEnum) = hWnd
    24.         lCountEnum = lCountEnum + 1
    25.     End If
    26.     ChildEnumProc = hWnd
    27. End Function
    Example Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim aList As Variant
    3.     Dim lIndex As Long
    4.  
    5.     aList = GetAllWindowsByClass_Enum(hWnd, "ThunderTextBox")
    6.     For lIndex = 0 To UBound(aList)
    7.         Debug.Print aList(lIndex)
    8.     Next
    9. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width