WM_DEVICECHANGE的使用方式有二種,一種是透過WindowProc()的方式,一種是直接在程式裡加上OnDeviceChange()的函數,底下就先介紹第二種方式。
1. 新增消息 -- 在Dialog的.cpp裡加上藍色部份:
BEGIN_MESSAGE_MAP(CDeviceMonitorDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()
2.新增一個函數 -- 這是Microsoft所提供的函數型態
DECLARE_MESSAGE_MAP()
public:
afx_msg BOOL OnDeviceChange(UINT nEventType,DWORD_PTR dwData);
3.函数內容的实现
BOOL OnDeviceChange(UINT nEventType,DWORD_PTR dwData)
{
switch(nEventType)
{
case DBT_DEVICEARRIVAL:
return TRUE;
}
}
4. 在MSDN上的說明
afx_msg BOOL OnDeviceChange(
UINT nEventType,
DWORD_PTR dwData
);
Parameters
nEventType
- An event type. See the Remarks section for a description of the available values dwData
- The address of a structure that contains event-specific data. Its meaning depends on the given event.
Remarks
For devices that offer software-controllable features, such as ejection and locking, the operating system typically sends aDBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully.
If the operating system forcefully removes of a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so.
The nEvent parameter can be one of these values:
- DBT_DEVICEARRIVAL A device has been inserted and is now available.
- DBT_DEVICEQUERYREMOVE Permission to remove a device is requested. Any application can deny this request and cancel the removal.
- DBT_DEVICEQUERYREMOVEFAILED Request to remove a device has been canceled.
- DBT_DEVICEREMOVEPENDING Device is about to be removed. Cannot be denied.
- DBT_DEVICEREMOVECOMPLETE Device has been removed.
- DBT_DEVICETYPESPECIFIC Device-specific event.
- DBT_CONFIGCHANGED Current configuration has changed.
- DBT_DEVNODES_CHANGED Device node has changed.
Note This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.