gpt4 book ai didi

delphi - 将控制台应用程序与 GUI 应用程序一起使用?

转载 作者:行者123 更新时间:2023-12-03 15:09:36 24 4
gpt4 key购买 nike

如何创建一个可以使用或不使用 GUI 的控制台应用程序?

例如,假设我有一个控制台应用程序,如果我尝试从 Windows 资源管理器启动此控制台应用程序,它将无法工作,它只会关闭,但我可以从我的 GUI 应用程序或 Windows 命令控制台 (cmd.exe) 调用它。 exe)并向其传递一些开关(参数?)。

这样,即使不启动 GUI 应用程序,也可以使用一些有用的功能,可以从命令行调用它们。

编辑

我不确定如何创建控制台应用程序,尤其是接受标志(开关、参数?)的控制台应用程序。

我见过一些执行类似操作的应用程序。例如,他们可能有一个控制台应用程序,可以将 bmp 转换为 png,GUI 调用此控制台应用程序并将参数等传递给它。

希望这是有道理的。

那么我该如何使用这样的东西呢?

谢谢。

最佳答案

For example, say if I had a console application, If i tried launching this console app from Windows Explorer it will not work it will just close, but I could call it from my GUI Application or the Windows Command Console (cmd.exe) and pass some switches (parameters?) to it.

起作用。但是,一旦程序退出,控制台窗口就会消失。如果您想让用户有机会在窗口关闭之前读取控制台应用程序的输出,只需使用一个

Readln;

Writeln('Press Enter to exit.');
Readln;

如果您想在 GUI 应用程序中使用控制台窗口进行输出(或输入),您可以尝试一下 AllocConsoleFreeConsole 函数。

命令行参数(例如 myapp.exe/OPEN "C:\some dir\file.txt"/THENEXIT)可用于所有类型的 Windows 应用程序,包括 GUI 应用程序和控制台应用程序。只需使用 ParamCountParamStr 函数即可。

如何创建接受命令行参数的控制台应用程序

在 Delphi IDE 中,选择"file"/“新建”/“控制台应用程序”。然后写

program Project1;

{$APPTYPE CONSOLE}

uses
Windows, SysUtils;

var
freq: integer;

begin

if ParamCount = 0 then
Writeln('No arguments passed.')

else if ParamCount >= 1 then

if SameText(ParamStr(1), '/msg') then
begin

if ParamCount = 1 then
Writeln('No message to display!')
else
MessageBox(0, PChar(ParamStr(2)), 'My Console Application',
MB_ICONINFORMATION);

end

else if SameText(ParamStr(1), '/beep') then
begin

freq := 400;

if ParamCount >= 2 then
if not TryStrToInt(ParamStr(2), freq) then
Writeln('Invalid frequency: ', ParamStr(2));

Windows.Beep(freq, 2000);

end;


end.

编译程序。然后打开命令处理器 (CMD.EXE) 并转到 Project1.exe 所在的目录。

那就试试

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1
No arguments passed.

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /msg
No message to display!

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /msg "This is a test."

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /beep

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>project1 /beep 600

C:\Users\Andreas Rejbrand\Documents\RAD Studio\Projects>

如何传递三个参数

if ParamCount >= 1 then
begin

if SameText(ParamStr(1), '/CONVERT') then
begin

// The user wants to convert

if ParamCount <= 2 then
begin
Writeln('Too few arguments!');
Exit;
end;

FileName1 := ParamStr(2);
FileName2 := ParamStr(3);

DoConvert(FileName1, FileName2);

end;

end;

关于delphi - 将控制台应用程序与 GUI 应用程序一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6563689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com