VS2010 C#創(chuàng)建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、刪除自動創(chuàng)建的Class1.cs文件,,然后在HelloWorld上右鍵->添加->新建項,在彈出的窗口中選擇“用
戶控件”,,名稱為Demo,,此時會出現(xiàn)一個類似于winform設(shè)計界面,我們在上面從工具箱中拖動一個Label在上面,,并設(shè)定Label的Text為"HelloWorld",。
Add New Item:(Visual C# Items – User Control),在name中輸入Demo
4,、畫ActiveX界面:
5、在菜單中:Project HelloWorld Properties,,Application頁面,,點擊Assembly
Information
6、在HelloWorld(用戶控件)項目上點擊右鍵,,選擇屬性,,將打開項目屬性面板,選擇應(yīng)用程序標(biāo)簽頁,,點擊“程序集信息”按鈕,,在彈出的窗口中勾選“使程序集COM可見”
(Make assembly COM-Visible) ;
7、切換到生成(Build)標(biāo)簽頁,然后勾選“為Com互操作注冊”(Register
for COM interop),,在該頁面的最上面,,有一個配置選項,切換到realse,,并再次勾選“為Com互操作注冊”
(Register for COM interop),。這樣無論是在debug還是在release狀態(tài)下,都可以把用戶控件當(dāng)做com接口使用,。如下圖,。(如果不使用realse模式,realse可以不設(shè)置,。)
8,、在目錄(F:\donetProject\HelloWorld\HelloWorld\bin\Debug)中增加一個html文件:HelloWorld.html,內(nèi)容如下:
- <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' onclick='helloworld.ShowMessage("Hello World!")' value='Click'>
-
- </body>
9、在 OLE/COM Object Viewer中,只需要在“OLE/COM對象查看器”中點Grouped
by Component Category->.Net Category->點擊你編寫的類庫名.用戶控件類查看(HelloWorld.Demo),在該節(jié)點上右鍵,,Copy
CLSID to ClipBoard ;
復(fù)制CLSID,,把 HelloWorld.html中classid="clsid:7004C725-9039-3FD7-9E55-9399F64072CE"修改為復(fù)制的CLSID;
10、點擊運(yùn)行(F5),即可彈出IE瀏覽器,,進(jìn)行調(diào)試,;
11,、C# 2010中ActiveX中讀取Html中參數(shù)初始化,,程序參數(shù):
A、定義一個接口:AxMyControl
- public interface AxMyControl {
- String UserName { set; get; }
- String UserPass { set; get; }
- int UserId { set; get; }
- }
B,、Demo class實現(xiàn)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>
-
-
運(yùn)行即可看到參數(shù)
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' onclick='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);
- }
-
-
-
-
-
-
-
-
- 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 ;
-
-
-
-
-
- [assembly: AssemblyTitle("HelloWorld")]
- [assembly: AssemblyDescription("")]
- [assembly: AssemblyConfiguration("")]
- [assembly: AssemblyCompany("ABC")]
- [assembly: AssemblyProduct("HelloWorld")]
- [assembly: AssemblyCopyright("Copyright ? ABC? 2012")]
- [assembly: AssemblyTrademark("")]
- [assembly: AssemblyCulture("")]
-
-
-
-
-
- [assembly: ComVisible(true)]
-
-
- [assembly: Guid("8cc6c007-781e-4672-a79d-7ceb2bd678bb")]
-
-
-
-
-
-
-
-
-
-
-
-
- [assembly: AssemblyVersion("1.0.0.0")]
- [assembly: AssemblyFileVersion("1.0.0.0")]
-
-
-
- [assembly: AllowPartiallyTrustedCallers]
-
-
-
- Demo.Designer.cs(此源碼有C#自動生成,,自己在Form上畫界面,自動生成)
- namespace HelloWorld
- {
- partial class Demo
- {
-
-
-
- private System.ComponentModel.IContainer components = null;
-
-
-
-
-
-
- protected override void Dispose(bool disposing)
-
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
-
- base.Dispose(disposing);
-
- }
-
- #region Component Designer generated code
-
-
-
-
-
-
- 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();
-
-
-
-
- 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";
-
-
-
- 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 = "用戶名:";
-
-
-
- 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 = "密 碼:";
-
-
-
- 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;
-
-
-
- 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;
-
-
-
- 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 = "確認(rèn)";
- this.B_Ok.UseVisualStyleBackColor = true;
- this.B_Ok.Click += new System.EventHandler(this.B_Ok_Click);
-
-
-
- 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 內(nèi)容如下:
- <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' onclick='helloworld.ShowMessage("Hello World!")' value='Click'>
-
- </body>
在最后一個加載的param中做初始化事情,,比如
public string UserPass {
get{ return userPass; }
set{ userPass = value;
TB_Pass.Text = value;
這里做初始化事情 ,;需要運(yùn)行一次,看看那個set方法是最后加載的,,以防止之前的參數(shù)沒加載,,而會使用它的情況;
}
}