简体   繁体   English

将GUI集成到控制台应用程序中

[英]Integrating GUI into console applications

I have two independent console applications developed in C++. 我有两个用C ++开发的独立控制台应用程序。 I was asked to develop a GUI for both of them. 我被要求为它们开发GUI。 In this GUI I collect some parameters that are needed to launch any of these applications. 在此GUI中,我收集了启动任何这些应用程序所需的一些参数。 Anyone can give some orientation on integrating the console applications with this GUI? 任何人都可以在将控制台应用程序与此GUI集成时给出一些指导? Could it be possible to launch the applications from the GUI and not seeing the console? 是否可以从GUI启动应用程序而不是看到控制台? Can the things that the applications write in the console be redirected to a log file? 应用程序在控制台中写入的内容是否可以重定向到日志文件?

Thanks in advance. 提前致谢。

Starting from GUI: 从GUI开始:

1) Try to extract the parts of the console application which are pure logic, and have nothing to do with displaying things on the screen. 1)尝试提取控制台应用程序的纯逻辑部分,与屏幕上的显示无关。 Throw out the parts that display data inside of the console. 丢弃在控制台内显示数据的部分。

You can use a GUI Toolkit, for example Qt , which is extremely easy to setup and use. 您可以使用GUI工具包,例如Qt ,它非常易于设置和使用。


Then 然后

2) If you want the application to write things to a log file, then there are two things you can do: 2)如果您希望应用程序将事物写入日志文件,那么您可以执行以下两项操作:

a) Launch the program with an argument that will redirect STDOUT to a file, for example: a)使用将STDOUT重定向到文件的参数启动程序,例如:

./MyProgram > logfile.txt

b) Simply open a file and write to it from within your application by modifying the printing code. b)只需打开一个文件,然后通过修改打印代码从应用程序中写入文件。

I hope this helps. 我希望这有帮助。

To decouple the GUI from the command line tools you have your GUI call the existing programs supplying the proper parameters. 要将GUI与命令行工具分离,您可以使用GUI调用提供适当参数的现有程序。

You can use the CreateProcess() API function redirecting its output to a filehandle . 您可以使用CreateProcess() API函数将其输出重定向到文件句柄 That call receives a STARTUPINFO structure where you can specify if the window for the command is shown or not. 该调用接收STARTUPINFO结构,您可以在其中指定是否显示命令的窗口。

Check the links for examples, a complete create process sample can be found here . 查看链接以获取示例,可在此处找到完整的创建过程示例。

That's the pure Windows API way of doing things. 这是纯Windows API的做事方式。

If you are doing C++/.NET development things are way easier via System.Diagnostics.Process . 如果您正在通过System.Diagnostics.Process进行C ++ / .NET开发,那么事情会更容易。

If you can choose any language to do it I'd certainly go the .NET way (and even change the language to C#), it's almost trivial (as it manages all the API calls itself, and you have a sane interface to it) 如果你可以选择任何语言来实现它,我肯定会采用.NET方式(甚至将语言改为C#),它几乎是微不足道的(因为它管理所有的API调用本身,你有一个理智的界面)

You can also redirect output via the shell, but that's fairly inflexible and error prone, IMO. 你也可以通过shell重定向输出,但这是相当不灵活且容易出错的,IMO。

I'd say you have two clear options: 我会说你有两个明确的选择:

  1. Extend your applications and link them against a GUI, making the function calls from the GUI. 扩展应用程序并将其链接到GUI,从GUI进行函数调用。
  2. Create a GUI that is an independent application (compiled by its own without any code dependencies from the console apps), that calls (execs) the console apps at runtime, passing the parameters collected from the forms or whatever interaction mechanism it provides. 创建一个独立应用程序的GUI(由它自己编译而没有来自控制台应用程序的任何代码依赖),在运行时调用(执行)控制台应用程序,传递从表单收集的参数或它提供的任何交互机制。

The second option is generally easier and cleaner, but may go "out of sync" if the console apps arguments syntax changes. 第二个选项通常更简单,更清晰,但如果控制台应用程序参数语法更改,则可能会“不同步”。

Take a look at Expect . 看看Expect Different implementations are available and will link to GUI libraries. 可以使用不同的实现,并链接到GUI库。

Expect is designed to interface to CLIs and interact with them (feeding inoput, reading output). Expect旨在与CLI进行交互并与它们进行交互(提供inoput,读取输出)。 Depending on the implementation chosen (there are Perl/Java/Python along with the original Tcl) you can then use the corresponding GUI toolkit (eg Swing to Java). 根据所选的实现(Perl / Java / Python以及原始Tcl),您可以使用相应的GUI工具包(例如Swing to Java)。

Take a look at Tcl/Tk. 看看Tcl / Tk。 It was designed specifically for retrofitting scripting to command line C programs and has a C API that makes it quite straightforward to do this. 它专门用于为命令行C程序改进脚本,并且具有一个C API,使得执行此操作变得非常简单。 As a bonus, you also get a scripting capability for your programs. 作为奖励,您还可以获得程序的脚本功能。 There are several ways it supports this type of integration: 有几种方法可以支持这种类型的集成:

  • Link the Tcl interpreter into the program. 将Tcl解释器链接到程序中。 There is a straightforward API to convert from Tcl data structures into argv type char ** constructs. 有一个简单的API可以从Tcl数据结构转换为argv类型的char **构造。 This lets you invoke the program and use the command line parameters pretty much as is. 这使您可以调用程序并使用命令行参数。

  • Spawn a pipe to the C/C++ program and interact with it over the pipe. 将管道生成到C / C ++程序并通过管道与它交互。

  • Spawn the program with its command parameters from the Tcl script. 使用Tcl脚本中的命令参数生成程序。

  • Other, more elaborate RPC constructs (but you probably don't need these). 其他更精细的RPC构造(但你可能不需要这些)。

Tcl is very portable and will run on many platforms including Windows. Tcl非常便携,可以在许多平台上运行,包括Windows。 Tcl also comes with an easy to program GUI toolkit called Tk. Tcl还附带了一个易于编程的GUI工具包,称为Tk。 Modern versions support theming engines to give them a native look-and-feel, so you can make the Tk apps look pretty close to native windows ones. 现代版本支持主题引擎,使其具有原生外观,因此您可以使Tk应用看起来非常接近原生Windows应用程序。

This posting discusses using Tcl/Tk for doing the sort of thing you are asking about in some depth. 这篇文章讨论了如何使用Tcl / Tk来深入研究你要问的事情。

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

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