在 Windows 窗体上承载 WebBrowser ActiveX 控件的问题

nsi20141 2003-06-09 07:29:32
下边这个程序是Windows 窗体快速入门教程的例子,是关于在Windows 窗体上承载 ActiveX 控件的问题。

在.NET下编译总是提示:找不到类型或命名空间名称“AxSHDocVw”(是否缺少 using 指令或程序集引用?)我已经使用包含在 SDK 中的 Aximp.exe 工具创建 ActiveX 控件包装了 aximp c:\winnt\system32\shdocvw.dll
生成了AxSHDocVw.dll和SHDocVw.dll 应该把这两个文件放在什么位置才可以解决问题呢?

请看过这个例子的,帮帮忙



代码如下:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.AxHost;
using AxSHDocVw;


namespace WindowsApplication5
{
/// <summary>
/// document 的摘要说明。
/// </summary>
public class Document : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private AxSHDocVw.AxWebBrowser AxWebBrowser1;
private System.Windows.Forms.Button buttonGo;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel1;

public Document() : base()
{

// Windows 窗体设计器所必需的
InitializeComponent();

this.Text = "新 Web 浏览器";

//处理“标题更改”事件以便可以更新父级中的标题
//和状态栏
AxWebBrowser1.TitleChange += new DWebBrowserEvents2_TitleChangeEventHandler(this.AxWebBrowser1_TitleChanged);

//创建浏览器控件后,可以与其交互,
//以便挂钩创建事件的句柄并使用 this 导航
//到起始页
AxWebBrowser1.HandleCreated += new EventHandler(this.AxWebBrowser1_Created);

//设置最小窗体大小
this.MinimumSize = new Size(100, 100);
}


//已创建了 WebBrowser,所以完成了初始化
public void AxWebBrowser1_Created(object sender, EventArgs evArgs)
{

//导航到起始页
buttonGo_Click(buttonGo, EventArgs.Empty);

//以后不再需要此事件处理程序,所以移除它
AxWebBrowser1.HandleCreated -= new EventHandler(this.AxWebBrowser1_Created);
}


//处理标题更改后事件
private void AxWebBrowser1_TitleChanged(object sender, DWebBrowserEvents2_TitleChangeEvent e)
{
this.Text = e.text;
((MainForm)(this.MdiParent)).statusBar1.Text=this.Text;
}


//处理 go 按钮的单击
private void buttonGo_Click(object sender, EventArgs evArgs)
{

Cursor currentCursor = Cursor.Current;
try
{
Cursor.Current = Cursors.WaitCursor;

object arg1 = 0; object arg2 = ""; object arg3 = ""; object arg4 = "";
if (textBox1.Text == null)
{
Console.WriteLine("textBox1.Text = null");
textBox1.Text = "";
}
AxWebBrowser1.Navigate(textBox1.Text,ref arg1,ref arg2, ref arg3, ref arg4);

}
finally
{
Cursor.Current = currentCursor ;
}
}


protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}


private void InitializeComponent()
{

this.components = new System.ComponentModel.Container();
this.AxWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.buttonGo = new System.Windows.Forms.Button();

AxWebBrowser1.BeginInit();
AxWebBrowser1.Size = new System.Drawing.Size(292, 273);
AxWebBrowser1.TabIndex = 3;
AxWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;

textBox1.Text = "http://localhost/quickstart/winforms/samples/axhosting/WinForms.html";
textBox1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
textBox1.TabIndex = 1;
textBox1.Size = new System.Drawing.Size(450, 20);
textBox1.Location = new System.Drawing.Point(56, 2);
textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;


panel1.Dock = System.Windows.Forms.DockStyle.Top;
panel1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
panel1.Size = new System.Drawing.Size(600, 24);
panel1.TabIndex = 0;
panel1.Text = "panel1";

label1.Size = new System.Drawing.Size(64, 16);
label1.Anchor = System.Windows.Forms.AnchorStyles.Left;
label1.TabIndex = 0;
label1.Text = "地址:";
label1.Location = new System.Drawing.Point(0, 4);

buttonGo.Anchor = System.Windows.Forms.AnchorStyles.Right;
buttonGo.Size = new System.Drawing.Size(88, 24);
buttonGo.TabIndex = 2;
buttonGo.Text = "进行(&G)";
buttonGo.Location = new System.Drawing.Point(510, 0);
buttonGo.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
buttonGo.Click += new System.EventHandler(buttonGo_Click);

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.Text = "";
this.ClientSize = new System.Drawing.Size(650, 480);
this.AcceptButton = buttonGo;

this.Controls.Add(AxWebBrowser1);
this.Controls.Add(panel1);
panel1.Controls.Add(textBox1);
panel1.Controls.Add(buttonGo);
panel1.Controls.Add(label1);

AxWebBrowser1.EndInit();

}


}
}
...全文
141 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nsi20141 2003-06-09
  • 打赏
  • 举报
回复
thank you
phoenix96_2000 2003-06-09
  • 打赏
  • 举报
回复
在Solution Explorer的工程选择添加引用,选择你的控件的路径就可以了

如果已经安装了控件,则可以在工具箱右键->自定义工具箱,那么可以在COM Library 里可以看到控件,加入就可以拖到窗体上直接使用了

110,534

社区成员

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

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

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