名称"WM_DEVICECHANGE"在当前上下文中不存在 [英] The name 'WM_DEVICECHANGE' does not exist in the current context

查看:121
本文介绍了名称"WM_DEVICECHANGE"在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测USB到达事件.我试图覆盖wndproc()来获取消息.但是我正面临Windows消息的错误.

I am trying to detect a usb arrival event . I tried to override wndproc() for getting my messages. But I am facing an error by windows messages.

错误是:

The name 'WM_DEVICECHANGE' does not exist in the current context

The name 'DBT_DEVICEARRIVAL' does not exist in the current context

这也是我尝试过的代码.

Also this is the code I am tried.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;         
using System.IO;
using Microsoft.Win32.SafeHandles; 

namespace USBCheckerApp
{
    public partial class Form1 : Form
    {
        bool bDeviceFound = false;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!bDeviceFound)
            {
                button1.Enabled = false;
            }


        }
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_DEVICECHANGE:
                    if (m.WParam == DBT_DEVICEARRIVAL)
                    {
                        MessageBox.Show("MEDIA FOUND");
                    }
            }

        }

    }
}

已添加,因此您可以在其中建议任何更新. 谢谢

Added so that you could suggest any updations in the same. Thanks

推荐答案

您必须声明并定义常量的值:

You have to declare and define the values of the constants:

private const int DBT_DEVICEARRIVAL = 0x8000;
private const int WM_DEVICECHANGE = 0x0219;

这篇关于名称"WM_DEVICECHANGE"在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆