光驱弹出、弹入触发哪个事件?怎么写?(10分)

你用程序打开光驱,用spy_++ 去跟踪消息看看
 
我的水平正如我的昵称,谢谢!请再清楚些
 
uses MMSystem;
//打开cd
procedure OpenCDdoor;
begin
MciSendString('Set CdAudio do
or open wait', nil, 0, handle);
end;
//关闭cd
procedure CloseCDdoor;
begin
MciSendString('Set CdAudio do
or closed wait', nil , 0, handle);
end;
 
Windows会发WM_DEVICECHANGE消息
 
spy++ 查了一下,对照pcexplorer 的答案 呵呵 msdn
确实是 WM_DEVICECHANGE ,具体的可以看看msdn,比较详细
 
怎么处理呀?我不知道该怎么写
 
你要去钩那个消息。例子程序:
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 TCDEvents.WMDeviceChange(var Msg : TWMDeviceChange);
var lpdb : PDEV_BROADCAST_HDR;
lpdbv : PDEV_BROADCAST_VOLUME;
begin
(* received a wm_devicechange message *)
lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
(* look at the event send together with the wm_devicechange message *)
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
Application.MessageBox (' CDROM closed.',
'CDROM Information.',
MB_OK);
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
if Assigned(fAfterRemove) then
Application.MessageBox (' CDROM Open.',
'CDROM Information.',
MB_OK);
end;
end;
end;
end;

{ get Handle to a CD-player}
function TCDEvents.GetDevice : word;
var OpenParms : TMCI_Open_Parms;
FID : Word;
begin
if not (csDesigning in ComponentState) then
begin
if fID=0 then
begin
FFlags := 0;
FFlags := mci_notify or mci_open_type or mci_open_shareable;
OpenParms.lpstrDeviceType := 'CDAudio';
OpenParms.dwCallback := 0;
fErrCode := mciSendCommand(0, mci_open, FFlags, Longint(@OpenParms));
if FErrCode = 0 then
{device successfully opened}
begin
fID := OpenParms.wDeviceID;
end;
end;
Result := fID;
end;
end;
{ Eject CDROM}
procedure TCDEvents.OpenDoor;
var
SetParms: TMCI_Set_Parms;
begin
FFlags := 0;
FFlags := mci_notify or mci_set_door_open;
SetParms.dwCallback := 0;
fErrCode := mciSendCommand(GetDevice, mci_Set, FFlags, Longint(@SetParms));
end;
{Close CDROM}
procedure TCDEvents.CloseDoor;
var
SetParm: TMCI_Set_Parms;
begin
FFlags := 0;
FFlags := mci_notify or mci_set_door_closed;
SetParm.dwCallback := 0;
fErrCode := mciSendCommand( GetDevice, mci_Set, FFlags, Longint(@SetParm) );
end;
 
to 人在昆明:
  好人做到底,我怎么编译都通不过?怎么回事?好多错误!可否寄一个通过的例子?
x-delphi@163.com
 
明天啦,睡觉去啦,呵呵,明天还要上班,上班没有事情,无聊呀!
 
手上没光盘,没测试,你可以测试一下,(要放入光盘才能捕获消息)
你还可以好好看看那个const 下面的消息翻译,可以把他们都加到 case 中去看看都是
干什么的。
unit UMsg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
const
//A request to change the current configuration(dock or undock)has been canceled.
DBT_CONFIGCHANGECANCELED = $0019;
//The current configuration has changed, due to a do
ck or undock.
DBT_CONFIGCHANGED = $0018;
// A device has been inserted and is now available.
DBT_DEVICEARRIVAL = $8000;
//Permission is requested to remove a device. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVE = $8001;
//A request to remove a device has been canceled.
DBT_DEVICEQUERYREMOVEFAILED = $8002;
//A device has been removed.
DBT_DEVICEREMOVECOMPLETE = $8004;
// A device is about to be removed. Cannot be denied.
DBT_DEVICEREMOVEPENDING = $8003;
//A device-specific event has occurred.
DBT_DEVICETYPESPECIFIC = $8005;
// Permission is requested to change the current configuration (dock or undock).
DBT_QUERYCHANGECONFIG = $0017;
//The meaning of this message is user - defined.
DBT_USERDEFINED = $FFFF;
type
TForm1 = class(TForm)
private
procedure WndProc(var Message: TMessage);
override;
public
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.WndProc(var Message: TMessage);
begin
case Message.Msg of
// A device is about to be removed. Cannot be denied.
DBT_DEVICEREMOVEPENDING:
begin
ShowMessage('safg');
end;
//Permission is requested to remove a device. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVE:
begin
ShowMessage('REMOVE CD ROM');
end;
end;
inherited WndProc(Message);
end;

end.
 
打开winsight32监控一下就知道是什么消息了
 
to 人在昆明:
  有盘无盘都没有反应呀!
 
其实捕获WM_DEVICECHANGE就行了
procedure WMDevicechange(var Message: TMessage);
message WM_DEVICECHANGE;
procedure TForm1.WMDevicechange(var Message: TMessage);
const
DBT_DEVICEARRIVAL=$8000;
DBT_DEVICEREMOVECOMPLETE=$8004;
begin
inherited;
case Message.WParam of
DBT_DEVICEARRIVAL:
Caption :='有了!';
DBT_DEVICEREMOVECOMPLETE:
Caption :='取走了';
end;
end;
 
多人接受答案了。
 
to wr960204:
如果没盘怎么判断呢?我试了,如果有盘都可以,如果没盘则什么反应也没有!
 
顶部