How do I write to StdOut from a GUI app started from the command line?

后端 未结 8 2149
一向
一向 2021-02-09 05:52

I am writing a standard windows app in Delphi 7.

If I was writing a console app, I can call the following to output to the cmd line or output file.

write         


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-09 06:25

    I'm not quite sure what you are trying to achieve.
    As I understood the question one way could be

    program Project1;
    {$APPTYPE CONSOLE}
    
    uses
      Forms, Classes, Windows,
      Unit1 in 'Unit1.pas' { Form1 } ;
    {$R *.res}
    
    var
      Finished: Boolean;
      Input: String;
    
    function IsConsoleMode(): Boolean;
    var
      SI: TStartupInfo;
    begin
      SI.cb := SizeOf(TStartupInfo);
      GetStartupInfo(SI);
      Result := ((SI.dwFlags and STARTF_USESHOWWINDOW) = 0);
    end;
    
    procedure HandleInput;
    begin
      Finished := Input = 'quit';
      if not Finished then
      begin
        Writeln('Echo: ' + Input);
      end
      else
        Writeln('Bye');
    end;
    
    begin
      if IsConsoleMode then
      begin
        Finished := false;
        Writeln('Welcome to console mode');
        while not Finished do
        begin
          readln(Input);
          HandleInput;
        end;
      end
      else
      begin
        Writeln('Entering GUI Mode');
        FreeConsole;
        Application.Initialize;
        Application.MainFormOnTaskbar := True;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      end;
    
    end.
    

提交回复
热议问题