使用C#編寫可被案頭應用調用的ActiveX控制項

來源:互聯網
上載者:User
 

環境說明: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;
            }
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.