C++ 代码加 C# GUI

C++ code plus C# GUI

我正在制作使用 tesseract、opencv、SDL、word automation 和未来可能使用其他库的程序,用 C++ 编写。我能制作的最好的 GUI 是什么(MFC、C++/CLI、ATL、WTL、C#)——开发速度最快? tesseract、opencv、SDL 库是在未管理的还是托管的 C++ 上编写的?有没有办法在 C++/CLR 或 C# 中使用用 C++ 编写的非托管库?我也听说过类似 "Services" 的事情。它们是否允许语言和 GUI 开发的组合,哪个更好(或开发速度更快)-> dll 或服务?

你的问题很宽泛,所以我将重点关注一些要点:

  1. C 和 C++ 库可以在 C++/CLI 中使用

  2. 用 C# 编写的程序集可以轻松访问用 C++/CLI 编写的程序集中的托管代码(ref classes 或 value classes)

  3. .NET 中基本上有 2 个 GUI 框架可用:WPF 和 Windows Forms。甚至可以直接从 C++/CLI 使用 Windows 表单,但是:不要这样做! 相反:

    • 用 C++/CLI 为本机库编写包装器代码
    • 选择一个 GUI 框架并在 C# 中实现您的 GUI
    • 通过 C++/CLI-Wrappers 使用本机库

编辑 - 一个非常简单的包装器示例:

// native includes
#include <Windows.h>
#include <lmcons.h>

namespace My 
{
    // a managed class which is public and can be used by other assemblies
    public ref class Wrapper
    {
    public:
        // a function with a managed return type 
        static System::String^ CallNativeFunction()
        {
            // call native function
            DWORD length = UNLEN;
            wchar_t userName[UNLEN];
            GetUserName(userName, &length);

            // convert result of native function to managed System::String
            System::String^ result = gcnew System::String(userName, 0, length);

            return result;
        }
    };
} // namespace

int main(array<System::String ^> ^args)
{
    // usage of the wrapper class
    System::Console::WriteLine(My::Wrapper::CallNativeFunction());
    return 0;
}

在您的 C# 项目中,您可以添加对 C++/CLI 项目的引用,然后只需调用 My.Wrapper.CallNativeFunction().