在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料。
用到的API函数如下:
//创建控制台
AllocConsole;
//获取控制台窗口
GetStdHandle;
//向控制台输出信息
WriteConsole;
//释放控制台
FreeConsole;
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } //控制台句柄 h_Console:THandle; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var p:PChar; num:Cardinal; begin //获取控制台窗口 h_Console := GetStdHandle(STD_OUTPUT_HANDLE ); p := PChar(Edit1.Text); //向控制台输出信息 WriteConsole(h_Console,p,Length(Edit1.Text),num,nil); end; procedure TForm1.FormCreate(Sender: TObject); begin //创建控制台 AllocConsole; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if h_Console = 0 then Exit; //释放控制台 FreeConsole; end; end.