Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1584382
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: LINUX

2014-09-08 21:56:14


点击(此处)折叠或打开

  1. #ifndef WIDGET_H
  2.     #define WIDGET_H
  3.        
  4.     #include <QtGui/QWidget>
  5.       
  6.     #include <windows.h>
  7.     #include <QString>
  8.        
  9.     class Widget : public QWidget
  10.     {
  11.         Q_OBJECT
  12.        
  13.     public:
  14.         Widget(QWidget *parent = 0);
  15.         ~Widget();
  16.        
  17.     protected:
  18.         bool winEvent(MSG *msg, long *result);
  19.        
  20.     private:
  21.         //UsbConfig usbConfig;
  22.         char FirstDriveFromMask (ULONG unitmask);
  23.     };
  24.        
  25.     #endif // WIDGET_H


点击(此处)折叠或打开

  1. #include "qt_Watch/qwidget.h"
  2.     #include <QDebug>
  3.       
  4.     #include <Windows.h>
  5.     #include <dbt.h>
  6.       
  7.     Widget::Widget(QWidget *parent)
  8.         : QWidget(parent)
  9.     {
  10.         setWindowFlags(Qt::Window); //窗口以Window为父窗口
  11.     }
  12.       
  13.     Widget::~Widget()
  14.     {
  15.     }
  16.       
  17.     bool Widget::winEvent(MSG *msg,long *result)
  18.     {
  19.         int msgType = msg->message;
  20.         if(msgType == WM_DEVICECHANGE)
  21.         {
  22.             qDebug() << "收到事件 " ;
  23.             PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
  24.             switch(msg->wParam)
  25.             {
  26.             case DBT_DEVICETYPESPECIFIC:
  27.                 {
  28.                     qDebug() << "DBT_DEVICETYPESPECIFIC " ;
  29.                     break;
  30.                 }
  31.             case DBT_DEVICEARRIVAL:
  32.                 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
  33.                 {
  34.                     PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
  35.                     if (lpdbv -> dbcv_flags == 0)
  36.                     {
  37.                         // 插入U盘,此处可以做你想做的事
  38.                         QString USBDisk = QString(this->FirstDriveFromMask(lpdbv ->dbcv_unitmask));
  39.                         qDebug() << "USB_Arrived and The USBDisk is: "<<USBDisk ;
  40.                     }
  41.                 }
  42.                 qDebug() << "DBT_DEVICEARRIVAL" ;
  43.                 break;
  44.             case DBT_DEVICEREMOVECOMPLETE:
  45.                 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
  46.                 {
  47.                     PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
  48.                     if (lpdbv -> dbcv_flags == 0)
  49.                     {
  50.                         qDebug() << "USB_设备移除";
  51.                     }
  52.                 }
  53.                 qDebug() << "DBT_DEVICEREMOVECOMPLETE" ;
  54.                 break;
  55.             }
  56.         }
  57.         return false;
  58.     }
  59.       
  60.     char Widget::FirstDriveFromMask (ULONG unitmask)
  61.     {
  62.         char i;
  63.       
  64.         for (i = 0; i < 26; ++i)
  65.         {
  66.             if (unitmask & 0x1)
  67.                 break;
  68.             unitmask = unitmask >> 1;
  69.         }
  70.         return (i + 'A');
  71.     }


点击(此处)折叠或打开

  1. #include <QApplication>
  2.     #include "qt_Watch/qwidget.h"
  3.       
  4.     int main(int argc,char ** argv)
  5.     {
  6.         QApplication app(argc,argv);
  7.       
  8.         Widget widget;
  9.         widget.show();
  10.       
  11.         return app.exec();
  12.     }

转载自:http://blog.csdn.net/zhuyingqingfen/article/details/37726981
阅读(9573) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~