当前正在尝试使用IHTMLStyleSheet在WebBrowser控件中插入CSS
How to inject CSS in WebBrowser control?
我读了这本书,我觉得它有点帮助,但它似乎对我没用。
IHTMLDocument2 doc = (webBrowser1.Document.DomDocument) as IHTMLDocument2;
IHTMLStyleSheet ss = doc.createStyleSheet("", 0);
ss.addRule("body", "background-color:#000");
ss.cssText = @"h1 { color: blue; }";
这就是我现在所拥有的,我需要在这之后将它添加到控件中吗,或者我在这里做错了什么?
编辑:
成功了我就是这么做的
CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
styleSheet = CurrentDocument.createStyleSheet("", 0);
StreamReader streamReader = new StreamReader(@"test.css"); //test.css is Stylesheet file
string text = streamReader.ReadToEnd();
streamReader.Close();
styleSheet.cssText = text;
最佳答案
我做了什么让它工作
CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
styleSheet = CurrentDocument.createStyleSheet("", 0);
StreamReader streamReader = new StreamReader(@"test.css"); //test.css is Stylesheet file
string text = streamReader.ReadToEnd();
streamReader.Close();
styleSheet.cssText = text;