网上有2种方法,一种是利用金山词霸组件,一种是利用nhw32.dll组件实现,这2种方法的代码我都下载过,可以运行,但是都取不了词,不知道是什么原因?请回复的大侠们就不要提其他如利用c++等方法,因为时间不允许我在去研究如何用c++或者c来实现这个功能,希望大家最好就利用金山词霸这个组件的方法,给我一点建议,或者给我一个能够运行且取得到词的例子,我会加分的,不甚感激!下面是我在网上找的代码,贴出来给大家参考!方法1:利用金山词霸组件 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;//是否取词的属性
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;
} //取得单词
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);
}
}
另一个版本的方法: public Form1()
{
InitializeComponent();
GrabProxy gp = new GrabProxy();
gp.GrabInterval = 20; //取词时间间隔
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.textBox2.Text = "X = " + lCursorX.ToString(); //鼠标坐标
this.textBox3.Text = "Y = " + lCursorY.ToString();
return 1;
} private void Form1_Load(object sender, EventArgs e)
{
}
方法2:利用nhw32.dll组件 [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern uint BL_SetFlag32(uint nFlag, IntPtr hNotifyWnd, int MouseX, int MouseY); [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern uint BL_GetText32([MarshalAs(UnmanagedType.LPStr)]StringBuilder lpszCurWord, int nBufferSize, ref Rectangle lpWordRect); [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern bool SetNHW32(); static void Main(string[] args)
{
int bufSize = 256;
StringBuilder sb = new StringBuilder(bufSize); Rectangle rect = new Rectangle(0, 0, 100, 100); if (SetNHW32())
{
BL_SetFlag32(1001, IntPtr.Zero, 100, 100);
BL_GetText32(sb, bufSize, ref rect);
} Console.WriteLine(sb);
Console.ReadLine();
}
{
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;//是否取词的属性
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;
} //取得单词
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);
}
}
另一个版本的方法: public Form1()
{
InitializeComponent();
GrabProxy gp = new GrabProxy();
gp.GrabInterval = 20; //取词时间间隔
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.textBox2.Text = "X = " + lCursorX.ToString(); //鼠标坐标
this.textBox3.Text = "Y = " + lCursorY.ToString();
return 1;
} private void Form1_Load(object sender, EventArgs e)
{
}
方法2:利用nhw32.dll组件 [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern uint BL_SetFlag32(uint nFlag, IntPtr hNotifyWnd, int MouseX, int MouseY); [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern uint BL_GetText32([MarshalAs(UnmanagedType.LPStr)]StringBuilder lpszCurWord, int nBufferSize, ref Rectangle lpWordRect); [DllImport(@"C:\Users\win7\Desktop\nhw32.dll")]
private static extern bool SetNHW32(); static void Main(string[] args)
{
int bufSize = 256;
StringBuilder sb = new StringBuilder(bufSize); Rectangle rect = new Rectangle(0, 0, 100, 100); if (SetNHW32())
{
BL_SetFlag32(1001, IntPtr.Zero, 100, 100);
BL_GetText32(sb, bufSize, ref rect);
} Console.WriteLine(sb);
Console.ReadLine();
}
大哥,这两个方法我都试过了,不行,哪里good?