• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++调用C#之C#COM控件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#做界面真的是比C++方便多了,所以尝试了一下,使用C++做核心功能(例如绘图),然后用C#来做节目(例如对话框),考虑到以后可能不能使用.net,使用DLL做一个隔离层,隔离C++和C#,方便以后可以使用不同的语言开发。

C#部分:

1. 新建工程

 

2.  修改工程属性

 

3. 代码部分

ShowDialog.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace NetActiveX
{
    //  事件接口
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IEvent
    {
        [DispId(20)]
        void NotifyEvent(string s);
    }
    //  显示对话框接口
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IShowDialog
    {
        void ShowDialog(string dlgType, string s);
    }
    //  显示对话框实现类
    [ComDefaultInterface(typeof(IShowDialog)),  
    ComSourceInterfaces(typeof(IEvent)),  
    ComVisible(true)]
    public class ShowDialogImpl : IShowDialog
    {
        public void ShowDialog(string dlgType, string s)
        {
            switch (dlgType)
            {
                case "EmployeeDialog":
                    ShowEmployeeDialog(s);
                    break;

                default:
                    break;
            }
        }
        //  定义委托和事件
        public delegate void EventDelegate(string s);
        public event EventDelegate NotifyEvent;
        //  对话框应用确定通知
        private void DialogNotify(Form from)
        {
            Type type = from.GetType();

            switch (type.Name)
            {
                case "EmployeeDialog":
                    {
                        EmployeeDialog dlg = (EmployeeDialog)from;
                        EmployeeData employee = dlg.GetEmployee();
                        s = employee.ToString();
                        NotifyEvent(s);
                    }
                    break;
                    
                default:
                    break;
            }
        }
        //  显示员工对话框
        private void ShowEmployeeDialog(string s)
        {
            EmployeeDialog dlg = new EmployeeDialog();
            EmployeeData employee = new EmployeeData();

            employee.ParseString(s);
            dlg.SetEmployee(employee);
            dlg.SetNotify(DialogNotify);
            dlg.ShowDialog();
        }
    }
}

EmployeeDialog.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NetActiveX
{
    public partial class EmployeeDialog : Form
    {
        public delegate void Notify(EmployeeDialog dlg);

        public EmployeeDialog()
        {
            InitializeComponent();
        }

        protected void SaveData()
        {
            m_employee.m_name = m_textName.Text;
            m_employee.m_sex = m_textSex.Text;
            m_employee.m_age =  Int32.Parse(m_textAge.Text);
            m_employee.m_phone = m_textPhone.Text;
            m_employee.m_mobile = m_textMobile.Text;
        }

        private void EmployeeDialog_Load(object sender, EventArgs e)
        {
            if (m_employee != null)
            {
                m_textName.Text = m_employee.m_name;
                m_textSex.Text = m_employee.m_sex;
                m_textAge.Text = m_employee.m_age.ToString();
                m_textPhone.Text = m_employee.m_phone;
                m_textMobile.Text = m_employee.m_mobile;
            }
        }

        private void m_btnOK_Click(object sender, EventArgs e)
        {
            SaveData();
            m_notify(this);
            this.Close();
        }
        private void m_btnApply_Click(object sender, EventArgs e)
        {
            SaveData();
            m_notify(this);
        }
        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void SetEmployee(EmployeeData employee)
        {
            m_employee = employee;
        }
        public EmployeeData GetEmployee()
        {
            return m_employee;
        }
        public void SetNotify(Notify notify)
        {
            m_notify = notify;
        }

        private Notify m_notify;
        private EmployeeData m_employee;
    }

    public class EmployeeData
    {
        public void ParseString(string s)
        {
            int beg = -1;
            int end = -1;

            beg = 0;
            end = s.IndexOf(",", beg);
            if (end != -1)
            {
                m_name = s.Substring(beg, end - beg);
                beg = end + 1;
            }
            end = s.IndexOf(",", beg);
            if (end != -1)
            {
                m_sex = s.Substring(beg, end - beg);
                beg = end + 1;
            }
            end = s.IndexOf(",", beg);
            if (end != -1)
            {
                m_age = Int32.Parse(s.Substring(beg, end - beg));
                beg = end + 1;
            }
            end = s.IndexOf(",", beg);
            if (end != -1)
            {
                m_phone = s.Substring(beg, end - beg);
                beg = end + 1;
            }
            if (beg < s.Length)
            {
                m_mobile = s.Substring(beg);
            }
        }

        public override string ToString()
        {
            string s = string.Empty;

            s += m_name;
            s = s + "," + m_sex;
            s = s + "," + m_age.ToString();
            s = s + "," + m_phone;
            s = s + "," + m_mobile;

            return s;
        }

        public string m_name;
        public string m_sex;
        public int m_age;
        public string m_phone;
        public string m_mobile;
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#数据库绑定发布时间:2022-07-13
下一篇:
c#将http调用返回额json中的有关中文的unicode转换为中文(转)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap