📄 unit2.pas
字号:
uses Forms, Windows, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
btnExecute: TButton;
btnCancel: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure btnExecuteClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
private
proc_info: TProcessInformation;
startinfo: TStartupInfo;
ExitCode: LongWord;
end;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
btnCancel.Enabled := False;
Timer1.Enabled := False;
Timer1.Interval := 200;
end;
procedure TForm1.btnExecuteClick(Sender: TObject);
begin
FillChar(proc_info, sizeof(TProcessInformation), 0);
FillChar(startinfo, sizeof(TStartupInfo), 0);
startinfo.cb := sizeof(TStartupInfo);
if CreateProcess(nil, 'c:\windows\notepad.exe', nil,
nil, false, CREATE_DEFAULT_ERROR_MODE
+ NORMAL_PRIORITY_CLASS, nil, nil, startinfo,
proc_info) then begin
btnExecute.Enabled := False;
btnCancel.Enabled := True;
Timer1.Enabled := True;
end else begin
CloseHandle(proc_info.hProcess);
Application.MessageBox('Couldn''t execute the '
+ 'application', 'Error', MB_ICONEXCLAMATION);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if GetExitCodeProcess(proc_info.hProcess, ExitCode)
then
if ExitCode = STILL_ACTIVE then
Timer1.Enabled := True
else begin
btnCancel.Enabled := False;
btnExecute.Enabled := True;
CloseHandle(proc_info.hProcess);
end
else begin
btnCancel.Enabled := False;
btnExecute.Enabled := True;
TerminateProcess(proc_info.hProcess, 0);
CloseHandle(proc_info.hProcess);
end;
end;
procedure TForm1.btnCancelClick(Sender: TObject);
begin
Timer1.Enabled := False;
if Application.MessageBox('You should try to finish'
+ ' the application normally.'#13#13'縏erminate it '
+ 'anyway?', 'Warning', MB_YESNO + MB_DEFBUTTON2 +
MB_ICONQUESTION + MB_TASKMODAL) = ID_YES then begin
TerminateProcess(proc_info.hProcess, 0);
CloseHandle(proc_info.hProcess);
btnCancel.Enabled := False;
btnExecute.Enabled := True;
end else begin
Timer1.Enabled := True;
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if btnCancel.Enabled then begin
btnCancelClick(Sender);
if btnCancel.Enabled then CanClose := False;
end;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -