//using SHDocVw; 需要添加引用 COM:MicroSoft Internet Control
//using mshtml; 需要添加引用 COM:MicroSoft HTML Object Libary
//using System.Runtime.InteropServices;
// 调用:textBox1.Text=GetSelectedIEtext();
[DllImport("user32", EntryPoint="FindWindow")]
public static extern int FindWindowA(string lpClassName, string lpWindowName);
/// <summary>
/// 获取网页鼠标选择文本内容,只支持IE
/// </summary>
/// <returns></returns>
public static string GetSelectedIEtext()
{
int ieHwnd=FindWindowA("IEFrame",null);
IShellWindows sw=new ShellWindowsClass();
string res=null;
for (int i=sw.Count-1;i>=0;i--)
{
try
{
SHDocVw.IWebBrowser2 ib=sw.Item(i) as SHDocVw.IWebBrowser2;
if(((SHDocVw.IWebBrowser2)sw.Item(i)).HWND==ieHwnd)
{
mshtml.IHTMLTxtRange txt=((HTMLDocumentClass)ib.Document).selection.createRange() as IHTMLTxtRange;
res=txt.text;
break;
}
}
catch
{
res=null;
}
}
return res;
}