unitfrmmain.pas

来自「《Delphi实用程序100例》配套书源码盘」· PAS 代码 · 共 57 行

PAS
57
字号
unit unitFrmMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string; //存储指定的应用程序文件名
begin
  if opendialog1.Execute then //选择要调用的外部可执行程序
    begin
      str := opendialog1.FileName; //获取可执行文件名
      winexec(PChar(str), SW_SHOWNORMAL); //启动指定的可执行程序
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  hWndClose: HWnd; //存储指定的外部应用程序窗口句柄
  str: string; //存储指定的外部应用程序的窗口名
begin
  str := InputBox('提示', '请输入应用程序名:', ''); //获取要关闭的应用程序窗口名
  if str <> '' then begin
      //根据窗口名查找要关闭的窗口句柄
      hWndClose := FindWindow(nil, PChar(str));
      if hWndClose <> 0 then //如果查找成功,则发送消息,关闭指定的窗口
        SendMessage(hWndClose, WM_CLOSE, 0, 0)
      else //否则,给出提示信息
        ShowMessage('没找到指定的应用程序,所以无法关闭!');
    end;
end;
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?