日期:2014-05-17  浏览次数:21190 次

.NET 获取系统鼠标选中的文本

C# 有没有办法获取鼠标选中的文本,类似于有道词典一样,打开了词典,鼠标移动可以自动获取页面的文本翻译

如果采用c++也是可以的,求大神帮忙分享一下经验
.net c++ c# 获取鼠标选中文本

------解决方案--------------------
引用:
引用:
this.textBox1.SelectedText
this.richTextBox1.SelectedText如果想根据选中文字变化而进行相应操作可以设置一个属性=this.textBox1.SelectedText,定义委托事件监控属性的值变化~

mark
说得对
------解决方案--------------------
A、金山词霸组件法

在金山词霸中2005中带了一个XdictGrb.dll,添加引用

(为方便大家测试,本人安装了金山词霸2005,把其中的XdictGrb.dll给大家下载)

废话不多说了,还是把源码放上

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XDICTGRB;//金山词霸组件

namespace WindowsApplication1
{
    public partial class Form1 : Form,IXDictGrabSink
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            GrabProxy gp = new GrabProxy();
            gp.GrabInterval = 1;//指抓取时间间隔
            gp.GrabMode = XDictGrabModeEnum.XDictGrabMouse;//设定取词的属性
            gp.GrabEnabled = true;//是否取词的属性
            gp.AdviseGrab(this);
        }
        //接口的实现
        int IXDictGrabSink.QueryWord(string WordString, int lCursorX, int lCursorY, string SentenceString, ref int lLoc, ref int lStart)
        {
            this.textBox1.Text = SentenceString;//鼠标所在语句
            //this.textBox1.Text = SentenceString.Substring(lLoc + 1,1);//鼠标所在字符
            return 1;
        }
    }
}

B.Nhw32.dll法

这个是C++写的一个组件

nhw32.dll 主要引出两个函数:

1. DWORD WINAPI BL_SetFlag32(UINT nFlag,
                             HWND hNotifyWnd,
                             int MouseX,