⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 应用程序调用外部程序
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, mmsystem,Classes, Graphics, Registry,Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, TrayBarIcon;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Timer1: TTimer;
    Button3: TButton;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Timer2: TTimer;
    Button4: TButton;
    TrayIcon1: TTrayIcon;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    OldWindowProc: TWndMethod;
    function   WaitExeRun(FileName,Param:string):Boolean;//文件名和参数
    procedure FormWndProc(var Message: TMessage);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses Tlhelp32,ShellApi;

{$R *.dfm}
const
  DBT_DEVICEARRIVAL = $8000;     // system detected a new device
  DBT_DEVICEREMOVECOMPLETE = $8004;     // device is gone
       DBT_DEVTYP_VOLUME = $00000002;     // logical volume
         DBTF_MEDIA = $0001;     // media comings and goings
type
    PDEV_BROADCAST_HDR = ^TDEV_BROADCAST_HDR;
    TDEV_BROADCAST_HDR = packed record
     dbch_size : DWORD;
     dbch_devicetype : DWORD;
     dbch_reserved : DWORD;
    end;
     PDEV_BROADCAST_VOLUME = ^TDEV_BROADCAST_VOLUME;
     TDEV_BROADCAST_VOLUME = packed record
         dbcv_size : DWORD;
         dbcv_devicetype : DWORD;
         dbcv_reserved : DWORD;
         dbcv_unitmask : DWORD;
         dbcv_flags : WORD;
     end;


procedure TForm1.Button1Click(Sender: TObject);
var
  ofile:string;
  hwndd:hwnd;
begin

//WinExec(extractfilepath(application.exename)+'\FastCopy190\FastCopy.exe',1);
 //extractfilepath(application.exename)+\FastCopy190\FastCopy.exe
 hwndd:=findwindow(nil,'FastCopy ver1.90');
 if (hwndd=0) then
   begin
     ofile:=extractfilepath(application.exename)+'\FastCopy190\FastCopy.exe';
     shellexecute(handle,'open',pchar(ofile),nil,pchar(extractfilepath(ofile)),sw_shownormal);
   end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  hwndd:hwnd;

begin
hwndd:=findwindow(nil,'FastCopy ver1.90');
if hwndd<>0 then
  sendmessage(hwndd,wm_close,0,0);
 close;



end;

procedure TForm1.Timer1Timer(Sender: TObject);
var   
  Registry: TRegistry; //用於註冊表的訪問
  USBCount: Integer;
begin
Registry :=TRegistry.Create;
  with Registry do  
  begin
    RootKey:=HKEY_Local_Machine;
    If OpenKey('SYSTEM\CurrentControlSet\Services\USBSTOR\Enum',true) then
    begin   
      //how many USB strorages we get   
      USBCount:= Registry.ReadInteger('Count');  //how many device we have now
      edit1.Text:='USB设备目前有 ' + IntToStr(USBCount) + ' 个';
    
    end;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin

 OldWindowProc:= WindowProc;
  WindowProc := FormWndProc;
end;
procedure TForm1.FormWndProc(var Message: TMessage);
var
     lpdb : PDEV_BROADCAST_HDR;
     lpdbv : PDEV_BROADCAST_VOLUME;
     unitmask:DWORD;
     i: Integer;
begin
  lpdb := PDEV_BROADCAST_HDR(Message.LParam);

  OldWindowProc(Message);

  if (Message.Msg=WM_DEVICECHANGE) then
  begin
    if (Message.WParam=DBT_DEVICEARRIVAL) then
    begin
      //Memo1.Lines.Add('新插入了一项装置!');
      //showmessage('有U盘咯!');
      if lpdb.dbch_devicetype=DBT_DEVTYP_VOLUME then
      begin
        lpdbv := PDEV_BROADCAST_VOLUME(lpdb);
        unitmask:=lpdbv.dbcv_unitmask;//

⌨️ 快捷键说明

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