我觉得可能在执行“doc2.body.innerHTML = this.htmlstring;”产生了异常,后面的代码没有执行。
解决方案 »
- C# 如何 改变系统鼠标样式!
- 不支持关键字instance
- c#字符串处理
- ASP.NET问题,网页中有两个按键,按了回车以总是提交了我不想按的那个按键,请问如何才能将按键锁定?
- 怎样在控制台中嵌入图片
- datagridview编辑后自动保存数据到数据库里,怎么实现
- 请问如何用C#写一个程序,让其自动检测一个IE窗口,当其打开完毕后,可以自动填写某些txtbox项目和Radiobutton?
- 关于连接数据库的错误,不明白???
- 想办一个网站,同时申请一个国际域名,不知道哪里的虚拟空间好?
- 如何实现实例化一个collection????
- 关于在用AxWebBrowser开发的Winform的浏览器中怎么样使网页不使用缓存??(50分求解!急急急)
- 关于TreeView的问题
iEvent = (mshtml.HTMLDocumentEvents2_Event) doc2;
iEvent.oncontextmenu += new mshtml.HTMLDocumentEvents2_oncontextmenuEventHandler(ContextMenuEventHandler);iEvent.onselectstart += new mshtml.HTMLDocumentEvents2_onselectstartEventHandler(SelectStartEventHandler);....private bool ContextMenuEventHandler(mshtml.IHTMLEventObj e)
{
return isNoCancelcontextmenu;
}
private bool SelectStartEventHandler(mshtml.IHTMLEventObj e)
{
return isNoCancelSelect;
}???
如果这条语句发生异常,那么下面的语句就不会执行了,即使执行了结果也是错误的
是我搞错了,但是有了新的问题,看如下程序:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WebTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private AxSHDocVw.AxWebBrowser browser;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; private bool isNoCancelcontextmenu = true;
private bool isNoCancelSelect = true;
private mshtml.IHTMLDocument2 doc2;
private mshtml.IHTMLWindow2 win2; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.browser = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.browser)).BeginInit();
this.SuspendLayout();
//
// button4
//
this.button4.Location = new System.Drawing.Point(32, 272);
this.button4.Name = "button4";
this.button4.TabIndex = 9;
this.button4.Text = "不屏蔽";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(352, 272);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 23);
this.button3.TabIndex = 8;
this.button3.Text = "屏蔽选择和右键";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(248, 272);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "屏蔽右键";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 272);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "屏蔽选择";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// browser
//
this.browser.Enabled = true;
this.browser.Location = new System.Drawing.Point(16, 16);
this.browser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("browser.OcxState")));
this.browser.Size = new System.Drawing.Size(472, 240);
this.browser.TabIndex = 10;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 349);
this.Controls.Add(this.browser);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.browser)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
browser.NavigateComplete2+=new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(browser_NavigateComplete2); object url= "about:blank";
object r=System.Reflection.Missing.Value; browser.Navigate2(ref url,ref r,ref r,ref r,ref r);
} private void browser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
browser.NavigateComplete2-=new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(browser_NavigateComplete2); doc2=(mshtml.IHTMLDocument2)browser.Document; string htmlString = "<BODY text=#000000 bgColor=#ffffff><FONT face=宋体>";
htmlString = htmlString + "<P>本主题包括 Internet 信息服务 5.0 版提供的五个主要的安全元素的概述</P>";
htmlString = htmlString + "<A href='http://www.csdn.net'>中国软件网</A></BODY>";
doc2.body.innerHTML = htmlString; win2=doc2.parentWindow; win2.execScript("function document.oncontextmenu(){return event.returnValue=" + this.isNoCancelcontextmenu.ToString().ToLower() + ";}","JScript");
win2.execScript("function document.onselectstart(){return " + this.isNoCancelSelect.ToString().ToLower() + ";}","JScript");
mshtml.HTMLDocumentEvents2_Event Event = (mshtml.HTMLDocumentEvents2_Event)browser.Document; //执行此句后就将导致浏览器中的文档的所有事件都丢失了,好象全部都屏蔽了
Event.onclick +=new mshtml.HTMLDocumentEvents2_onclickEventHandler( Event_onclick );
} private bool Event_onclick( mshtml.IHTMLEventObj p)
{
//自己写的代码 return ( true );
} private void button1_Click(object sender, System.EventArgs e)
{
isNoCancelcontextmenu = true;
isNoCancelSelect = false; Navigate();
} private void button4_Click(object sender, System.EventArgs e)
{
isNoCancelcontextmenu = true;
isNoCancelSelect = true; Navigate();
} private void button3_Click(object sender, System.EventArgs e)
{
isNoCancelcontextmenu = false;
isNoCancelSelect = false; Navigate();
} private void button2_Click(object sender, System.EventArgs e)
{
isNoCancelcontextmenu = false;
isNoCancelSelect = true; Navigate();
} private void Navigate()
{
browser.NavigateComplete2+=new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(browser_NavigateComplete2); object url = "about:blank";
object r=System.Reflection.Missing.Value; browser.Navigate2(ref url,ref r,ref r,ref r,ref r);
}
}
}
以上是我的测试源程序,发现:
只要执行这句
Event.onclick +=new mshtml.HTMLDocumentEvents2_onclickEventHandler( Event_onclick ); 就将导致浏览器中的文档的所有事件都丢失了,好象全部都屏蔽了不明白原因,请高手指教,解决另送100分!