首页上一页 1 下一页尾页 3 条记录 1/1页
[请教]关于屏幕取词
发表在VB答疑区
2010-06-07
是否精华
是
否
版块置顶:
是
否
正在学习VB范例完全自学手册.对第5章060实例比较感兴趣.
该范例只能对桌面的图片的名称进行取词,无法对文本文档,网页文字,Word文档,QQ聊天对话框等文本进行取词.
因为是自学,对API不太了解,
请教明日的老师们,要对其余的文本取词,应该如何修改代码,谢谢.
以下是关键代码片段:
Public Function MyHook(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error GoTo 1
Dim i As Long
Dim pos As Long
Dim carpos As Long
Dim lc As Long
If ncode = HC_ACTION Then
hwd = GetFocus() '获得拥有输入焦点的窗口的句柄
If hwd <> Form1.hwnd Then
GetWindowRect hwd, focus_rect '获得范围矩形
GetCaretPos caret_pos '判断插入符的当前位置
Form1.Top = (focus_rect.Top + caret_pos.Y + 20) * Screen.TwipsPerPixelY
Form1.Left = (focus_rect.Left + caret_pos.X + 10) * Screen.TwipsPerPixelX
pos = caret_pos.X + caret_pos.Y * 65536
carpos = SendMessage(hwd, EM_CHARFORMPOS, 0, ByVal pos) '获取指定位置的字符串
lc = carpos Mod 65536
Form1.Text1.Text = GetWord(lc)
If Form1.Caption <> GetWord(lc) Then
Form1.Caption = GetWord(lc)
End If
End If
Else
CallNextHookEx haw, ncode, wParam, lParam '下一个钩子
End If
CallNextHookEx haw, ncode, wParam, lParam
1: End Function
Public Function GetWord(pos As Long) As String
On Error Resume Next
Dim pos1 As Integer, pos2 As Integer, i As Integer
Dim strlen As Long
Dim st() As Byte
strlen = SendMessage(hwd, WM_GETTEXTLENGTH, 0, 0) + 1 '获取字符串长度
ReDim st(strlen) As Byte
SendMessage hwd, WM_GETTEXT, strlen, st(0)
pos1 = 0: pos2 = UBound(st)
For i = pos - 1 To 0 Step -1 '向前搜索分格符的位置
If IsDelimiter(st(i)) Then
pos1 = i + 1
Exit For
End If
Next
For i = pos To UBound(st) '向后搜寻分隔符字符的位置
If IsDelimiter(st(i)) Then
pos2 = i - 1
Exit For
End If
Next
If pos2 > pos1 Then '截取pos1-pos2之间的字符,以构成一个单词
ReDim bArr2(pos2 - pos1) As Byte
For i = pos1 To pos2
bArr2(i - pos1) = st(i)
Next
GetWord = StrConv(bArr2, vbUnicode)
Else
GetWord = ""
End If
End Function
非常感谢,!!!
该范例只能对桌面的图片的名称进行取词,无法对文本文档,网页文字,Word文档,QQ聊天对话框等文本进行取词.
因为是自学,对API不太了解,
请教明日的老师们,要对其余的文本取词,应该如何修改代码,谢谢.
以下是关键代码片段:
Public Function MyHook(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Error GoTo 1
Dim i As Long
Dim pos As Long
Dim carpos As Long
Dim lc As Long
If ncode = HC_ACTION Then
hwd = GetFocus() '获得拥有输入焦点的窗口的句柄
If hwd <> Form1.hwnd Then
GetWindowRect hwd, focus_rect '获得范围矩形
GetCaretPos caret_pos '判断插入符的当前位置
Form1.Top = (focus_rect.Top + caret_pos.Y + 20) * Screen.TwipsPerPixelY
Form1.Left = (focus_rect.Left + caret_pos.X + 10) * Screen.TwipsPerPixelX
pos = caret_pos.X + caret_pos.Y * 65536
carpos = SendMessage(hwd, EM_CHARFORMPOS, 0, ByVal pos) '获取指定位置的字符串
lc = carpos Mod 65536
Form1.Text1.Text = GetWord(lc)
If Form1.Caption <> GetWord(lc) Then
Form1.Caption = GetWord(lc)
End If
End If
Else
CallNextHookEx haw, ncode, wParam, lParam '下一个钩子
End If
CallNextHookEx haw, ncode, wParam, lParam
1: End Function
Public Function GetWord(pos As Long) As String
On Error Resume Next
Dim pos1 As Integer, pos2 As Integer, i As Integer
Dim strlen As Long
Dim st() As Byte
strlen = SendMessage(hwd, WM_GETTEXTLENGTH, 0, 0) + 1 '获取字符串长度
ReDim st(strlen) As Byte
SendMessage hwd, WM_GETTEXT, strlen, st(0)
pos1 = 0: pos2 = UBound(st)
For i = pos - 1 To 0 Step -1 '向前搜索分格符的位置
If IsDelimiter(st(i)) Then
pos1 = i + 1
Exit For
End If
Next
For i = pos To UBound(st) '向后搜寻分隔符字符的位置
If IsDelimiter(st(i)) Then
pos2 = i - 1
Exit For
End If
Next
If pos2 > pos1 Then '截取pos1-pos2之间的字符,以构成一个单词
ReDim bArr2(pos2 - pos1) As Byte
For i = pos1 To pos2
bArr2(i - pos1) = st(i)
Next
GetWord = StrConv(bArr2, vbUnicode)
Else
GetWord = ""
End If
End Function
非常感谢,!!!