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;
}
}

最新文章

  1. Ant: Class not found: javac1.8
  2. Python与C++结构体交互
  3. ABK (枚举)
  4. 分布式领域CAP理论
  5. win7_64bit下桌面及开始菜单中图标变为.lnk
  6. java的nio之:java的nio系列教程之buffer的概念
  7. Java笔记——面向切面编程(AOP模式)
  8. 【原创】Linux opensource-src-4.3.2.tar.gz的安装。
  9. XFire构建服务端Service的两种方式(转)
  10. 如何实现调用console.log(‘good’.repeat(3))时输出goodgoodgood?
  11. 【zabbix教程系列】二、zabbix特点
  12. numpy array的复制-【老鱼学numpy】
  13. 关于Oracle的44951事件
  14. 路飞学城-Python开发集训-第2章
  15. 转载:C++ 二维数组new
  16. How to show color in CSS
  17. zip&amp;ftp命令
  18. PHP json_encode/json_decode与serialize/unserializ性能测
  19. Ruby 基础教程 第二部分 Ruby 的基础 第4章
  20. 2017/2/14springmvc基础学习

热门文章

  1. vs2012常用快捷键总结
  2. Java的I/O总结
  3. flex安装debug
  4. Oberon程序设计—目录
  5. CSS,height:auto和height:100%有什么区别?
  6. 杭电三部曲一、基本算法;19题 Cow Bowling
  7. sql随机
  8. 重识 CSS
  9. hibernate的批量更新、批量删除
  10. DateUtils时间工具类探究