我在网上找到一段C#代码,是用来实现得到当前IE中选中的文本。请哪位大虾把这段代码改成VC2005下的代码?
//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;
}
//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;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货