积极答复者
winform下如何获取ie当前打开的标签页地址

问题
-
一开始根据SHDocVw.ShellWindows取得所有窗口,再判断此窗口是否为正在使用的ie,功能虽实现了,但发现ie7下使用多标签就不行了,得到的是整个浏览器所有标签的网址,代码如下:
- C# code
// 取得所有窗口 SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); foreach (SHDocVw.InternetExplorer ie in shellWindows) { // 判断窗口是否为 iexplore if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals("iexplore")) { // 判断此 Internet Explorer 是否为正在使用的窗口 if (ie.HWND == GetForegroundWindow().ToInt32()) { label1.Text = ie.HWND.ToString(); } } }这段代码只有在浏览器只打开一个标签才有意义,我想实现的效果是不论打开了多少标签,总能获得当前活动标签的网址。
另一种方法是得到当前标签的句柄,怎么通过这个句柄得到地址栏的网址?
- 已编辑 统一爱吃康师傅 2011年4月20日 1:47
答案
全部回复
-
Hi,
我有一种方法可以实现。
代码先贴上:
SHDocVw.ShellWindows shellWindows = new ShellWindows();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
// 判斷視窗是否為 iexplore
if (Path.GetFileNameWithoutExtension(ie.FullName).ToLower().Equals("iexplore"))
{
mshtml.HTMLDocument hd = ie.Document as mshtml.HTMLDocument;
mshtml.IHTMLElement he = hd.activeElement;
textBox1.Text += he.outerHTML;
//if (ie.Visible == true)
//{
// textBox1.Text += hd.documentElement.outerHTML;
//}
}
}
经过测试,是可以的。注释掉的那种方法,效率太低。虽然说结果一致。
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.