環境說明:Windows2003,VS2005
簡介:控制項用C#開發,控制項中有一個按鈕和一個文字框,單擊控制項上的按鈕則清空文字框裡的內容; 控制項測試程式用VC開發,為MFC對話方塊程式,對話方塊上有一個“發送按鈕”、“接收按鈕”、“文字框”和“C#開發的控制項”; 單擊對話方塊上的“發送按鈕”,則將對話方塊上“文字框”中的內容發送到“C#開發的控制項”的文字框中,單擊對話方塊上的“接收按鈕”,則將“C#開發的控制項”的文字框中的內容接收到對話方塊上的“文字框”中。
第一步:建立“windows控制項工程”MyControl第二步:修改項目配置資訊 1、修改項目設定檔:AssemblyInfo.cs [assembly: AssemblyDescription("MyControl & com")] [assembly: ComVisible(true)]
2、修改項目屬性: 選中項目“MyControl”\按右鍵\選擇屬性\選擇“產生”\選中為Com Interop註冊
3、將控制項名稱改為:MyTestControl 在檔案MyTestControl.cs中添加如下代碼:using System.Runtime.InteropServices;
第三步:建立介面:IMyControl
[Guid("6B576723-028A-4b1e-81DB-295E0159C8AF")]
public interface IMyControl
{
string MyText
{
get;
set;
}
}
第四步:修改控制項定義 將 public partial class MyTestControl : UserControl改為: [Guid("FDF49DB3-54C2-4b4f-963C-349B5D68E483")]
public partial class MyTestControl : UserControl, IMyControl第五步:向控制項添加一個按鈕(button1)和一個文字框(textBox1) 並向按鈕添加單擊事件:
public void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
第六步:向MyTestControl類中添加如下代碼: public string MyText
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}