| Код | //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components void __fastcall FormCreate(TObject *Sender); private: // User declarations void __fastcall WMDeviceChange(TMessage& Msg); BEGIN_MESSAGE_MAP VCL_MESSAGE_HANDLER(WM_DEVICECHANGE, TMessage, WMDeviceChange) END_MESSAGE_MAP(TForm) public: // User declarations __fastcall TForm1(TComponent* Owner); bool __fastcall RegisterDeviceInterfaceToHwnd(GUID InterfaceClassGuid, HWND hWnd, HDEVNOTIFY *hDeviceNotify); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
|
и
| Код | //---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop #include <Dbt.h> #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; GUID WceusbshGUID = {0x25dbce51,0x6c8f,0x4a72,0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35}; // для PnP HDEVNOTIFY hDeviceNotify; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::WMDeviceChange(TMessage &Msg) { if(Msg.WParam == DBT_DEVICEARRIVAL) ShowMessage("Воткнули"); if(Msg.WParam == DBT_DEVICEREMOVECOMPLETE) ShowMessage("Выткнули");
} void __fastcall TForm1::FormCreate(TObject *Sender) { RegisterDeviceInterfaceToHwnd(WceusbshGUID, Form1->Handle, &hDeviceNotify); } //--------------------------------------------------------------------------- bool __fastcall TForm1::RegisterDeviceInterfaceToHwnd(GUID InterfaceClassGuid, HWND hWnd, HDEVNOTIFY *hDeviceNotify) { DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter) ); NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; NotificationFilter.dbcc_classguid = InterfaceClassGuid;
*hDeviceNotify = RegisterDeviceNotification(hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if(NULL == *hDeviceNotify) return FALSE;
return TRUE; }
|
|