您的位置:首页 > 其它

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

2015-08-21 17:31 585 查看
在很多程序中,都可以看到程序运行中,会有一个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.


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

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

function PauseConsole(Prompt: PAnsiChar): boolean;
var
hStdIn, hStdOut: THandle;
dwRd, dwWr, i: Cardinal;
cBuf: array [0..128] of TInputRecord;
begin
result := false;
hStdIn := GetStdHandle(STD_INPUT_HANDLE);
hStdOut := GetStdHandle(STD_OUTPUT_HANDLE);
if ((hStdIn <> 0) and (hStdOut <> 0)) then
begin
WriteFile(hStdOut,Prompt[0],lstrlenA(Prompt),dwWr,nil);
while ReadConsoleInput(hStdIn,cBuf[0],128,dwRd) do
begin
for i := 0 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: