|
-
May 28th, 2000, 11:53 PM
#12
Guru
This should be a little check program... Not an OCR one. (Maybe)
Extracting the text from graphics is OCR (Optical Character Recognition). It's a little too advanced for something like getting the text from ListBoxes. I haven't tested my code, but I think everything you wanted (except ListBoxes) works with the code I gave you.
Here is some code that will identify whether it's a ListBox or not. If it is, it will get the text of the currently selected item there. If it isn't, it will run the GetWindowText code I gave you earlier.
Try this code in a module (I haven't tested it, but it should work):
Code:
Option Explicit
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Const LB_GETCURSEL = &H188
Public Const LB_GETTEXT = &H189
Public Const LB_GETTEXTLEN = &H18A
Public Const LB_ERR = (-1)
Type POINTAPI
x As Long
y As Long
End Type
Sub TrimNulls(ByRef sString As String)
Dim lPos As Long
lPos = InStr(sString, vbNullChar) ' Scan the string for nulls.
If lPos > 0 Then sString = Left(sString, lPos - 1) ' Found one? Trim! Only leave whatever is before it.
End Sub
Function GetTextFromListBox(ByVal hWndListBox As Long) As String
Dim sBuffer As String, lBufferSize As Long, lCurSel As Long
lCurSel = SendMessage(hWndListBox, LB_GETCURSEL, 0, ByVal 0) ' Get the current selection index.
If lCurSel = LB_ERR Then Exit Function ' No selection? Return an empty string.
lBufferSize = SendMessage(hWndListBox, LB_GETTEXTLEN, lCurSel, ByVal 0) + 1 ' Get length + null place.
sBuffer = String(lBufferSize, vbNullChar) ' Reserve the correct amount of places.
Call SendMessage(hWndListBox, LB_GETTEXT, lCurSel, ByVal sBuffer) ' Get the text followed by a bunch of nulls.
Call TrimNulls(sBuffer) ' Trim all the nulls!
GetTextFromListBox = sBuffer
End Function
Function GetTextFromCursorPosition() As String
Dim sBuffer As String, lBufferSize As Long, hWndThingie As Long, ptCursorPos As POINTAPI
' First we get the hWnd of the thingie where the cursor is.
Call GetCursorPos(ptCursorPos)
hWndThingie = WindowFromPoint(ptCursorPos.x, ptCursorPos.y)
' Now, hWndThingie is the hWnd of the window where the mouse cursor is.
' We need to check if it's a ListBox or not.
sBuffer = String(255, vbNullChar) ' Reserve 255 places.
Call GetClassName(hWndThingie, sBuffer, 255) ' Get the ClassName followed by a bunch of nulls.
Call TrimNulls(sBuffer) ' Trim all the nulls!
' If you know any ListBox ClassNames other than "ListBox" and "ThunderListBox", add them!
If (sBuffer = "ListBox") Or (sBuffer = "ThunderListBox") Then
GetTextFromCursorPosition = GetTextFromListBox(hWndThingie)
Exit Function
End If
' If it's not a ListBox...
lBufferSize = GetWindowTextLength(hWndThingie) + 1 ' Get length + null place.
sBuffer = String(lBufferSize, vbNullChar) ' Reserve the correct amount of places.
Call GetWindowText(hWndThingie, sBuffer, lBufferSize) ' Get the text followed by a bunch of nulls.
Call TrimNulls(sString) ' Trim all the nulls!
GetTextFromCursorPosition = sBuffer
End Function
Same syntax as before, except now it works with ListBoxes.
If you want to get all the text of all the items in a ComboBox or a ListBox, then post a reply and tell me about it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|