VS2010 C#创建ActiveX控件
1、File-->New-->Project,弹出 New Project窗口;
2、在左边的 Installed Templates -->Visual C#-->Windows中,选择Class Library,输入名字(name):HelloWorld,选择项目保存路径(Location):F:\donetProject, Solution: Create new solution ,如下图:
图 1
3、删除自动创建的Class1.cs文件,然后在HelloWorld上右键->添加->新建项,在弹出的窗口中选择“用
户控件”,名称为Demo,此时会出现一个类似于winform设计界面,我们在上面从工具箱中拖动一个Label在上面,并设定Label的Text为"HelloWorld"。
Add New Item:(Visual C# Items – User Control),在name中输入Demo
4、画ActiveX界面:
5、在菜单中:Project HelloWorld Properties,Application页面,点击Assembly Information
6、在HelloWorld(用户控件)项目上点击右键,选择属性,将打开项目属性面板,选择应用程序标签页,点击“程序集信息”按钮,在弹出的窗口中勾选“使程序集COM可见” (Make assembly COM-Visible) ;
7、切换到生成(Build)标签页,然后勾选“为Com互操作注册”(Register for COM interop),在该页面的最上面,有一个配置选项,切换到realse,并再次勾选“为Com互操作注册” (Register for COM interop)。这样无论是在debug还是在release状态下,都可以把用户控件当做com接口使用。如下图。(如果不使用realse模式,realse可以不设置。)
8、在目录(F:\donetProject\HelloWorld\HelloWorld\bin\Debug)中增加一个html文件:HelloWorld.html,内容如下:
<body bgcolor='#ABCABC'>
<object id="helloworld" classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE" Width="500" Height="500" >
<param name="UserText" value="Mycomputer">
</object>
<br>
<input type='button' οnclick='helloworld.ShowMessage("Hello World!")' value='Click'>
</body>
9、在 OLE/COM Object Viewer中,只需要在“OLE/COM对象查看器”中点Grouped by Component Category->.Net Category->点击你编写的类库名.用户控件类查看(HelloWorld.Demo),在该节点上右键,Copy CLSID to ClipBoard ;
复制CLSID,把 HelloWorld.html中classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE"修改为复制的CLSID;
10、点击运行(F5),即可弹出IE浏览器,进行调试;
11、C# 2010中ActiveX中读取Html中参数初始化,程序参数:
A、定义一个接口:AxMyControl
public interface AxMyControl {
String UserName { set; get; }
String UserPass { set; get; }
int UserId { set; get; }
}
B、Demo class实现AxMyControl接口,
Demo定义个属性
private string userName ;
private string userPass ;
private int userId ;
public int UserId{
get { return userId; }
set { userId = value; }
}
public string UserName{
get
{
return userName;
}
set
{
userName = value;
TB_Name.Text = value;
}
}
public string UserPass{
get
{
return userPass;
}
set
{
userPass = value;
TB_Pass.Text = value;
}
}
修改HelloWorld.html为:
<body bgcolor='#ABCABC'>
<object id="helloworld" classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE" Width="500" Height="500" >
<param name="userName" value="UserName"/>
<param name="userPass" value="UserPass"/>
<param name="userId" value="123" >
</object>
<br>
</body>
运行即可看到参数
12、在html中访问ActiveX中的方法:
在Demo class中增加方法:
public void ShowMessage(String value) {
MessageBox.Show(value);
}
修改HelloWorld.html为:
<body bgcolor='#ABCABC'>
<object id="helloworld" classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE" Width="500" Height="500" >
<param name="userName" value="UserName"/>
<param name="userPass" value="UserPass"/>
<param name="userId" value="123" >
</object>
<br>
<input type='button' οnclick='helloworld.ShowMessage("Hello World!")' value='Click'>
</body>
点击Click按钮即可弹出信息;
基本完成
完整代码如下:
Demo.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelloWorld
{
public interface AxMyControl {
String UserName { set; get; }
String UserPass { set; get; }
}
public partial class Demo : UserControl , AxMyControl
{
private string userName;
private string userPass;
private int userId;
public int UserId
{
get { return userId; }
set { userId = value; }
}
public Demo()
{
InitializeComponent();
}
public string UserName
{
get
{
return userName;
}
set
{
userName = value;
TB_Name.Text = value;
}
}
public string UserPass
{
get{
return userPass;
}
set{
userPass = value;
TB_Pass.Text = value;
}
}
public void ShowMessage(String value) {
MessageBox.Show(value);
}
private void B_Ok_Click(object sender, EventArgs e)
{
MessageBox.Show(UserId + "--" + UserName);
}
///<ê?summary>ê?
///清?理¤¨ª所¨´有®D正y在¨²使º1用®?的Ì?资Á¨º源¡ä。¡ê
///<ê?/summary>ê?
public override void Dispose(bool disposing) {
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
}
}
AssemblyInfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security ;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HelloWorld")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ABC")]
[assembly: AssemblyProduct("HelloWorld")]
[assembly: AssemblyCopyright("Copyright © ABC? 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8cc6c007-781e-4672-a79d-7ceb2bd678bb")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
//设¦¨¨置?控?件t的Ì?权¨¡§限T
[assembly: AllowPartiallyTrustedCallers]
Demo.Designer.cs(此源码有C#自动生成,自己在Form上画界面,自动生成)
namespace HelloWorld
{
partial class Demo
{
///<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 Component 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.L_Str = new System.Windows.Forms.Label();
this.L_Name = new System.Windows.Forms.Label();
this.L_Pass = new System.Windows.Forms.Label();
this.TB_Name = new System.Windows.Forms.TextBox();
this.TB_Pass = new System.Windows.Forms.TextBox();
this.B_Ok = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// L_Str
//
this.L_Str.AutoSize = true;
this.L_Str.Font = new System.Drawing.Font("KaiTi_GB2312", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.L_Str.Location = new System.Drawing.Point(153, 25);
this.L_Str.Name = "L_Str";
this.L_Str.Size = new System.Drawing.Size(189, 29);
this.L_Str.TabIndex = 0;
this.L_Str.Text = "Hello World";
//
// L_Name
//
this.L_Name.AutoSize = true;
this.L_Name.Location = new System.Drawing.Point(82, 92);
this.L_Name.Name = "L_Name";
this.L_Name.Size = new System.Drawing.Size(53, 12);
this.L_Name.TabIndex = 1;
this.L_Name.Text = "用户名:";
//
// L_Pass
//
this.L_Pass.AutoSize = true;
this.L_Pass.Location = new System.Drawing.Point(82, 124);
this.L_Pass.Name = "L_Pass";
this.L_Pass.Size = new System.Drawing.Size(53, 12);
this.L_Pass.TabIndex = 2;
this.L_Pass.Text = "密 码:";
//
// TB_Name
//
this.TB_Name.Location = new System.Drawing.Point(152, 83);
this.TB_Name.Name = "TB_Name";
this.TB_Name.Size = new System.Drawing.Size(190, 21);
this.TB_Name.TabIndex = 3;
//
// TB_Pass
//
this.TB_Pass.Location = new System.Drawing.Point(152, 115);
this.TB_Pass.Name = "TB_Pass";
this.TB_Pass.Size = new System.Drawing.Size(190, 21);
this.TB_Pass.TabIndex = 4;
//
// B_Ok
//
this.B_Ok.Location = new System.Drawing.Point(345, 183);
this.B_Ok.Name = "B_Ok";
this.B_Ok.Size = new System.Drawing.Size(90, 29);
this.B_Ok.TabIndex = 5;
this.B_Ok.Text = "确认";
this.B_Ok.UseVisualStyleBackColor = true;
this.B_Ok.Click += new System.EventHandler(this.B_Ok_Click);
//
// Demo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.B_Ok);
this.Controls.Add(this.TB_Pass);
this.Controls.Add(this.TB_Name);
this.Controls.Add(this.L_Pass);
this.Controls.Add(this.L_Name);
this.Controls.Add(this.L_Str);
this.Name = "Demo";
this.Size = new System.Drawing.Size(521, 276);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label L_Str;
private System.Windows.Forms.Label L_Name;
private System.Windows.Forms.Label L_Pass;
private System.Windows.Forms.TextBox TB_Name;
private System.Windows.Forms.TextBox TB_Pass;
private System.Windows.Forms.Button B_Ok;
}
}
HelloWorld.html 内容如下:
<body bgcolor='#ABCABC'>
<object id="helloworld" classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE" Width="500" Height="500" >
<param name="userName" value="UserName"/>
<param name="userPass" value="UserPass"/>
<param name="userId" value="123" >
</object>
<br>
<input type='button' οnclick='helloworld.ShowMessage("Hello World!")' value='Click'>
</body>
在最后一个加载的param中做初始化事情,比如
public string UserPass {
get{ return userPass; }
set{ userPass = value;
TB_Pass.Text = value;
这里做初始化事情 ;需要运行一次,看看那个set方法是最后加载的,以防止之前的参数没加载,而会使用它的情况;
}
}