procedure TForm1.Button1Click(Sender: TObject); begin winExec('c:\test.bat',0);end;procedure TForm1.Timer1Timer(Sender: TObject); begin if (fileExists('c:\test.txt')) then memo1.Lines.LoadFromFile('c:\test.txt'); end;
以下是c:\test.bat的内容:rem-------------------- @echo off dir c:\>c:\test.txt rem--------------------分数太少。
begin
winExec('c:\test.bat',0);end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
if (fileExists('c:\test.txt')) then
memo1.Lines.LoadFromFile('c:\test.txt');
end;
@echo off
dir c:\>c:\test.txt
rem--------------------分数太少。
Dos内部命令是不能在 WinExec() 里执行的。
procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(handle,nil,pchar('c:\windows\123.bat'),nil,nil,sw_shownormal);
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellAPI;type
TConsoleForm = class(TForm)
btOpen: TButton;
btClose: TButton;
btRun: TButton;
btOutput: TButton;
procedure btOpenClick(Sender: TObject);
procedure btCloseClick(Sender: TObject);
procedure btOutputClick(Sender: TObject);
procedure btRunClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
ConsoleForm: TConsoleForm;implementation{$R *.dfm}procedure TConsoleForm.btOpenClick(Sender: TObject);
begin
if not AllocConsole then Application.MessageBox('Can''t allocate console!','Console',MB_OK OR MB_ICONINFORMATION)
else SetConsoleTitle('Console Demo');
end;procedure TConsoleForm.btCloseClick(Sender: TObject);
begin
if not FreeConsole then Application.MessageBox('Can''t free console!','Console',MB_OK OR MB_ICONINFORMATION);
end;procedure TConsoleForm.btOutputClick(Sender: TObject);
var OutText:PChar;nWrite:Cardinal;sHandle:Cardinal;
begin
OutText:='Hello,console!'#$A#$D;
sHandle:=GetStdHandle(STD_OUTPUT_HANDLE);
if (sHandle<>0) then
begin
WriteConsole(sHandle,OutText,StrLen(OutText),nWrite,nil);
end;
end;procedure TConsoleForm.btRunClick(Sender: TObject);
begin
//这里要运行dos的dir命令,并将结果输出到"Open"按钮打开的Console
//Window,怎么写
end;end.