在delphi中的console application,其界面就是一个控制台,即象dos一样的界面。
以前一般直接new一个application,使用的是delphi提供的默认界面,生成一个form。
其project单元代码如下:
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}
begin
Application.Initialize; //初始化
Application.CreateForm(TForm1, Form1); //创建窗体form1
Application.Run; //运行,运行后显示主窗体(默认form1),并根据用户的操作激发事件,进而执行相应的代码
end.
而console application的project单元代码如下:
program Project1;{$APPTYPE CONSOLE}
uses
SysUtils;//变量声明在这里,在console application中声明的变量都是全局变量吗?(待查)
begin
{ TODO -oUser -cConsole Main : Insert code here }
//代码添加到这里,是应用程序执行你要的操作
end.
要进行console application编程,必须了解其API。在网上找了一会,只找到以下API的用法:
GetStdHandle(STD_OUT_HANDLE):THandle
SetConsoleTitle(String) //标题
SetConsoleTextAttribute(THandle,FOREGROUND_BLUE); //字体颜色
Writeln(string)
Write(string)
SetConsoleTextAttribute(THandle,RGB(100,200,255)) //底色
Readln;
Read;上面和下面一组都用于设置输出字体颜色
function SetConsoleActiveScreenBuffer; external kernel32 name 'SetConsoleActiveScreenBuffer';
function SetConsoleCP; external kernel32 name 'SetConsoleCP';
function SetConsoleCtrlHandler; external kernel32 name 'SetConsoleCtrlHandler';
function SetConsoleCursorInfo; external kernel32 name 'SetConsoleCursorInfo';
function SetConsoleCursorPosition; external kernel32 name 'SetConsoleCursorPosition';
function SetConsoleMode; external kernel32 name 'SetConsoleMode';
function SetConsoleOutputCP; external kernel32 name 'SetConsoleOutputCP';
function SetConsoleScreenBufferSize; external kernel32 name 'SetConsoleScreenBufferSize';
function SetConsoleTextAttribute; external kernel32 name 'SetConsoleTextAttribute';
function SetConsoleTitle; external kernel32 name 'SetConsoleTitleA';
function SetConsoleTitleA; external kernel32 name 'SetConsoleTitleA';
function SetConsoleTitleW; external kernel32 name 'SetConsoleTitleW';
function SetConsoleWindowInfo; external kernel32 name 'SetConsoleWindowInfo';