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


    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;
        }
    }
    
  • 相关阅读:
    异常介绍
    docker 命令
    acm
    Openfiler能把标准x86/64架构的系统变成一个强大的NAS、SAN存储和IP存储网关
    docker 图解学习
    基于Docker的TensorFlow机器学习框架搭建和实例源码解读
    菜鸟打印控件
    Oracle 12c on Solaris 10 安装文档
    内存对齐小解
    安装oracle 11gr2 rac on solaris
  • 原文地址:https://www.cnblogs.com/dongc/p/5225116.html
Copyright © 2020-2023  润新知