是否可以使用Lazarus编写的Windows GUI程序创建控制台并在运行时写入控制台? [英] Can a Windows GUI program written in Lazarus create a console and write to it at runtime?
问题描述
编写Lazarus程序时,可以选择使用$APPTYPE console
或在项目选项"中取消选择Win32 GUI Application
(-WG
选项).
When writing a Lazarus program, you have the option of using $APPTYPE console
or deselecting the Win32 GUI Application
(-WG
option) in the Project Options .
我注意到没有这些选项,DebugLn
和WriteLn
会引发异常.即使没有对程序进行上述编译,也没有办法创建控制台,然后用DebugLn
和WriteLn
输出到控制台?
I noticed that without these options DebugLn
and WriteLn
raise exceptions. Is there a way to create a console even if the program is not compiled above mentioned options and output to it with DebugLn
and WriteLn
afterwards?
推荐答案
在Windows中,与Delphi相比,它有些棘手.在Delphi中,您所需要做的就是调用AllocConsole
.使用Lazarus/FreePascal,您需要做一些额外的工作:
In windows it is a little more tricky compared to Delphi. In Delphi all you need to do is to call AllocConsole
. Using Lazarus/FreePascal you need to do a little extra work:
uses
Windows;
begin
AllocConsole; // in Windows unit
IsConsole := True; // in System unit
SysInitStdIO; // in System unit
// Now you can do Writeln, DebugLn, ...
end.
很遗憾,我无法在其他平台上为您提供帮助.尽管linux控制台中的iirc对于某个程序始终存在,即使它不可见.因此,它无需额外的代码即可工作.但是我无法测试这个atm,所以要带一点盐.
Unfortunately I cannot help you on other platforms. Although iirc in linux console is always present for a program even if not visible. So it should work without extra code. However I cannot test this atm so take it with a grain of salt.
这篇关于是否可以使用Lazarus编写的Windows GUI程序创建控制台并在运行时写入控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!