今天想要做类似金山词霸屏幕取词的功能,在网上找了一些资料,发现都很复杂。既然金山词霸已经实
现了,借用之:
金山词霸中2005中带了一个
XdictGrb.dll,其实是一个COM组件,并且实现了一个取词的插件框架。
其中的有以下内容:
1 接口
1.1 GrabProxy 取词代理对象
Function AdviseGrab(XDictGrabSink As IXDictGrabSink) As Long广播一个XDictGrabSink(如果要实现取词功能,你需要自己来实现这个接口,具体的方法后面会有介绍。(Visual Basic的示例,使用其他语言类似)) 接口,在需要取词的时候会调用XDictGrabSink的QueryWord方法,你可以在这里实现你需要的功能。Property GrabEnabled As Boolean是否取词的属性Property GrabMode As XDictGrabModeEnum设定取词的属性,XDictGrabFlagEnum的定义后面会讲到。Property GrabFlag As XDictGrabFlagEnum设定取词健,比如是否要结合Ctrl或者Shift.Property GrabSystemCP As Long这个属性暂时不太清楚。Sub GrabWord(lCursorX As Long,lCursorY As Long)取x,y坐标的词,估计是屏幕坐标。Sub UnadviseGrab(lCookie As Long)取消对XDictGrabSink接口的广播。Property XDictGrabPlugins As IPluginsCollection返回一个IPluginsCollection集合。1.2 IPluginsCollection 插件集合对象1.3 IXDictGrabPlugin 插件接口1.5 IXDictGrabSink 自己要实现的接口,参见后面的例子。Function QueryWord(WordString As String,lCursorX As Long,lCursorY As Long,SentenceString As String,lLoc As Long,lStart As Long) As Long
2 常量定义
2.1 XDictGrabFlagEnumConst XDictGrabDisableButton = 4不取按钮上的文字Const XDictGrabDisableCaption = 8Const XDictGrabDisableMenu = 2Const XDictGrabOnlyEnglish = 1只取英文以上几个常量可以用与(AND)的方式进行组合。2.2 XDictGrabModeEnumConst XDictGrabMouse = 1使用鼠标取词Const XDictGrabMouseWithCtrl = 3使用鼠标加Ctrl取词Const XDictGrabMouseWithMiddleButton = 4使用鼠标加鼠标中键进行取词Const XDictGrabMouseWithShift = 2使用鼠标加Shift进行取词2.3 Enum XDictGrabPluginPriorityEnum 设定插件的优先级Const XDictGrabPriorityChief = 4Const XDictGrabPriorityGeneral = 1Const XDictGrabPriorityNormal = 2Const XDictGrabPrioritySpecial = 3
3 示例代码(使用VB6实现)
Option Explicit
Implements IXDictGrabSink
Private gp As GrabProxy
Implements IXDictGrabSink
Private gp As GrabProxy
Private Sub Form_Load()
Set gp = New GrabProxy
With gp
.GrabEnabled = True '是否有效
.GrabInterval = 30 '指定抓取时间间隔
.GrabMode = XDictGrabMouse '模式(鼠标是否加按键)
.AdviseGrab Me '接口指向自身
Set gp = New GrabProxy
With gp
.GrabEnabled = True '是否有效
.GrabInterval = 30 '指定抓取时间间隔
.GrabMode = XDictGrabMouse '模式(鼠标是否加按键)
.AdviseGrab Me '接口指向自身
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set gp = Nothing
End Sub
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,lStart As Long) As Long
Label1.Caption = "当前坐标:" & "(" & lCursorX & "," & lCursorY & ")"
Label2.Caption = "当前语句:" & SentenceString
Label3.Caption = "当前字符:" & Mid(SentenceString,lLoc + 1,1000)
Label1.Caption = "当前坐标:" & "(" & lCursorX & "," & lCursorY & ")"
Label2.Caption = "当前语句:" & SentenceString
Label3.Caption = "当前字符:" & Mid(SentenceString,lLoc + 1,1000)
End Function
原文链接:https://www.f2er.com/vb/261975.html