在Win32程序中显示Dos调试窗口,可暂停(AllocConsole,WriteConsole,FreeConsole函数,GetStdHandle函数取得输入句柄)

在很多程序中,都可以看到程序运行中,会有一个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 = then Exit;
//释放控制台
FreeConsole;
end; end.

参考:http://www.cnblogs.com/key-ok/p/3429861.html

------------------------------------------------------------------------------------

function PauseConsole(Prompt: PAnsiChar): boolean;
var
hStdIn, hStdOut: THandle;
dwRd, dwWr, i: Cardinal;
cBuf: array [..] of TInputRecord;
begin
result := false;
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
if ((hStdIn <> ) and (hStdOut <> )) then
begin
WriteFile(hStdOut,Prompt[],lstrlenA(Prompt),dwWr,nil);
while ReadConsoleInput(hStdIn,cBuf[],,dwRd) do
begin
for i := to dwRd do
begin
if ((cBuf[i].EventType = KEY_EVENT) and (cBuf[i].Event.KeyEvent.bKeyDown)) then
begin
Result := true;
exit;
end;
end;
end;
end;
end; try
PauseConsole('Press a key to continue...');
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;

参考:http://www.cnblogs.com/key-ok/p/3380446.html

上一篇:JQuery 的基本命令


下一篇:IOS开发基础知识--碎片33