【转】C#利用金山词霸的xdictgrb.dll实现鼠标取词

金山词霸的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);
        }
    }
}

结果如下:

转载于:https://www.cnblogs.com/jiangj/archive/2010/09/26/1836159.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值