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...
Printable View
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...
Try:VB Code:
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long 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 Private Const WM_GETTEXT = &HD Private Const WM_GETTEXTLENGTH = &HE Private Type POINTAPI x As Long y As Long End Type Private Sub Form_Load() Timer1.Interval = 1 End Sub Private Sub Form_Resize() Text1.Move 0, 0, ScaleWidth, ScaleHeight End Sub Private Sub Timer1_Timer() Dim lhWnd As Long Dim sText As String Dim lLen As Long Dim tPOINT As POINTAPI Call GetCursorPos(tPOINT) lhWnd = WindowFromPoint(tPOINT.x, tPOINT.y) lLen = SendMessage(lhWnd, WM_GETTEXTLENGTH, 0, ByVal 0&) + 1 sText = Space(lLen) Call SendMessage(lhWnd, WM_GETTEXT, lLen, ByVal sText) Text1 = sText End Sub
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??
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
MSDNYou 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.Quote:
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.
In a Module:Example Usage:VB Code:
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 Public Function GetAllWindowsByClass(ByVal ParentHwnd As Long, ByVal sClass As String) As Variant Dim lHwnd As Long Dim aHwnds() As Long Dim lCount As Long Do lHwnd = FindWindowEx(ParentHwnd, lHwnd, sClass, vbNullString) If lHwnd Then ReDim Preserve aHwnds(lCount) aHwnds(lCount) = lHwnd lCount = lCount + 1 End If Loop While lHwnd GetAllWindowsByClass = IIf(lCount, aHwnds, Array()) End FunctionAlternatively 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.VB Code:
Private Sub Command1_Click() Dim aList As Variant Dim lIndex As Long aList = GetAllWindowsByClass(hWnd, "ThunderTextBox") For lIndex = 0 To UBound(aList) Debug.Print aList(lIndex) Next End Sub
In a Module:Example Usage:VB Code:
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private aHwndsEnum() As Long Private lCountEnum As Long Private sClassEnum As String Public Function GetAllWindowsByClass_Enum(ByVal ParentHwnd As Long, ByVal sClass As String) As Variant ReDim aHwndsEnum(0) lCountEnum = 0 sClassEnum = sClass Call EnumChildWindows(ParentHwnd, AddressOf ChildEnumProc, 0) GetAllWindowsByClass_Enum = aHwndsEnum End Function Private Function ChildEnumProc(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim sClass As String sClass = Space(255) sClass = Left(sClass, GetClassName(hWnd, ByVal sClass, 255)) If LCase(sClass) = LCase(sClassEnum) Then ReDim Preserve aHwndsEnum(lCountEnum) aHwndsEnum(lCountEnum) = hWnd lCountEnum = lCountEnum + 1 End If ChildEnumProc = hWnd End FunctionVB Code:
Private Sub Command1_Click() Dim aList As Variant Dim lIndex As Long aList = GetAllWindowsByClass_Enum(hWnd, "ThunderTextBox") For lIndex = 0 To UBound(aList) Debug.Print aList(lIndex) Next End Sub