在本文中,我们将带你了解c#–如何在WebBrowser控件中注入CSS?在这篇文章中,我们将为您详细介绍c#–如何在WebBrowser控件中注入CSS?的方方面面,并解答c#webbrowser控
在本文中,我们将带你了解c# – 如何在WebBrowser控件中注入CSS?在这篇文章中,我们将为您详细介绍c# – 如何在WebBrowser控件中注入CSS?的方方面面,并解答c#webbrowser控件的使用方法常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的.NET WebBrowser控件中的阻止对话框、asp.net – 如何以WPF格式捕获网页的按钮点击事件(在WebBrowser控件中打开)?、c# – 从WebBrowser控件中删除边框、c# – 从WebBrowser控件中加载的文档中获取标题。
本文目录一览:- c# – 如何在WebBrowser控件中注入CSS?(c#webbrowser控件的使用方法)
- .NET WebBrowser控件中的阻止对话框
- asp.net – 如何以WPF格式捕获网页的按钮点击事件(在WebBrowser控件中打开)?
- c# – 从WebBrowser控件中删除边框
- c# – 从WebBrowser控件中加载的文档中获取标题
c# – 如何在WebBrowser控件中注入CSS?(c#webbrowser控件的使用方法)
HtmlElement head = webbrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement scriptEl = webbrowser1.Document.CreateElement("script"); IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement; element.text = "function sayHello() { alert('hello') }"; head.AppendChild(scriptEl); webbrowser1.Document.InvokeScript("sayHello");
有没有更简单的方式将CSS注入DOM?
解决方法
<html> <head> <style type="text/css"> h1 {color:red} p {color:blue} </style> </head>
你可以尝试给:
HtmlElement head = webbrowser1.Document.GetElementsByTagName("head")[0]; HtmlElement styleEl = webbrowser1.Document.CreateElement("style"); IHTMLStyleElement element = (IHTMLStyleElement)styleEl.DomElement; IHTMLStyleSheetElement styleSheet = element.styleSheet; styleSheet.csstext = @"h1 { color: red }"; head.AppendChild(styleEl);
一路走来您可以在IHTMLStyleElement here上找到更多信息.
编辑
似乎答案比我原来想的要简单得多:
using mshtml; IHTMLDocument2 doc = (webbrowser1.Document.DomDocument) as IHTMLDocument2; // The first parameter is the url,the second is the index of the added style sheet. IHTMLStyleSheet ss = doc.createStyleSheet("",0); // Now that you have the style sheet you have a few options: // 1. You can just set the content as text. ss.csstext = @"h1 { color: blue; }"; // 2. You can add/remove style rules. int index = ss.addRule("h1","color: red;"); ss.removeRule(index); // You can even walk over the rules using "ss.rules" and modify them.
我写了一个小的测试项目,以验证这是否有效.我通过在MSDN上搜索IHTMLStyleSheet,在this page,this page和this one发生了这个最终结果.
.NET WebBrowser控件中的阻止对话框
我有一个.NET 2.0
WebBrowser控件,该控件用于在没有用户交互的情况下导航某些页面(不要问…长话短说)。由于此应用程序的用户较少,因此我将WebBrowser控件的ScriptErrorsSuppressed属性设置为true,VS
2005状态随附的文档将隐藏所有来自基础ActiveX控件的对话框,不只是脚本错误。”
但是,MSDN文章没有提到这一点。我设法取消了NewWindow事件,该事件可以防止弹出窗口,因此已经解决了这个问题。
任何人都有使用其中一种方法并成功阻止 所有 对话框,脚本错误等的经验吗?
编辑
这不是IE的独立实例,而是Windows Form应用程序上的WebBrowser控件的实例。任何人都具有使用此控件的经验,或者具有基础控件
AxSHDocVW的经验 ?
再次编辑
抱歉,我忘了提及这一点…我试图仅使用OK按钮来阻止 JavaScript alert()
。也许我可以转换为IHTMLDocument2对象并以这种方式访问脚本,我已经使用了MSHTML一点,有人知道吗?
asp.net – 如何以WPF格式捕获网页的按钮点击事件(在WebBrowser控件中打开)?
Web页面在Webbrowser Control中加载.该网页包含一个按钮.
该网页是ASP.NET应用程序.
我想将网页的按钮点击事件捕获到WPF表单(主持Webbrowser控件).有没有办法实现这个功能?
谢谢,
塔潘
解决方法
public partial class MainWindow : Window { /// <summary> /// This is a helper class. It appears that we can't mark the Window as ComVisible /// so instead,we'll use this seperate class to be the C# code that gets called. /// </summary> [ComVisible(true)] public class ComVisibleObjectForScripting { public void ButtonClicked() { //Do whatever you need to do. For Now,we'll just show a message Box MessageBox.Show("Button was clicked in web page"); } } public MainWindow() { InitializeComponent(); //Pass an instance of our helper class as the target object for scripting webbrowser1.ObjectForScripting = new ComVisibleObjectForScripting(); } private void Window_Loaded(object sender,RoutedEventArgs e) { //Navigate to your page somehow webbrowser1.Navigate("http://www.somewhere.com/"); } private void webbrowser1_LoadCompleted(object sender,NavigationEventArgs e) { //Once the document is loaded,we need to inject some custom JavaScript. //Here is the JavaScript var javascript = @" //This is the JavaScript method that will forward the click to the WPF app function htmlButtonClicked() { //Do any other procession...here we just always call to the WPF app window.external.ButtonClicked(); } //Find the button that you want to watch for clicks var searchButton = document.getElementById('theButton'); //Attach an onclick handler that executes our function searchButton.attachEvent('onclick',htmlButtonClicked); "; //Grab the current document and cast it to a type we can use //NOTE: This interface is defined in the MSHTML COM Component // You need to add a Reference to it in the Add References window var doc = (IHTMLDocument2)webbrowser1.Document; //Once we have the document,execute our JavaScript in it doc.parentwindow.execScript(javascript); } }
其中一些来自http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html
c# – 从WebBrowser控件中删除边框
查看屏幕截图中的红色区域:
Make it stop! http://img229.imageshack.us/img229/8342/badbadwebbrowser.gif
我希望Webbrowser看起来像蓝色区域 – 也就是说,填充Form并与Form的边缘齐平.
解决方法
谢天谢地,这是going away in IE9.
c# – 从WebBrowser控件中加载的文档中获取标题
请帮我用c#实现这个目标.
解决方法
XAML:
<Grid> <Webbrowser LoadCompleted="webbrowser1_LoadCompleted" Height="100" HorizontalAlignment="Left" Margin="73,72,0" Name="webbrowser1" VerticalAlignment="Top" Width="200" /> <Button Content="Go" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> </Grid>
码:
private void webbrowser1_LoadCompleted(object sender,NavigationEventArgs e) { dynamic doc = webbrowser1.Document; this.Title = doc.Title; } private void Button_Click(object sender,RoutedEventArgs e) { webbrowser1.Navigate("http://google.com"); }
没有动态,几乎没有任何异常处理:
private void webbrowser1_LoadCompleted(object sender,NavigationEventArgs e) { Object doc = webbrowser1.Document; this.Title = GetPropertyValue<string>(doc,"Title"); } private T GetPropertyValue<T>(object obj,string propertyName) { Type objectType = obj.GetType(); PropertyInfo propertyInfo = objectType.GetProperty(propertyName); Type propertyType = propertyInfo.PropertyType; if(propertyType == typeof(T)) { object propertyValue = (T)info.GetValue(obj,null); return value; } else { throw new Exception("Property " + propertyName + " is not of type " + T); } }
关于c# – 如何在WebBrowser控件中注入CSS?和c#webbrowser控件的使用方法的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.NET WebBrowser控件中的阻止对话框、asp.net – 如何以WPF格式捕获网页的按钮点击事件(在WebBrowser控件中打开)?、c# – 从WebBrowser控件中删除边框、c# – 从WebBrowser控件中加载的文档中获取标题的相关信息,请在本站寻找。
本文标签: