我用下面的程序能监控到光驱、USB设备的插入与拔出,但不知道具体是光驱还是USB设备?怎么半????
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure WMDEVICECHANGE(var msgx: Tmessage); message WM_DEVICECHANGE;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}procedure Tform1.WMDEVICECHANGE(var msgx: Tmessage);
const
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
begin
inherited;
case msgx.WParam of
DBT_DEVICEARRIVAL:
Caption := '有了!';
DBT_DEVICEREMOVECOMPLETE:
Caption := '取走了';
end;
end;end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure WMDEVICECHANGE(var msgx: Tmessage); message WM_DEVICECHANGE;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}procedure Tform1.WMDEVICECHANGE(var msgx: Tmessage);
const
DBT_DEVICEARRIVAL = $8000;
DBT_DEVICEREMOVECOMPLETE = $8004;
begin
inherited;
case msgx.WParam of
DBT_DEVICEARRIVAL:
Caption := '有了!';
DBT_DEVICEREMOVECOMPLETE:
Caption := '取走了';
end;
end;end.
解决方案 »
- 请教精通dll的高手,DLL封装MDI子窗体,如何在窗体关闭时自动卸载dll,以减少内存占用?
- delphier们,锋回路转的时候肯定会来
- 我想请问一下delphi有没有给单元文件或者函数画流程图的工具?
- BitBtn分组的问题
- 如何读取Exel中插入的图片(急)
- [DCC Error] E2597
- Update 2 downloads for Delphi 6 are currently available
- 问你们一个巨深巨难的问题。。。
- 我在NT用中用timetostr()得到一个'上午 12:00:00’这是为何?如何改变?
- 关于TEECHAART。使用ADO时,用ODBC、JET351在X、Y、LABEL中可用字符(如上海、北京等),但JET4.0不可(只有数值、日期)。why?
- 各位朋友,我知道你们只关心技术,其实我也一样
- 用QuickReport做报表在 pagesize中以将报表改成横着的可预览还是竖的。为什么啊?
Value Meaning
DRIVE_UNKNOWN The drive type cannot be determined.
DRIVE_NO_ROOT_DIR The root directory does not exist.
DRIVE_REMOVABLE The disk can be removed from the drive.
DRIVE_FIXED The disk cannot be removed from the drive.
DRIVE_REMOTE The drive is a remote (network) drive.
DRIVE_CDROM The drive is a CD-ROM drive.
DRIVE_RAMDISK The drive is a RAM disk.
var
Drivetype:Integer;
root:String;
i:Integer;
begin for i :=65 to 91 do
begin
root:=char(i)+':\';
Drivetype:=GetDriveType(pChar(root));
case Drivetype of
DRIVE_UNKNOWN : CheckListBox1.Items.Add(root+'未知');
DRIVE_NO_ROOT_DIR:; //不存在
DRIVE_REMOVABLE:CheckListBox1.Items.Add(root+'移动磁盘(包括软盘和U盘等)');
DRIVE_FIXED:CheckListBox1.Items.Add(root+'硬盘');
DRIVE_REMOTE:CheckListBox1.Items.Add(root+'网络磁盘');
DRIVE_CDROM:CheckListBox1.Items.Add(root+'光驱');
DRIVE_RAMDISK:CheckListBox1.Items.Add(root+'虚拟磁盘');
end;
end;
end;
我用你的程序,测试的U盘显示的是硬盘类型,根本无法判断是不是U盘
环境WinXP+D6
//如果哪位试验成功,不要忘了在这儿告知小弟
function TfrmMain.GetLastMoveDrive: String;
var
Drivetype:Integer;
root:String;
i:Integer;
begin
Result:='';
for i :=67 to 91 do //从C:盘开始到Z:盘
begin
root:=char(i)+':\';
Drivetype:=GetDriveType(pChar(root));
if Drivetype=DRIVE_REMOVABLE then
begin
Result:= root;
Break;
end;
end;
end;
但我在朋友的xp上测试也是硬盘类型
Windows; // UNIT, DWORD CONST
// Events of WM_DEVICECHANGE (wParam)
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEQUERYREMOVE = $8001; // wants to remove, may fail
DBT_DEVICEQUERYREMOVEFAILED = $8002; // removal aborted
DBT_DEVICEREMOVEPENDING = $8003; // about to remove, still avail
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVICETYPESPECIFIC = $8005; // type specific event
DBT_CONFIGCHANGED = $0018; // type of device in DEV_BROADCAST_HDR
DBT_DEVTYP_OEM = $00000000; // OEM- or IHV-defined
DBT_DEVTYP_DEVNODE = $00000001; // Devnode number
DBT_DEVTYP_VOLUME = $00000002; // Logical volume
DBT_DEVTYP_PORT = $00000003; // Port (serial or parallel
DBT_DEVTYP_NET = $00000004; // Network resource // media types in DBT_DEVTYP_VOLUME
DBTF_MEDIA = $0001; // change affects media in drive
DBTF_NET = $0002; // logical volume is network volume
TYPE
// Instead of TMessage, use this:
TWMDeviceChange =
RECORD
Msg : Cardinal;
Event : UINT;
dwData : pointer;
Result : LongInt;
END; // In a DBT_DEVICECHANGE or DBT_DEVICEREMOVECOMPLETE event, dwData contains an
// address of a DEV_BROADCAST_HDR structure identifying the device inserted. TDevBroadcastHDR =
PACKED RECORD
dbch_size : DWORD;
dbch_devicetype: DWORD;
dbch_reserved : DWORD;
END;
pDevBroadcastHDR = ^TDevBroadcastHDR; // When the device is of type volume, then we can get some device specific
// information, namely specific information about a logical volume.
TDevBroadcastVolume =
PACKED RECORD
dbcv_size : DWORD;
dbcv_devicetype: DWORD;
dbcv_reserved : DWORD;
dbcv_unitmask : DWORD;
dbcv_flags : WORD;
END;
pDevBroadcastVolume = ^TDevBroadcastVolume;IMPLEMENTATIONEND.硬件设置发生变化时,系统将会向所有应用程序发送一条WM_DEVICECHANGE消息.当有设备被禁止或激活时(比如我们插入光盘,从光驱中取出光盘,或者在系统设备中删除或禁止打印端口),该消息的wParam参数分别被指定为DBT_DEVICEARRIVAL和DBT_DEVICEREMOVECOMPLETE.此时,lParam参数指向一个DEV_BROADCAST_HDR结构,该结构中的dbch_devicetype成员表识设备的类型,当该值为DBT_DEVTYP_VOLUME时,表明一个带有卷标名的驱动器发生了变化,一般情况下该驱动器既为光驱.
http://soft.56kc.com/ ->delphi原码-》硬件控制串口重发事例 jpyc 9 2004-8-19
串口通讯协议分析事例 jpyc 45 2004-7-6
USB访问的源代码 www 801 2004-6-18
鼠标键盘动作记录及播放程序源代码 www 99 2004-2-27
并口控制(98/2000/xp)