用API函数GetWindowText。
10年积累的成都做网站、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有青山湖免费网站建设让你可以放心的选择与我们合作。
GetWindowText
VB声明
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
说明
取得一个窗体的标题文字,或者一个控件的内容
返回值:Long,复制到lpString的字串长度;不包括空中止字符。会设置GetLastError
参数表
hwnd: Long,欲获取文字的那个窗口的句柄
lpString: String,预定义的一个缓冲区,至少有cch+1个字符大小;随同窗口文字载入
cch: Long,lpString缓冲区的长度
注解
不能用它从另一个应用程序的编辑控件中获取文字
我要补充以下,如果不知道句柄,那么可以用For计数循环进行穷举。
如果你装了金山词霸,可以这样
从“引用”中添加XdictGrb.dll
Option Explicit
Implements IXDictGrabSink
Private gp As GrabProxyPrivate Sub Form_Load()
Set gp = New GrabProxy
With gp
.GrabEnabled = True '是否有效
.GrabInterval = 30 '指定抓取时间间隔
.GrabMode = XDictGrabMouse '模式(鼠标是否加按键)
.AdviseGrab Me '接口指向自身End WithEnd SubPrivate Sub Form_Unload(Cancel As Integer)
Set gp = Nothing
End Sub
Private Function IXDictGrabSink_QueryWord(ByVal WordString As String, ByVal lCursorX As Long, ByVal lCursorY As Long, ByVal SentenceString As String, lLoc As Long, lStart As Long) As Long
Label1.Caption = "当前坐标:" "(" lCursorX "," lCursorY ")"
Label2.Caption = "当前语句:" SentenceString
Label3.Caption = "当前字符:" Mid(SentenceString, lLoc + 1, 1000)
End Function
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
TextBox1.SelectAll()
TextBox1.ForeColor = Color.Blue
End Sub
如果不想每次都这样的话,就加一个if判断TextBox1.ForeColor 是否是Color.Blue就可以了
添加引用:Microsoft.mshtml,然后代码如下即可:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'获取用户选中的文字
Dim htmlDocument As mshtml.IHTMLDocument2 'IHTMLDocument2
htmlDocument = WebBrowser1.Document.DomDocument
Dim currentSelection As mshtml.IHTMLSelectionObject
currentSelection = htmlDocument.selection
Dim range As mshtml.IHTMLTxtRange
If Not (currentSelection Is Nothing) Then
range = currentSelection.createRange()
If Not (range Is Nothing) Then
MsgBox(range.text)
'tbKeyWord.Text = range.text;
End If
End If
End Sub