C#构建GUI界面--C++生成DLL的编程框架
2016-07-16 10:29
543 查看
(如需交流,请关注公众号:神马观止)
由于之前一直使用MFC,所以对于C#并没有太多的学习,然而MFC编写界面的繁琐让大量的时间耗费在了GUI的排版和构建中。所以,最终我选择了用C# Windows Form。然而,为了将核心算法模块和GUI剥离开,最终我选择了以C# Windows Form构建GUI,C++ DLL实现核心算法的编程框架,并坚持以后都以此框架来编写常用算法分析和仿真工具。我以VS2008为例:
1. 首先新建Win32 Console Apllication工程,记得更改Solution Name。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/01a78b93b670ff1561152b8cc440ef45)
然后,选择Apllication type,而Additional options可勾选Export symbols,如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/09c22cd30e924c0c6a961a4dc21fc0e9)
2.构建TestDll工程
默认生成了3个.h文件和3个.cpp文件。现在需要添加三个文件,即:
(1) TestDataStructure.h----专门用来定义所需要的数据类型,方便后面使用
(2) ExportAPI.cpp----作为需要导出API的接口函数
(3) DefAPI.def----这个文件非常重要,定义了函数的导出点,这样在C#程序中才能调用这个函数
如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/d9d72087dcfb70471cf424a87f1ab163)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/aabc44d98e88ea333eed8e884f5de68b)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/6aadfc533fb349b5bee4392b73719b22)
具体文件内容如下:
(1) TestDll.h
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/4ff4dc4e1ce721d7f04aa22e521bc851)
然后Build TestDll,即可得到TestDll.dll
3. 构建C#主程序
这里为了简单起见采用C# Console Application,在Solution中选择“Add to Solution”,如图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/420e73dad188e58852675058fb39aa0e)
Program.cs代码为:
然后配置TestGUI工程的Build属性,Project -> TestGUI Properties -> Build,配置如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/18/7ad5fe33bcc3982e60726e98aa70bee4)
最后设置TestGUI为“Set as Startup Project”。
Build Solution然后运行,可以看见TestGUI可以成功调用TestDll的TestDll.dll中的接口函数。
如果创建Windows Form工程,过程是一样的,核心思想仍然是在TestGUI中定义一个和TestDll中一样的数据结构,通过接口函数进行数据传递,只是对数据结构的处理是在TestDll中进行,而处理结果是在TestGUI中显示的。
尾声:实际上这个框架可以更加复杂用来进行仿真分析,而方便多人一起合作编程,每个人负责一个DLL的编写,一个人负责GUI界面的搭建。另外现在的数据输入需要通过GUI的用户输入,其实完全可以再添加数据库部分,将数据源独立出去从而使整个框架更适合仿真模型的构建和仿真分析。接下来我准备将SQLite添加进去,对框架进行更新。
由于之前一直使用MFC,所以对于C#并没有太多的学习,然而MFC编写界面的繁琐让大量的时间耗费在了GUI的排版和构建中。所以,最终我选择了用C# Windows Form。然而,为了将核心算法模块和GUI剥离开,最终我选择了以C# Windows Form构建GUI,C++ DLL实现核心算法的编程框架,并坚持以后都以此框架来编写常用算法分析和仿真工具。我以VS2008为例:
1. 首先新建Win32 Console Apllication工程,记得更改Solution Name。
然后,选择Apllication type,而Additional options可勾选Export symbols,如下图所示:
2.构建TestDll工程
默认生成了3个.h文件和3个.cpp文件。现在需要添加三个文件,即:
(1) TestDataStructure.h----专门用来定义所需要的数据类型,方便后面使用
(2) ExportAPI.cpp----作为需要导出API的接口函数
(3) DefAPI.def----这个文件非常重要,定义了函数的导出点,这样在C#程序中才能调用这个函数
如图:
具体文件内容如下:
(1) TestDll.h
// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the TESTDLL_EXPORTS // symbol defined on the command line. this symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // TESTDLL_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifndef __TESTDLL_H__ #define __TESTDLL_H__ #define TESTDLL_API __declspec(dllexport) #endif(2) TestDll.cpp
// TestDll.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include <iostream> #include "TestDll.h" #include "TestDataStructure.h" using namespace std; CTestDll::CTestDll() { width = 6.0; height = 4.0; flag = 1; } float CTestDll::CalculateArea(float w, float h) { if ((w > 0 && h > 0) && (1 == flag)) return w * h; else { cout << "Something wrong!" << endl; return -1; } } bool CTestDll::IsTriangle() { if (1 == flag) return true; else return false; }(3) TestDataStructure.h
#ifndef __TESTDLL_TEST_DATA_STRUCTURE__ #define __TESTDLL_TEST_DATA_STRUCTURE__ #include "TestDll.h" // This class is exported from the TestDll.dll class TESTDLL_API CTestDll { public: CTestDll(void); // TODO: add your methods here. float CalculateArea(float, float); bool IsTriangle(); public: float width; float height; bool flag; }; #endif(4) ExportAPI.cpp
#include "stdafx.h" #include "TestDll.h" #include "TestDataStructure.h" // This is an example of an exported function. TESTDLL_API float fnTestDll(void) { CTestDll testdll; float w = testdll.width; float h = testdll.height; float area = testdll.CalculateArea(w, h); return area; }(5) DefAPI.def
LIBRARY "TestDll" EXPORTS fnTestDll;
然后Build TestDll,即可得到TestDll.dll
3. 构建C#主程序
这里为了简单起见采用C# Console Application,在Solution中选择“Add to Solution”,如图:
Program.cs代码为:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace TestGUI { class Program { [DllImport("../../../Debug/TestDll.dll")] public static extern float fnTestDll(); static void Main(string[] args) { Console.WriteLine("******************* Main Start **********************"); Console.WriteLine(fnTestDll()); Console.Read(); } } }
然后配置TestGUI工程的Build属性,Project -> TestGUI Properties -> Build,配置如下:
最后设置TestGUI为“Set as Startup Project”。
Build Solution然后运行,可以看见TestGUI可以成功调用TestDll的TestDll.dll中的接口函数。
如果创建Windows Form工程,过程是一样的,核心思想仍然是在TestGUI中定义一个和TestDll中一样的数据结构,通过接口函数进行数据传递,只是对数据结构的处理是在TestDll中进行,而处理结果是在TestGUI中显示的。
尾声:实际上这个框架可以更加复杂用来进行仿真分析,而方便多人一起合作编程,每个人负责一个DLL的编写,一个人负责GUI界面的搭建。另外现在的数据输入需要通过GUI的用户输入,其实完全可以再添加数据库部分,将数据源独立出去从而使整个框架更适合仿真模型的构建和仿真分析。接下来我准备将SQLite添加进去,对框架进行更新。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c#调用COM组件
- FREEBASIC 编译可被python调用的dll函数示例
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- DLL(Dynamic Linkable Library) 详解说明
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性