如何以编程方式杀死命令窗口 [英] How to kill a command window programmatically

查看:65
本文介绍了如何以编程方式杀死命令窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序* .exe,它是一个GUI应用程序.现在,我需要将其转换为既可以用作控制台又可以用作GUI的
如果输入任何命令行参数,例如:
D:\ Projects \ TestProjects \ TestQtConsole \ Debug> TestQtConsole.exe fdsa
它应该只是作为控制台应用程序工作,其中应用程序接受命令args.

如果未输入命令行参数,则需要作为GUI应用程序使用.

我是这样的:

I have a application *.exe which is a GUI applicaition. Now I need to convert it to work as both console and GUI i.e.
if any commandline args is entered like :
D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe fdsa
It should simply work as console application wherein application accepts the command args.

if no commandline args are entered it need to work as GUI application.

I did this way:

int main(int argc, char *argv[])
{
 HWND hwndC = GetConsoleWindow();
 QApplication::Type appType = QApplication::GuiClient;
 if (argc > 1)
 {
  appType = QApplication::Tty;
 }
 QApplication a(argc, argv, appType);
 if (a.type() != QApplication::Tty)
 {
  CSensoRescue *w = new CSensoRescue();
  w->show();
  if (hwndC)
  {
   ShowWindow(hwndC, SW_HIDE);
  }
 }



我面临的两个问题是当我像这样输入时:

D:\ Projects \ TestProjects \ TestQtConsole \ Debug> TestQtConsole.exe fdsa

我收到了无效的arg作为输出.


当我输入
时第二次 D:\ Projects \ TestProjects \ TestQtConsole \ Debug> TestQtConsole.exe

让我改善我的问题:

当我直接通过双击启动.exe时,我也不想打开我的控制台.
我只想要GUI.



Two problems i''m facing is when i enter like :

D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe fdsa

i''m getting Invalid arg as output.


Second when i enter as
D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe

let me improve my Question:

When i start my .exe directly by double clicking , my console is also getting opened, that i don''t want.
i just want GUI only.

推荐答案

正如我从您的评论中看到的那样,您想关闭已在GUI模式下启动应用程序的控制台.在这种情况下,答案很简单:不要这样做.

严重的是,这种行为对于用户而言是意料之外的并且令人沮丧.想象一下有人正在打开控制台,使用良好的cddir检查磁盘,运行应用程序和系统命令,出于好奇而启动应用程序... BLAM,没有控制台,没有命令历史记录,应用程序窗口代替.我会很生气的:). 任何程序都不能关闭我的控制台!这是我的,而控制它的是我!" -这就是我要说的:).运行.bat/.cmd文件也将很困难,那么通过快捷方式使用命令行参数启动您的应用程序又如何呢?我根本不知道它是否会有父控制台.
因此,我的建议是除非您只为自己/企业使用而创建此应用程序,而且绝对必须这样做,否则请不要理会控制台.
As I can see from your comment, you want to close the console your application has been launched from in GUI mode. In this case the answer is simple: don''t do it.

Seriously, this kind of behaviour would be unexpected and frustrating for users. Imagine someone''s opening console, examining your disk with good old cds and dirs, running apps and system commands, launching you app just out of curiosity... BLAM, no console, no command history, app window instead. I''d be angered with that :). "No program should close MY console! It''s mine, and it''s me who controls it!" - that''s what I''d say :). Running a .bat/.cmd file would also be difficult, and what about your application being launched with command line arguments via shortcut? I simply don''t know whether it would have parent console.
So my recomendation is to leave the console alone unless you create this application for own/enterprise use only and it''s an absolute must.


将常用代码放入DLL中并创建不同的应用程序用于GUI和控制台.

首先,从一开始您的GUI应用程序将是没有控制台的GUI应用程序,而您的控制台应用程序将是没有GUI的控制台应用程序,因此使用单独的应用程序将具有很多优势.

为它们两个创建快捷方式会更容易.

您无需为控制台应用程序指定任何命令(如选项).如果未提供任何参数,则在控制台应用程序始终需要参数的情况下,只需显示使用信息即可.
Put common code in a DLL and create distinct application for the GUI and the console.

Using separate applications would have a lot of advantages beginning by the fact that from the start you GUI application would be a GUI application without a console.... and your console application would be a console application without a GUI.

It would be easier to create shortcut for both of them.

You won''t have to specifyu any command like options for the console application. If no argument are provided, simply dispaly usage information if the console application always need arguments.


建议:
1.将您的控制台程序命名为TestQtConsole.com
2.将您的GUI程序命名为TestQtConsole.exe
suggest:
1. make your console program named TestQtConsole.com
2. make your GUI program named TestQtConsole.exe


这篇关于如何以编程方式杀死命令窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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