.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
WebBroswer In C#

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

If you want to write into webbrowser in windows form in C#, you have to make reference MSHTML library and use its interface to do so. The trick is, if you did this in ASP or Visual Basic, you can simply put

wb.document.write(sHtml)

where wb is WebBrowser a object and sHtml is a string.

In .Net it works differently. You can create sample project in VB.6 and load it into VB.NET.
It would complains about bad type variable, but will compile and run (surprisingly, if just create such a VB.NET project from the scratch, it will complain and not compile!)

The following approach shows how to make that functionality with the right types.

Put into C# windows form
1. text box txt1 (System.Windows.Forms.TextBox)
2. Web Browser WBresult (AxSHDocVw.AxWebBrowser)
3. button to run the code
4. make reference to MSHTML library
5. add two functions: fillWeb (which does the real job) and redirectWeb(redirects to url, file, or clears)
6. on button click event function put fillWeb("top linebold

another line

");
7. run the project and click on the button (if you have any errors, they would appear in text box)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using mshtml;

// ...
// here is the main functionality for web control to get/fill/redirect web
content:

      private string getWeb()
      {
         string s = "";
         try
         {
            object boxDoc = axWebBrowser1.Document;
            IHTMLDocument2 hDoc = (mshtml.IHTMLDocument2)boxDoc;
            s += hDoc.body.innerText; // or innerHtml
            return s;
         }
         catch(Exception exc)
         {
            s += exc.Message;
            return s;
         }
         finally
         {
         }
      }

      private void fillWeb(string sHTML)
      {
         redirectWeb("about:blank");
         try
         {
            object[] psa = {sHTML};//could be:{"bold",
"<i>italic</i>"};
            IHTMLDocument2 hDoc2 = (IHTMLDocument2)axWebBrowser1.Document;
            hDoc2.write(psa);

         }
         catch(Exception exc)
         {
            txt1.Text = exc.Message;
         }
      }

      private void redirectWeb(string sUrl)
      {
         try
         {
            axWebBrowser1.Visible = true;
            axWebBrowser1.BringToFront();
            //   System.Object nullObject = 0;
            //   System.Object nullObjStr = "";
            //   axWebBrowser1.Navigate(sUrl, ref nullObject, ref
nullObjStr, ref nullObjStr, ref nullObjStr);
            Object n = null;
            axWebBrowser1.Navigate(sUrl, ref n, ref n, ref n, ref n);
         }
         catch(Exception exc)
         {
            txt1.Text = exc.Message;
         }
      }



相关文章

相关软件