Add CSS file to WebBrowser control in Winform/Wpf

For adding an css file to browser in Winform/Wpf ,

First->       Add "microsoft HTML object library (MSHTML).dll" to
                   "reference" of your project.

Second->   Add "using mshtml" & "using System.IO" namespaces to code file.

Third->     Add/Create a CSS file that want to add in webbrowser to customize
                  HTML code/visual.

Final->     Add/Write  following codes under windows_load event to enable
                   css to webbrowser.
 Code Follows :-
mshtml.HTMLDocument CurrentDocument = (mshtml.HTMLDocument)WebBrowser1.Document.DomDocument;
                mshtml.IHTMLStyleSheet styleSheet = CurrentDocument.createStyleSheet("", 0);
                StreamReader streamReader = new StreamReader(@"..\browser.css"); //browser.css is Stylesheet file
                string text = streamReader.ReadToEnd();
                streamReader.Close();
                styleSheet.cssText = text;
    


2 comments: (+add yours?)

Anonymous said...

unknown object DomDocument

Anonymous said...

Yes worked for me..Thank you :)

Post a Comment