在网上看到的关于 C# 利用金山词霸2005 XdictGrb.dll 模块开发屏幕取词。代码如下: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
    {
        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);
            
            int i;
            GrabProxy gp = new GrabProxy();//取词代理对象
            gp.GrabInterval = 1;//指抓取时间间隔 
            gp.GrabMode = XDictGrabModeEnum.XDictGrabMouse;//设定取词的属性 
            //gp.GrabFlag = XDictGrabFlagEnum.XDictGrabOnlyEnglish;//只取英文            //Const XDictGrabDisableButton = 4
            //不取按钮上的文字
            //Const XDictGrabDisableCaption = 8
            //不取标题栏的文字
            //Const XDictGrabDisableMenu = 2
            //不取菜单的文字
            //Const XDictGrabOnlyEnglish = 1
            //只取英文            gp.GrabFlag = (XDictGrabFlagEnum.XDictGrabDisableButton & XDictGrabFlagEnum.XDictGrabDisableMenu & XDictGrabFlagEnum.XDictGrabDisableCaption);
            gp.GrabEnabled = true;//是否取词的属性 
            i = 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);
        } 
        /////
    }
}
可是使用VS2010编译有很多问题
1.  
i = gp.AdviseGrab(this);//要实现的接口,   这个函数有问题
错误 1 与“XDICTGRB.IGrabProxy.AdviseGrab(XDICTGRB.IXDictGrabSink)”最匹配的重载方法具有一些无效参数 D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 17 WindowsFormsApplication1
错误 2 参数 1: 无法从“WindowsFormsApplication1.Form1”转换为“XDICTGRB.IXDictGrabSink” D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 31 WindowsFormsApplication12.
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);//取得单词错误 3 “WindowsFormsApplication1.Form1”不包含“textBox4”的定义,并且找不到可接受类型为“WindowsFormsApplication1.Form1”的第一个参数的扩展方法“textBox4”(是否缺少 using 指令或程序集引用?) D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 60 18 WindowsFormsApplication1
错误 4 “WindowsFormsApplication1.Form1”不包含“textBox2”的定义,并且找不到可接受类型为“WindowsFormsApplication1.Form1”的第一个参数的扩展方法“textBox2”(是否缺少 using 指令或程序集引用?) D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 61 18 WindowsFormsApplication1
错误 5 “WindowsFormsApplication1.Form1”不包含“textBox3”的定义,并且找不到可接受类型为“WindowsFormsApplication1.Form1”的第一个参数的扩展方法“textBox3”(是否缺少 using 指令或程序集引用?) D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 62 18 WindowsFormsApplication1
错误 6 “WindowsFormsApplication1.Form1”不包含“textBox1”的定义,并且找不到可接受类型为“WindowsFormsApplication1.Form1”的第一个参数的扩展方法“textBox1”(是否缺少 using 指令或程序集引用?) D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 63 18 WindowsFormsApplication1
有着方面经验的大虾求解

解决方案 »

  1.   

    textBox  是文本框控件.你要建立一个winform...然后可以拖动textBox 到窗体上.基本上能解决错误3.4.5.6
      

  2.   


    顶!i = gp.AdviseGrab(this);//要实现的接口, 这个函数有问题
    错误 1 与“XDICTGRB.IGrabProxy.AdviseGrab(XDICTGRB.IXDictGrabSink)”最匹配的重载方法具有一些无效参数 D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 17 WindowsFormsApplication1
    错误 2 参数 1: 无法从“WindowsFormsApplication1.Form1”转换为“XDICTGRB.IXDictGrabSink” D:\Study\临时实验\屏幕取词\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 31 WindowsFormsApplication1
    错误 1 说的很清楚,他要求AdviseGrab的参数必须是XDICTGRB.IXDictGrabSink,或者子类
    错误 2 意思是编译器想把Form1转化成IXDictGrabSink,但是你并没有实现他的借口,所以类型转化失败
      

  3.   

    public partial class Form1 : Form,IXDictGrabSink
    改成如此便可