既可以作为控制台也可以作为 GUI 编程 [英] Program both as Console and GUI

查看:20
本文介绍了既可以作为控制台也可以作为 GUI 编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以(如果可以,我如何)使用 Delphi 2007 使单个程序既可作为控制台应用程序又可作为 GUI 版本运行?

Is it possible to (and if so, how do I) make a single program work both as a console application and a GUI version using Delphi 2007?

我所追求的是,​​如果程序使用适当的命令行选项运行,它应该作为控制台程序运行,使用 WRITELN 将输出打印到控制台,但如果没有给出命令行参数,它应该运行为一个普通的 Delphi GUI 应用程序?

What I am after is that if the program is run with the appropriate command-line options, it should function as a console program, printing output to the console using WRITELN, but if no command line arguments are given it should run as a normal Delphi GUI application?

问题是,当作为控制台应用程序运行时,命令行解释器在允许您输入新命令之前等待应用程序终止,而从命令行启动的 GUI 应用程序会立即将您返回到命令行并GUI 应用程序在一个分离的进程中启动.我希望保留此行为.

The catch is that when running as a console application, the command line interpreter waits for the application to terminate before allowing you to enter a new command, whereas a GUI application started from the command line immediately returns you to the command line and the GUI application is started in a detached process. I want this behaviour retained.

我不介意这样的事情:

IF GUI THEN StartApplicationAsGUI(ParamStr(0))

IF GUI THEN StartApplicationAsGUI(ParamStr(0))

即.我不介意如果需要,我必须使用某种形式的 EXECUTE 调用重新启动应用程序以在 GUI 模式下启动它,只要在 GUI 版本启动时命令行界面返回到命令行输入.

ie. I don't mind that I'll have to restart the application using some form of EXECUTE call to start it in GUI mode if needed, as long as the command line interface returns to the command line input when the GUI version is started.

我更喜欢这样的解决方案/建议:

I'd prefer a solution/suggestion that is along the lines of:

<解析命令行>
IF 控制台模式 THEN
   RunConsole(参数)
其他开始
   Application.Initialize;
   Application.CreateForm(...)
   Application.Run;
结束

<Parse Comnand Line>
IF ConsoleMode THEN
   RunConsole(Parameters)
ELSE BEGIN
   Application.Initialize;
   Application.CreateForm(...)
   Application.Run;
END

(反之亦然,即在 GUI 模式下以特殊方式做事)

(or vice-versa, ie. doing things a special way if GUI mode)

这样我在制作GUI界面时仍然可以使用Delphi的IDE和VCL...

so that I can still use Delphi's IDE and VCL when making the GUI interface...

推荐答案

http://blogs.msdn.com/oldnewthing/archive/2009/01/01/9259142.aspx

这篇关于既可以作为控制台也可以作为 GUI 编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆