简体   繁体   English

如何在c#WebBrowser控件中捕获上下文菜单“查看源代码”?

[英]How to capture context menu “view source” click in c# WebBrowser control?

I am trying to intercept a click on the "view source" item on C# WebBrowser's context menu. 我试图拦截单击C#WebBrowser上下文菜单上的“查看源代码”项。 The default click opens the web page's source in notepad, but I would like, to cancel the default action and open my own form with the source code in a RichTextBox, which is a part of the opening window. 默认的单击会在记事本中打开网页的源代码,但是我想取消默认操作,并在RichTextBox(这是打开窗口的一部分)中使用源代码打开自己的表单。

Thanks to Robert I managed to intercept opening right-clicked links in new tabs instead of new windows ( 多亏了Robert,我设法拦截了在新标签页(而不是新窗口)中打开右键单击的链接( Open link in new TAB (WebBrowser Control) ), but I was unable to capture the "view source" click. 在新的TAB(WebBrowser控件)中打开链接 ,但是我无法捕获“查看源代码”单击。

I would appreciate any advices and solutions to my problem. 对于我的问题,我将不胜感激。

Thank you in advance :) 先感谢您 :)

Late reply, but one way to do it is to reference Microsoft.mshtml and read the source like: 回复较晚,但执行此操作的一种方法是引用Microsoft.mshtml并阅读源代码,例如:

var doc = yourBrowserControl.Document.DomDocument as IHTMLDocument2;
txtSource.Text = doc.body.innerHTML;

However, this method does not give you the complete source (for example, the HEAD section of the source is completely missing). 但是,此方法不能为您提供完整的源(例如,源的HEAD部分完全丢失)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM