您的位置:首页 > 运维架构

原创 Win32窗体工程--监控U盘插入和拔出(1) 收藏

2009-08-13 15:45 471 查看





document.body.oncopy = function() {
if (window.clipboardData) {
setTimeout(function() {
var text = clipboardData.getData("text");
if (text && text.length>300) {
text = text + "/r/n/n本文来自CSDN博客,转载请标明出处:" + location.href;
clipboardData.setData("text", text);
}
}, 100);
}
}

function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}

原理说明:创建一个win32窗体程序。使用全局变量hWnd获取创建窗体的句柄。在winmain函数中创建窗体,同时将盘符改变的消息注册给窗体。在窗体回调函数中添加对盘符改变消息的响应处理即可。

// TestWin32.cpp : Defines the entry point for the application.

//

#include "stdafx.h"

#include "TestWin32.h"

#include <dbt.h>

#include <strsafe.h>

#define MAX_LOADSTRING 100

// Global Variables:

HINSTANCE hInst; // current instance

TCHAR szTitle[MAX_LOADSTRING]; // The title bar text

TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

HWND hWnd; //自定义全局变量,窗体句柄

// Forward declarations of functions included in this code module:

ATOM MyRegisterClass(HINSTANCE hInstance);

BOOL InitInstance(HINSTANCE, int);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

//获取新增的盘符,即usb接口插入设备的盘符 光如此其实还是不够的,最好的就是使用GetDeviceType()函数对盘符加以判断

char FirstDriveFromMask (ULONG unitmask)

{

char i;

for (i = 0; i < 26; ++i)

{

if (unitmask & 0x1)

break;

unitmask = unitmask >> 1;

}

return (i + 'A');

}

///向窗体注册盘符改变 的消息

BOOL DoRegisterDeviceInterface(

GUID InterfaceClassGuid,

HDEVNOTIFY *hDevNotify

)

/*

Routine Description:

Registers for notification of changes in the device interfaces for

the specified interface class GUID.

Parameters:

InterfaceClassGuid - The interface class GUID for the device

interfaces.

hDevNotify - Receives the device notification handle. On failure,

this value is NULL.

Return Value:

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE.

*/

{

DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

char szMsg[80];

ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );

NotificationFilter.dbcc_size =

sizeof(DEV_BROADCAST_DEVICEINTERFACE);

NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

NotificationFilter.dbcc_classguid = InterfaceClassGuid;

*hDevNotify = RegisterDeviceNotification( hWnd,

&NotificationFilter,

DEVICE_NOTIFY_WINDOW_HANDLE

);

if(!*hDevNotify)

{

sprintf(szMsg, "RegisterDeviceNotification failed: %d/n",

GetLastError());

MessageBox(hWnd, szMsg, "Registration", MB_OK);

return FALSE;

}

return TRUE;

}

int APIENTRY _tWinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

UNREFERENCED_PARAMETER(hPrevInstance);

UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.

MSG msg;

HACCEL hAccelTable;

// Initialize global strings

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);

MyRegisterClass(hInstance);

// Perform application initialization:

if (!InitInstance (hInstance, nCmdShow))

{

return FALSE;

}

//定义盘符改变消息的guid,并通过函数向窗体注册

_GUID GUID_DEVINTERFACE_USB_DEVICE;

GUID_DEVINTERFACE_USB_DEVICE.Data1 = 0xA5DCBF10L;

GUID_DEVINTERFACE_USB_DEVICE.Data2 = 0x6530;

GUID_DEVINTERFACE_USB_DEVICE.Data3 = 0x11D2;

GUID_DEVINTERFACE_USB_DEVICE.Data4[0] = 0x90;

GUID_DEVINTERFACE_USB_DEVICE.Data4[1] = 0x1F;

GUID_DEVINTERFACE_USB_DEVICE.Data4[2] = 0x00;

GUID_DEVINTERFACE_USB_DEVICE.Data4[3] = 0xC0;

GUID_DEVINTERFACE_USB_DEVICE.Data4[4] = 0x4F;

GUID_DEVINTERFACE_USB_DEVICE.Data4[5] = 0xB9;

GUID_DEVINTERFACE_USB_DEVICE.Data4[6] = 0x51;

GUID_DEVINTERFACE_USB_DEVICE.Data4[7] = 0xED;

HDEVNOTIFY hDevNotify;

DoRegisterDeviceInterface( GUID_DEVINTERFACE_USB_DEVICE, &hDevNotify);

//WaitForSingleObject(hDevNotify, INFINITE);

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN32));

// Main message loop:

while (GetMessage(&msg, NULL, 0, 0))

{

if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return (int) msg.wParam;

}

//

// FUNCTION: MyRegisterClass()

//

// PURPOSE: Registers the window class.

//

// COMMENTS:

//

// This function and its usage are only necessary if you want this code

// to be compatible with Win32 systems prior to the 'RegisterClassEx'

// function that was added to Windows 95. It is important to call this function

// so that the application will get 'well formed' small icons associated

// with it.

//

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN32));

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTWIN32);

wcex.lpszClassName = szWindowClass;

wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);

}

//

// FUNCTION: InitInstance(HINSTANCE, int)

//

// PURPOSE: Saves instance handle and creates main window

//

// COMMENTS:

//

// In this function, we save the instance handle in a global variable and

// create and display the main program window.

//

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)

{

return FALSE;

}

///显示窗体并刷新

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

return TRUE;

}

//

// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)

//

// PURPOSE: Processes messages for the main window.

//

// WM_COMMAND - process the application menu

// WM_PAINT - Paint the main window

// WM_DESTROY - post a quit message and return

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

int wmId, wmEvent;

PAINTSTRUCT ps;

HDC hdc;

switch (message)

{

case WM_COMMAND:

wmId = LOWORD(wParam);

wmEvent = HIWORD(wParam);

// Parse the menu selections:

switch (wmId)

{

case IDM_ABOUT:

DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

break;

case IDM_EXIT:

DestroyWindow(hWnd);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

break;

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...

EndPaint(hWnd, &ps);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

///////盘符改变的消息响应处理

case WM_DEVICECHANGE:

if(wParam == DBT_DEVICEARRIVAL) //设备激活

{

PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;

PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

char szMsg[80];

sprintf (szMsg, "Drive %c: 被装载/n",

FirstDriveFromMask(lpdbv ->dbcv_unitmask));

MessageBox(hWnd, szMsg, "WM_DEVICECHANGE", MB_OK);

}

else if(wParam == DBT_DEVICEREMOVECOMPLETE)

{

PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;

PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

char szMsg[80];

sprintf (szMsg, "Drive %c: 被卸载/n",

FirstDriveFromMask(lpdbv ->dbcv_unitmask));

MessageBox(hWnd, szMsg, "WM_DEVICECHANGE", MB_OK);

}

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

// Message handler for about box.

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{

UNREFERENCED_PARAMETER(lParam);

switch (message)

{

case WM_INITDIALOG:

return (INT_PTR)TRUE;

case WM_COMMAND:

if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)

{

EndDialog(hDlg, LOWORD(wParam));

return (INT_PTR)TRUE;

}

break;

}

return (INT_PTR)FALSE;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: