webrowser load from file

來源:互聯網
上載者:User
Using the Web Browser Control in your C# Applications 

It can be a powerful thing to display dynamic HTML in your C# applications. It can give your applications a modern look and feel and can make displaying data in non-standard ways easy with some simple markup. We have the web browser ActiveX control that wraps up what we know as Internet Explorer. While I don't want to get into the in's and out's of using the web browser in your applications, I do want to demonstrate a few things that will make the use of the web browser easier so you can integrate it seamlessly into your applications.

Some common requests I see in regards to using the web browser control is the ability to set the HTML to display in the browser without writing it to a file and navigating to it, printing the contents of the browser, and other items related to manipulating it's contents. Let's take a quick look at those.

Setting the HTML without writing to a file

Many times when the web browser control is used, the HTML content that the developer wishes to display will be written to a text/html file and then Navigate is used to navigate to the file, such as the following:

 

object empty = System.Reflection.Missing.Value;axWebBrowser1.Navigate(@"c:/myfile.htm", ref empty, ref empty, ref empty, ref empty);

 

The problem with that is, of course, that you need to write to a file. This brings in other possible issues such as ensuring you write the file to a location where the user has permissions to do so. A better route is to not write to a file at all. Instead, set the HTML directly in the browser. To accomplish this we need to get a reference to the browser's internal HTML document and manipulate it using the Document Object Model. We need to add a reference to the “Microsoft HTML Object Library” (mshtml) to get to the interfaces & classes that we will use to get to the document's DOM. Once you add the reference, you can get to the document and do whatever you want. However, in order to get to the document, you will have to navigate to about:blank before it can be accessed. If you do not first load or navigate to somethingthen the document will be null. So first of all let's load about:blank to create a blank document.

 

object empty = System.Reflection.Missing.Value;axWebBrowser1.Navigate("about:blank", ref empty, ref empty, ref empty, ref empty);

 

Now that we've done that we can get a reference to the document's DOM via the IHTMLDocument interface and write to it.

 

// create an IHTMLDocument2mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as mshtml.IHTMLDocument2;// write to the docdoc.clear();doc.writeln("This is my text...");doc.close();

 

Not too bad. We can write whatever markup we want to the document such as HTML, links, CSS, or even JavaScript.

Printing the Browser content

With a reference to the document, we can do whatever we want with it. That would include methods in the DOM such as execCommand. We can use that to print the browser's HTML.

 

mshtml.IHTMLDocument2 doc = axWebBrowser1.Document as mshtml.IHTMLDocument2;doc.execCommand("Print", true, 0);

 

Printing is only the beginning of what you can do here. Take a look at all the possible commands you can send via execCommand here. It really opens up what you can get to when you look at what you have available. I'll outline in another post how to disable the right-click menu of the browser (by default you'll get the standard IE right-click menu) as well as how to take this a step further in integrating the power of C# with dynamic HTML in your applications.

 

Download sample project using the browser

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.