C# winForm webbrowser控件 怎么既能显示网页页面,同时可以切换到Html源码

ladenglaola 2011-11-26 03:53:09
例如:页面上显示字符


请求高人指点,谢谢!


切换到HTML源码显示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.6148" name=GENERATOR></HEAD>
<BODY>
<P><FONT style="BACKGROUND-COLOR: #cce8cf">请求高人指点,谢谢!</FONT></P></BODY></HTML>


希望大家帮忙?分不多,先给20,等有分了不到100分,等待中。。。。。。。。
...全文
585 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycm_12345 2011-12-06
  • 打赏
  • 举报
回复
webBrowser1.Document.Body.InnerHtml=txtbox1
ladenglaola 2011-12-05
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 denqh 的回复:]

html源码无法显示在webbrowser控件中。
[/Quote]

谢谢你,那只能利用其它方法了
BDS 2011-12-03
  • 打赏
  • 举报
回复
为什么非得显示到webbrowser里面。。。
如果你非要这么干,自己重写一下webbrowser控件吧
不过估计你现在也搞不定
还是别弄这么纠结了。
DENQH 2011-12-03
  • 打赏
  • 举报
回复
html源码无法显示在webbrowser控件中。
ladenglaola 2011-12-03
  • 打赏
  • 举报
回复
各位大侠帮忙啊
cf_nxcxy 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zhangsanlaola 的回复:]
引用 7 楼 cf_nxcxy 的回复:

界面:
C# code
namespace RanIP
{
partial class qiehuan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer c……



……
[/Quote]
这个。。。。。。。。。。。。
ladenglaola 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zincy 的回复:]

有答案了
[/Quote]


有答案了告诉我
ladenglaola 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cf_nxcxy 的回复:]

界面:
C# code
namespace RanIP
{
partial class qiehuan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer c……
[/Quote]


你好,你的这些代码很好,对我帮助很大,不过这是显示在textbox中,我能不能直接把html源码显示在webbrowser控件中呢?还有如果我把 html源码编辑后,相应的web页面也可以变化,就像asp.net 里的“设计和源码”切换视图的效果,大侠你帮我想想,感激不尽,分数更是小事,呵呵
ladenglaola 2011-11-30
  • 打赏
  • 举报
回复
你好,你的这些代码很好,对我帮助很大,不过这是显示在textbox中,我能不能直接把html源码显示在webbrowser控件中呢?还有如果我把html源码编辑后,相应的web页面也可以变化,就像asp.net 里的“设计和源码”切换视图的效果,大侠你帮我想想,感激不尽,分数更是小事,呵呵
cf_nxcxy 2011-11-29
  • 打赏
  • 举报
回复
界面:
namespace RanIP
{
partial class qiehuan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "查看代码";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(12, 42);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(531, 301);
this.webBrowser1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 40);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(531, 302);
this.textBox1.TabIndex = 2;
//
// qiehuan
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(555, 355);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.button1);
this.Name = "qiehuan";
this.Text = "qiehuan";
this.Load += new System.EventHandler(this.qiehuan_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.TextBox textBox1;
}
}


cs文件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;

namespace RanIP
{
public partial class qiehuan : Form
{
public qiehuan()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Visible = true;
this.textBox1.Text = getHtml("http://www.baidu.com", "");
}

private void qiehuan_Load(object sender, EventArgs e)
{
this.textBox1.Visible = false;
this.webBrowser1.Navigate("http://www.baidu.com");

}

public string getHtml(string url, string charSet)//url是要访问的网站地址,charSet是目标网页的编码,如果传入的是null或者"",那就自动分析网页的编码
{
WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
// 需要注意的:
//有的网页可能下不下来,有种种原因比如需要cookie,编码问题等等
//这是就要具体问题具体分析比如在头部加入cookie
// webclient.Headers.Add("Cookie", cookie);
//这样可能需要一些重载方法。根据需要写就可以了

//获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//如果服务器要验证用户名,密码
//NetworkCredential mycred = new NetworkCredential(struser, strpassword);
//myWebClient.Credentials = mycred;
//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);

//获取网页字符编码描述信息
Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[2].Value;
if (charSet == null || charSet == "")
charSet = webCharSet;

if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
return strWebData;
}



}
}

zincy 2011-11-29
  • 打赏
  • 举报
回复
有答案了
tangjia281323 2011-11-29
  • 打赏
  • 举报
回复
URL = URL + "?UserName=" + frmMain.UserName + "&AgentID=" + frmMain.AgentID;
webBrowser1.Url = new Uri(URL);
URL是自定义路径。至于怎么显示html代码。你看下能不能把html代码都转换成一个字符串。串起来之后显示到控件里面。。
cf_nxcxy 2011-11-29
  • 打赏
  • 举报
回复
帮你做一个
稍等
ladenglaola 2011-11-29
  • 打赏
  • 举报
回复
没人帮忙吗 跪求!!!
ladenglaola 2011-11-28
  • 打赏
  • 举报
回复
朋友,能说详细点吗?

另外:主帖最后一句“希望大家帮忙?分不多,先给20,等有分了不到100分,等待中。。。。。。。。”,写错了,是这样“希望大家帮忙?分不多,先给20,等有分了补到100分,等待中。。。。。。。。”,不好意思!
bdmh 2011-11-26
  • 打赏
  • 举报
回复
自己封装一个,两个页签,一个显示网页,一个读取源码,思维开拓点,非得一根筋和webborwser较劲

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧