[Win] 用C#调用金山词霸XDictGrb.dll屏幕取词

博客首页 » Win 用C#调用金山词霸XDictGrb.dll屏幕取词

发布于 28 Dec 2015 06:52
标签 blog
金山词霸的屏幕取词功能不错,自己做的话对于各个Windows版本支持就不够完善强壮了,那么如何调用金山词霸的取词功能呢。
http://www.ithao123.cn/content-5746834.html

确保已经登记XdictGrb 1.0 Type Library,如果没有则用regsvr32 XdictGrb.dll添加。
在Visual Studio中创建一个新的Windows Forms工程,然后放置4个Text,添加COM的XdictGrb 1.0 Type Library参照,把Form1的代码替换如下。

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using XDICTGRB;

namespace GrabDemo
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form,IXDictGrabSink 
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 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
            );

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(32, 32);
            this.textBox1.Name = "textBox1";
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "textBox1";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(144, 32);
            this.textBox2.Name = "textBox2";
            this.textBox2.TabIndex = 1;
            this.textBox2.Text = "textBox2";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(32, 64);
            this.textBox3.Name = "textBox3";
            this.textBox3.TabIndex = 2;
            this.textBox3.Text = "textBox3";
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(144, 64);
            this.textBox4.Name = "textBox4";
            this.textBox4.TabIndex = 3;
            this.textBox4.Text = "textBox4";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.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);
        } 
    }    
}

另外,在最新版的金山词霸的库是cbgrabproxy.dll,不知道Interface还是不是一样
cbgrabproxy 1.0 type library


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Win 用C#调用金山词霸XDictGrb.dll屏幕取词

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment