المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : الإستجابة لأي سواقة يتم إدخالها في الكمبيوتر لحظياً


issamdnn
22-10-2009, 11:01 AM
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,MATH;
CONST
DBTF_Media = $0001;
DBT_DEVTYP_DEVICEINTERFACE = $0005;
DBT_DEVTYP_HANDLE = $0006;
DBT_DEVTYP_OEM = $0000;
DBT_DEVTYP_PORT = $0003;
DBT_DEVTYP_VOLUME = $0002;
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
DBTF_NET = $0001;
//DBT_DEVTYP_VOLUME = $0002;
DBT_CONFIGCHANGECANCELED = $0019;
DBT_CONFIGCHANGED = $0018;
DBT_CUSTOMEVENT = $8006;
// DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEQUERYREMOVE = $8001;
DBT_DEVICEQUERYREMOVEFAILED = $8002;
// DBT_DEVICEREMOVECOMPLETE = $8004;
DBT_DEVICEREMOVEPENDING = $8003;
DBT_DEVICETYPESPECIFIC = $8005;
DBT_DEVNODES_CHANGED = $0007;
DBT_QUERYCHANGECONFIG = $0017;
DBT_USERDEFINED = $FFFF ;
type



DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;
{$EXTERNALSYM DEV_BROADCAST_HDR}
TDevBroadcastHdr = DEV_BROADCAST_HDR;
PDevBroadcastHdr = ^TDevBroadcastHdr ;

DEV_BROADCAST_VOLUME = packed record
dbcv_size: DWORD;
dbcv_devicetype: DWORD;
dbcv_reserved: DWORD;
dbcv_unitmask: DWORD;
dbcv_flags: Word;
end;
{$EXTERNALSYM DEV_BROADCAST_VOLUME}
TDevBroadcastVolume = DEV_BROADCAST_VOLUME;
PDevBroadcastVolume = ^TDevBroadcastVolume;

TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
Button1: TButton;
Timer1: TTimer;
Label2: TLabel;
Label3: TLabel;
Function GetDrive(pDBVol: PDevBroadcastVolume): string;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
// procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;

private
procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;

{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Msg: TMessage;
implementation

{$R *.dfm}

procedure TForm1.WMDeviceChange(var Msg: TMessage);
var
Drive: string;
bv: PDevBroadcastVolume;
begin
case Msg.wParam of
DBT_DeviceArrival:
if PDevBroadcastHdr(Msg.lParam)^.dbch_devicetype =
DBT_DevTyp_Volume then
begin
bv := PDevBroadcastVolume(Msg.lParam);
Drive := GetDrive(bv);
// first check if network drive, if 0 then has to be DBTF_MEDIA
if ((bv^.dbcv_flags and DBTF_NET) = DBTF_NET) then
begin
label1.Caption := 'Network drive added, Drive ' + Drive;
end
else
begin
// if DBTF_MEDIA is 1 then it's media, otherwise it's a device
if ((bv^.dbcv_flags and DBTF_MEDIA) = DBTF_MEDIA) then
label1.Caption := 'Media inserted in Drive ' + Drive
else
label1.Caption := 'الجهاز ' + Drive +'تم إدخاله'
end;
end;
DBT_DeviceRemoveComplete:
if PDevBroadcastHdr(Msg.lParam)^.dbch_devicetype =
DBT_DevTyp_Volume then
begin
bv := PDevBroadcastVolume(Msg.lParam);
Drive := GetDrive(bv);
if ((bv^.dbcv_flags and DBTF_NET) = DBTF_NET) then
begin
label1.Caption := 'Network drive removed, Drive ' + Drive;
end
else
begin
if ((bv^.dbcv_flags and DBTF_MEDIA) = DBTF_MEDIA) then
label1.Caption := 'Media removed from Drive ' + Drive
else
label1.Caption := 'الجهاز ' + Drive +'تم فصله' ;

end;
end;
end;
END;
Function TForm1.GetDrive(pDBVol: PDevBroadcastVolume): string;
var
i: Byte;
Maske: DWORD;
drive: Char;
begin
Maske := pDBVol^.dbcv_unitmask;
drive := 'A';
for i := 0 to 25 do
begin
if (Maske and Round(Power(2, I))) > 0 then
begin
Result := drive;
Break;
end;
Inc(drive);
end;
end;

{function TForm1.GetFirstDrive(UnitMask: DWORD): Char;
var
I: Char;
begin
Result:=#0;
for I:='A' to 'Z' do
begin
if (UnitMask and 1) = 1 then
begin
Result:=I;
break;
end;
UnitMask:=UnitMask Shr 1;
end;
end; }

procedure TForm1.Button1Click(Sender: TObject);
VAR
MES:tagMSG;
begin
//GetMessage(MES,0,0,0);
//WMDeviceChange(MES);

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
//WMDeviceChang(Msg);
end;
{procedure TForm1.WMDeviceChang(var Msg: TMessage);
VAR
Drive: string;
bv: PDevBroadcastVolume;
s: string;
begin
case Msg.wParam of
DBT_DeviceArrival:
begin
Label2.Caption:=FormatCurr('',PDevBroadcastHdr(Msg .lParam)^.dbch_devicetype);
case PDevBroadcastHdr(Msg.lParam)^.dbch_devicetype of
DBT_DEVTYP_DEVICEINTERFACE: s := 'DBT_DEVTYP_DEVICEINTERFACE arrival';
DBT_DEVTYP_HANDLE: s := 'DBT_DEVTYP_HANDLE arrival';
DBT_DEVTYP_OEM: s := 'DBT_DEVTYP_OEM arrival';
DBT_DEVTYP_PORT: s := 'DBT_DEVTYP_PORT arrival';
DBT_DEVTYP_VOLUME: s := 'DBT_DEVTYP_VOLUME arrival';
else
s := 'No arrival match';
end;
Label1.Caption := s;
end;
DBT_DeviceRemoveComplete:
begin
case PDevBroadcastHdr(Msg.lParam)^.dbch_devicetype of
DBT_DEVTYP_DEVICEINTERFACE: s := 'DBT_DEVTYP_DEVICEINTERFACE remove';
DBT_DEVTYP_HANDLE: s := 'DBT_DEVTYP_HANDLE remove';
DBT_DEVTYP_OEM: s := 'DBT_DEVTYP_OEM remove';
DBT_DEVTYP_PORT: s := 'DBT_DEVTYP_PORT remove';
DBT_DEVTYP_VOLUME: s := 'DBT_DEVTYP_VOLUME remove';
else
s := 'No remove match';
end;
Label1.Caption := s;
end;
end;
end; }

end.