给你转帖一个吧,其实我也不会,一起进步TForm1 = class(TForm)
label1:Tlabel;
.......
procedure WMDeviceChange(var Msg : TWMDeviceChange);
procedure HasCdRom;
procedure HasNotCdRom;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
FWindowHandle: HWND;
procedure myWndProc(var Msg: TMessage);
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.myWndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then //如果是设备改变消息的话
try
WMDeviceChange(TWMDeviceChange(Msg));
except
Application.HandleException(Self);
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
//如果不是设备改变消息,则交给FORM缺省的消息处理系统完成
end;
procedure TForm1.WMDeviceChange(var Msg : TWMDeviceChange);
var
lpdb : PDEV_BROADCAST_HDR; //
lpdbv : PDEV_BROADCAST_VOLUME;
begin
(* 接受 a wm_devicechange message *)
lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
(* 查看 wm_devicechange 消息中EVENT 是何事件 *)
case Msg.Event of
DBT_DEVICEARRIVAL : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then
begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
begin
//CheckCdRomVolume;我的程序里要检查光盘卷标
//HasCdRom; CDRom被加载
showmessage('有光盘加入进来');
end;
end;
end;
DBT_DEVICEREMOVECOMPLETE : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
//HasNotCdRom; CD Rom被取出
showmessage('CD被取出');
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FWindowHandle:=AllocateHWnd(mywndproc);
//截取WINDOWS交给每个窗口的消息,交给MYWNDPROC处理,用来判断CD驱动器改变
//在WIN95里,每个设备的改变,系统都要发一个消息给所有当前窗口
//所以这里要用自定义的消息循环代替DELPHI 程序默认的窗口消息循环
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
DeallocateHWnd(FWindowHandle);
//释放句柄
end;
label1:Tlabel;
.......
procedure WMDeviceChange(var Msg : TWMDeviceChange);
procedure HasCdRom;
procedure HasNotCdRom;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject); private
FWindowHandle: HWND;
procedure myWndProc(var Msg: TMessage);
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.myWndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then //如果是设备改变消息的话
try
WMDeviceChange(TWMDeviceChange(Msg));
except
Application.HandleException(Self);
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
//如果不是设备改变消息,则交给FORM缺省的消息处理系统完成
end;
procedure TForm1.WMDeviceChange(var Msg : TWMDeviceChange);
var
lpdb : PDEV_BROADCAST_HDR; //
lpdbv : PDEV_BROADCAST_VOLUME;
begin
(* 接受 a wm_devicechange message *)
lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
(* 查看 wm_devicechange 消息中EVENT 是何事件 *)
case Msg.Event of
DBT_DEVICEARRIVAL : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then
begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
begin
//CheckCdRomVolume;我的程序里要检查光盘卷标
//HasCdRom; CDRom被加载
showmessage('有光盘加入进来');
end;
end;
end;
DBT_DEVICEREMOVECOMPLETE : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
//HasNotCdRom; CD Rom被取出
showmessage('CD被取出');
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FWindowHandle:=AllocateHWnd(mywndproc);
//截取WINDOWS交给每个窗口的消息,交给MYWNDPROC处理,用来判断CD驱动器改变
//在WIN95里,每个设备的改变,系统都要发一个消息给所有当前窗口
//所以这里要用自定义的消息循环代替DELPHI 程序默认的窗口消息循环
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
DeallocateHWnd(FWindowHandle);
//释放句柄
end;
Within our message handler procedure we will have to inherit the original handling of the message by calling inherited. If you do not want to inherit the original procedure then do not declare this within the handler procedure. In our case we simply want to test the wParam member of the TMessage, so we will call inherited.
WM_DEVICECHANGE device message notifies an application or device driver of a change to the hardware configuration of a device or the computer. The wParam parameter specifies the event taking place. The lParam parameter is a pointer to a structure that contains the event secific data. Here is a list of the possible wParam values:
DBT_CONFIGCHANGECANCELED a request to change the current configuration (dock or undock) has been canceled.
DBT_CONFIGCHANGED 当前设置已被改变, due to a dock or undock.
DBT_DEVICEARRIVAL 一个设备已被插入,并且现在已可用.
DBT_DEVICEQUERYREMOVE permission is requested to remove a device. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVEFAILED a request to remove a device has been canceled.
DBT_DEVICEREMOVECOMPLETE 一个设备已被移走.
DBT_DEVICEREMOVEPENDING a device is about to be removed. Cannot be denied.
DBT_DEVICETYPESPECIFIC a device-specific event has occurred.
DBT_QUERYCHANGECONFIG permission is requested to change the current configuration (dock or undock).
DBT_USERDEFINED the meaning of this message is user-defined.
The constant values for the wParam member of WM_DEVICECHANGE was found on http://www.microsoft.com
Example 1
{...}type
TForm1 = class(TForm)
private
{ Private declarations }
procedure WMDeviceChange(var Msg: TMessage);
message WM_DEVICECHANGE;
public
{ Public declarations }
end; {...}procedure TForm1.WMDeviceChange(var Msg: TMessage);
const
DBT_QUERYCHANGECONFIG = $0017;
DBT_CONFIGCHANGED = $0018;
DBT_CONFIGCHANGECANCELED = $0019;
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEQUERYREMOVE = $8001;
DBT_DEVICEQUERYREMOVEFAILED = $8002;
DBT_DEVICEREMOVEPENDING = $8003;
DBT_DEVICEREMOVECOMPLETE = $8004;
DBT_DEVICETYPESPECIFIC = $8005;
DBT_USERDEFINED = $FFFF;
var
tmpStr : String;
begin
inherited;
case Msg.wParam of
DBT_DEVICEARRIVAL : tmpStr := 'CD INSERTED!!!';
DBT_DEVICEREMOVECOMPLETE : tmpSTr := 'CD REMOVED!!!';
end;
ShowMessage(tmpStr);
end;{...}