给你转帖一个吧,其实我也不会,一起进步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;

解决方案 »

  1.   

    CDROM: Detect CD Inserted or Removed07/05/1999This tip is by request as I am going through a small series on CDROM. Detecting a CD going into or coming out of your CDROM is accomplished by checking the WM_DEVICECHANGE Windows message. I have read of a few different ways of accopmplishing this task, but checking this message seemed to be the easiest for me. If you read the WIN32 API help file you will notice that this message has specific wParam values and if you try to find them declared in Delphi you will not find them. We will be declaring them as constants within our message procedure. To understand what we are doing you will need to understand message handling. Message handlers are procedures that create responses to Window's dispatched messages. You can do this on a VCL level or application level. In past tips we have created an Application.OnMessage handler. Today we will show how to create a TForm message handler to a specific message, WM_DEVICECHANGETo start, a message handler is created by including the message directive in a procedure declaration, followed by an integer constant which specifies the message ID. Most of these constants can be found in the Delphi Windows.pas file. The declared procedure must have a single variable parameter of type TMessage or a specific message type if one is defined in the Windows.pas file. Look at the declaration below to see how it must be setup. 
    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;{...}
      

  2.   

    win95系统的硬件设置发生变化时,系统将会向所有应用程序发送一条WM_DEVICECHANGE消息.当有设备被禁止或激活时(比如我们插入光盘,从光驱中取出光盘,或者在系统设备中删除或禁止打印端口),该消息的wParam参数分别被指定为DBT_DEVICEARRIVAL和DBT_DEVICEREMOVECOMPLETE.此时,lParam参数指向一个DEV_BROADCAST_HDR结构,该结构中的dbch_devicetype成员表识设备的类型,当该值为DBT_DEVTYP_VOLUME时,表明一个带有卷标名的驱动器发生了变化,一般情况下该驱动器既为光驱.
      

  3.   

    有控件!http://vcl.vclxx.org/DELPHI/D32FREE/DISKCAPS.ZIP 此控件还可获得关于当前光盘的很多有关信息,如:卷标