【转】C#利用金山词霸的xdictgrb.dll实现鼠标取词
时间:2010-09-26 来源:shaya
金山词霸的xdictgrb.dll程序集是很容易找到的,然后引用到项目中;
新建Windows窗口应用程序,添加四个显示控件,例如:TextBox或Label都可以。
实现代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using XDICTGRB;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form,IXDictGrabSink
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos( //窗口永远置前
IntPtr hwnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags
);
private void Form1_Load(object sender, EventArgs e)
{
//窗口永远置前
SetWindowPos(this.Handle, -1, Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2, this.Width, this.Height, 0);
GrabProxy gp = new GrabProxy();
gp.GrabInterval = 1;//指抓取时间间隔
gp.GrabMode = XDictGrabModeEnum.XDictGrabMouse;//设定取词的属性
gp.GrabFlag = XDictGrabFlagEnum.XDictGrabOnlyEnglish;//只取英文
gp.GrabEnabled = true;//是否取词的属性
gp.AdviseGrab(this);
}
//接口的实现
int IXDictGrabSink.QueryWord(string WordString, int lCursorX, int lCursorY, string SentenceString, ref int lLoc, ref int lStart)
{
this.textBox4.Text = SentenceString;//鼠标所在语句
this.textBox2.Text = SentenceString.Substring(lLoc ,1);//鼠标所在字符
this.textBox3.Text = lCursorX.ToString() + " " + lCursorY.ToString();
this.textBox1.Text = GetWord(SentenceString, lLoc + 1);//取得单词
return 1;
}
//取得单词的Method
private string GetWord(string SentenceString, int lLoc)
{
int iR=0;
int iL=0;
int ilen=0;
ilen = SentenceString.Length;
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (iL = lLoc; iL > 0; iL--)
{
if (str.IndexOf(SentenceString.Substring(iL, 1)) == -1)
break;
}
for (iR = lLoc; iR< ilen; iR++)
{
if (str.IndexOf(SentenceString.Substring(iR, 1)) ==-1)
break;
}
return SentenceString.Substring(iL+1, iR - iL-1);
}
}
}
结果如下:
