简体   繁体   English

将MFC gui添加到Win32 C ++命令行应用程序的好方法是什么?

[英]What is a good way to add MFC gui to a Win32 C++ command line application?

We have a command line application that could benefit from a GUI. 我们有一个可以从GUI中受益的命令行应用程序。 We want to add some plotting functionality and have identified a plotting library that uses MFC. 我们要添加一些绘图功能,并确定了使用MFC的绘图库。 Initially we developed a separate app, but we'd rather have the GUI in the same process space. 最初,我们开发了一个单独的应用程序,但是我们宁愿在相同的处理空间中使用GUI。

I was thinking of possibly a GUI in an MFC DLL that could be hosted in the production app AND in a testing app. 我正在考虑可能在生产应用程序和测试应用程序中托管的MFC DLL中的GUI。

The questions are: 问题是:

  • What are the steps necessary to add an MFC GUI to a win32 command line app 将MFC GUI添加到Win32命令行应用程序需要执行哪些步骤?
  • Is it possible to make a GUI in an MFC DLL and how can it be done? 是否可以在MFC DLL中制作GUI,该怎么做? (so that different apps can reuse the same GUI) (以便不同的应用程序可以重复使用相同的GUI)

EDIT 编辑

I should add that this is an unmanaged app (and needs to stay that way - it needs to be highly performant, makes extensive use of templates, boost, custom allocators, internally developed thread serialization, etc) 我应该补充一点,这是一个不受管的应用程序(需要保持这种状态-它需要高性能,广泛使用模板,boost,自定义分配器,内部开发的线程序列化等)

RESULTS: 结果:

Nick D's answer worked great - especially the follow-up link in his comment with the details about a regular MFC DLL . Nick D的答案非常有用- 特别是在评论中的后续链接中,其中包含有关常规MFC DLL的详细信息

Note that we will be using Qt for the next iteration. 请注意,我们将在下一次迭代中使用Qt。 Modifying our build environment and getting used to aa new framework was just too much this time around. 这次修改我们的构建环境并适应一个新的框架实在太多了。

You can call/reuse GUI code in a dll. 您可以在dll中调用/重用GUI代码。 (I even use Delphi forms in my C++ projects) (我什至在我的C ++项目中使用Delphi表单)

A very simple dll example: 一个非常简单的dll示例:

// The DLL exports foo() function
void foo()
{
    AFX_MANAGE_STATE( AfxGetStaticModuleState() );

    CDlgFoo dlg;
    dlg.DoModal();
}

In the console program you'll have code like this: 在控制台程序中,您将具有以下代码:

h = ::LoadLibrary( "my.dll" );
::DisableThreadLibraryCalls( h ); 
pfoo = (foo_type*)::GetProcAddress( h, (const char*)1 );
if ( pfoo ) pfoo();

First, you will have to surrender WinMain(). 首先,您必须放弃WinMain()。

If you still want to retain the command-line arguments functionality, process command arguments in InitInstance() of your App class. 如果仍要保留命令行参数功能,请在App类的InitInstance()中处理命令参数。

The straight forward approach would be to add a switch to your program and given a certain value it will launch the gui, otherwise use the command line options. 直接的方法是在程序中添加一个开关,并给定一定的值,它将启动gui,否则使用命令行选项。 Something like "app.exe -mode=gui". 类似于“ app.exe -mode = gui”。 If you don't see this command arg on program launch, fall back to the old command line behavior. 如果在程序启动时没有看到此命令arg,请回退到旧的命令行行为。

Regarding the DLL, you could write all the UI functionality in a DLL and use it from your "production app" where you have a message loop running and a WinMain. 关于DLL,您可以在DLL中编写所有UI功能,并从运行消息循环和WinMain的“生产应用程序”中使用它。 But what's the point? 但是有什么意义呢? If it's for testing purposes why not just separate the presentation from the logic and test the logic alone. 如果是出于测试目的,那么为什么不将演示文稿与逻辑分开并将其单独进行测试。 How do you intend to test the UI in your test app anyway? 无论如何,您打算如何在测试应用程序中测试UI? Simulate button clicks? 模拟按钮点击?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM