高手请进。C# 取得IE浏览器当前页面的内容

lai178 2009-07-01 11:02:16
原文:http://www.omegacoder.com/?p=71

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
string filename;

int a = shellWindows.Count;
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{

filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("ttraveler"))
{
Console.WriteLine("Web Site : {0}", ie.LocationURL);
mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;
string aa = htmlDoc != null ? htmlDoc.body.outerHTML.ToString() : "***Failed***";
this.textBox1.Text = aa;

}
else
{
MessageBox.Show("erroe");
}

}
}
我这样,是可以得到所有打开的IE窗口HTML内容,可是我要得到当前那个,昨办。
...全文
1106 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
mugenmn 2009-07-03
  • 打赏
  • 举报
回复
mark 先
himoggy 2009-07-03
  • 打赏
  • 举报
回复
GetActiveWindow?
yuyu622 2009-07-03
  • 打赏
  • 举报
回复
收藏了。
lai178 2009-07-03
  • 打赏
  • 举报
回复
貌似也不行,我现在想通过win发送消息来截获,不知可行否。现在难就难在获取当前那个窗口,。COM操作又不熟悉。
lai178 2009-07-02
  • 打赏
  • 举报
回复
http://hi.baidu.com/f_fx/blog/item/c60513f733a0922b730eec46.html
这有一文章,你看下有没有用。
lai178 2009-07-02
  • 打赏
  • 举报
回复
是的,就是主要要判断“当前页面”标题栏也判断不了吧。。我不太明白,你试下。。
himoggy 2009-07-02
  • 打赏
  • 举报
回复
通过标题栏获取需要的页面- -

其实我们在讨论的就是怎么判断浏览器是“当前页面”
lai178 2009-07-02
  • 打赏
  • 举报
回复
试了下,用IE不会报错,但是这样也得不到内容,因为
if (ie.HWND == activeHwnd.ToInt32())
{
textBox1.Text = htmlDoc.body.outerHTML;
ie.Refresh();
}
这判断里面的永远不会相等,有其它招数吗?
himoggy 2009-07-02
  • 打赏
  • 举报
回复
= =最近真忙- -
他是通过句柄来获取页面内容

你的问题有两个,一是“当前”,二是“浏览器”
“当前”的标准是什么,最直白的判断应该是激活的
另外,这个被激活的应该是浏览器。

if (ie.HWND == activeHwnd.ToInt32()) 永远也不会相等
可能就是因为TT只在子窗口使用了IE的内核,而COM取到的只有子窗口的句柄。
具体的你可以用spy++查一下(= =我没装tt)
如果是子窗口的值相等,你可以考虑遍历TT的子窗口,获得所有句柄再进行对比

IE得不到内容可能是因为IE7以上有了TAB的问题
考虑尝试以下代码

public static string GetTabHtml(string theUrl)
{
shellWindows = new ShellWindowsClass();
string tabHtml = "";

foreach (SHDocVw.WebBrowser tab in shellWindows)
{
if (theUrl == tab.LocationURL)
{
HTMLDocument currentTab = tab.Document as mshtml.HTMLDocument;
tabHtml = currentTab.body.outerHTML;
}
}

return tabHtml;
}


himoggy 2009-07-01
  • 打赏
  • 举报
回复
不会吧0.0
user32.dll是系统dll
在system32或者spFiles里都应该有
lai178 2009-07-01
  • 打赏
  • 举报
回复
user32.dll在那引用,我找不到,我的是2003系统,VS2008
himoggy 2009-07-01
  • 打赏
  • 举报
回复
- -貌似也没增加啥变量啊
写一个

        [DllImport("user32.dll")]
extern static IntPtr GetForegroundWindow();

private void Form1_Load(object sender, EventArgs e)
{
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
string filename;

int a = shellWindows.Count;

foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore"))
{
Console.WriteLine("Web Site : {0}", ie.LocationURL);
mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;
IntPtr activeHwnd = GetForegroundWindow();
if (htmlDoc != null)
{
if (ie.HWND == activeHwnd.ToInt32())
{
textBox1.Text = htmlDoc.body.outerHTML;
ie.Refresh();
}
}
}
}
}
lai178 2009-07-01
  • 打赏
  • 举报
回复
当前上下文不存在GetForegroundWindow,要引用什么吗?请问。
lai178 2009-07-01
  • 打赏
  • 举报
回复
麻烦你把你的代码放在我程序合适的位置,有些变量我不知你在那来的,谢谢了。
himoggy 2009-07-01
  • 打赏
  • 举报
回复
貌似msdn上讨论过这个问题- -

有tag的话(IE7以上)
string tmp = GetTabHtml(URL.ToString());
if(tmp != null || tmp != "")
{
string empty = tmp.Trim();
if (empty != "")
{

没有的话
可以尝试
IntPtr activeHwnd = GetForegroundWindow();
if(doc != null )
{
if (ie.HWND == activeHwnd.ToInt32())
{
textBox1.Text = doc.body.outerHTML;
ie.Refresh();
}
}
}

lai178 2009-07-01
  • 打赏
  • 举报
回复
是的,都是用ie的内核,只是外包装了一层,现在是他不能调用此属性。ie.HWND
himoggy 2009-07-01
  • 打赏
  • 举报
回复
这个就不清楚了 你用IE来运行试试
SHDocVw.InternetExplorer 对于基于IE的浏览器理论上是一样的吧?

要不你加个MessageBox.Show(GetLastError());看能不能获得最后出错时的错误号
lai178 2009-07-01
  • 打赏
  • 举报
回复
是的,我是用TT,调试时也用TT,你那不会报错吗?是不是我什么服务给我关了。。
himoggy 2009-07-01
  • 打赏
  • 举报
回复
那有可能是ie版本的问题了

刚注意到你用的是TT?
那么调试的时候也是用TT的?

这个错误暂时没查到资料的说
lai178 2009-07-01
  • 打赏
  • 举报
回复
ie.HWND 一执行这个就报错,
加载更多回复(9)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧