C#如何拦截WndProc(ref Message m)的消息?就是当我插入USB时...不会执行系统提示...而执行自己写的程序,就想键盘HOOK那样的效果.要用到什么API函数吗?

解决方案 »

  1.   

    C#如何拦截WndProc(ref Message m)  重载这个函数就是其它的问题还回答不上来
      

  2.   


    我重载了啊...
    但是...每当我插入USB时...他一样还是如我的例子:
    protected override void WndProc(ref Message m)
    {
    if ((int)m.WParam == WM_DEVICEARRVIAL)
    {
    // MessageBox.Show("普通到来USB");
    data.USB_path=get_USB_path();
    data.dev_Exist=true;
    string usb_password=data.USB_path+"PassWord";
    string usb_md5=data.USB_path+"MD5_file";
    Judgment_USB_psw(usb_password,usb_md5);
    }
    if ((int)m.WParam == WM_DEVICEARRVIAL&&data.U_key==false)
    {
    // MessageBox.Show("普通到来USB");
    data.USB_path=get_USB_path();
    data.dev_Exist=true;
    }
    else if((int)m.WParam==WM_DEVICEREMOVE&&data.U_key==false)
    {
    // MessageBox.Show("优盘拔开了");
    // MessageBox.Show("普通移除USB");
    data.USB_path="";
    data.dev_Exist=false;
    // Unlock();
    }
    //USB的arrival or remove
    if ((int)m.WParam == WM_DEVICEARRVIAL&&data.U_key&&data.isHavestop==false)
    {
    // MessageBox.Show("优盘插入了");
    //解锁
    // MessageBox.Show("U-KEY到来");
    data.USB_path=get_USB_path();
    Unlock();
    }
    else if((int)m.WParam==WM_DEVICEREMOVE&&data.U_key)
    {
    // MessageBox.Show("优盘拔开了");
    //开启锁
    data.USB_path="";
    // MessageBox.Show("U-KEY移除USB");
    startsaver();
    }
    else if(((int)m.WParam != WM_DEVICEARRVIAL)&&((int)m.WParam!=WM_DEVICEREMOVE)&&data.U_key&&(data.isPasstheword==false)&&(data.isHavestart==false))
    {
    MessageBox.Show("U-KEY下USB不在");
    startsaver();
    }
    contextMenu1.MenuItems[0].Enabled=true;
    base.WndProc(ref m);
    }是否base.WndProc(ref m);的问题?我不知道这个base.WndProc(ref m);会起到啥作用...调用base类的WndProc?啥意思?base是指啥?
      

  3.   

    你写的基本没错。至于程序没反应,我估计是你的if中的那些个常量没定义的事儿。        public const int WM_DEVICECHANGE = 0x219;
            public const int DBT_DEVICEARRIVAL = 0x8000;
            public const int DBT_CONFIGCHANGECANCELED = 0x0019;
            public const int DBT_CONFIGCHANGED = 0x0018;
            public const int DBT_CUSTOMEVENT = 0x8006;
            public const int DBT_DEVICEQUERYREMOVE = 0x8001;
            public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
            public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
            public const int DBT_DEVICEREMOVEPENDING = 0x8003;
            public const int DBT_DEVICETYPESPECIFIC = 0x8005;
            public const int DBT_DEVNODES_CHANGED = 0x0007;
            public const int DBT_QUERYCHANGECONFIG = 0x0017;
            public const int DBT_USERDEFINED = 0xFFFF;至于那个base,是基类,你重载了这个方法,用完了消息还要往下传啊,传给基类,要不然别的程序就收不到这个消息了。就像击鼓传花一样。好老的贴啊,一年前的了。