求高手帮忙解决个困扰已久的问题~!怎么c#怎么判断电脑是否插入耳机
就是实现 一个button 初始为不可点击 然后程序自动查找电脑中是否插入耳机 是否有耳麦 如果有 button 将可点击 ~!??求高手 帮忙 ·······
typedef struct _DEV_BROADCAST_HDR { DWORD dbch_size; DWORD dbch_devicetype; DWORD dbch_reserved; } DEV_BROADCAST_HDR, *PDEV_BROADCAST_HDR;
typedef struct _DEV_BROADCAST_VOLUME { DWORD dbcv_size; DWORD dbcv_devicetype; DWORD dbcv_reserved; DWORD dbcv_unitmask; WORD dbcv_flags; } DEV_BROADCAST_VOLUME, *PDEV_BROADCAST_VOLUME;其中dbcv_unitmask 字段表示当前改变的驱动器掩码,第一位表示驱动器号A,第二位表示驱动器号B,第三位表示驱动器号C,以此类推…… dbcv_flags 表示驱动器的类别,如果等于1,则是光盘驱动器;如果是2,则是网络驱动器;如果是硬盘、U盘则都等于0
using System; using System.Runtime.InteropServices; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UDiskDetect { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } [StructLayout(LayoutKind.Sequential)] struct DEV_BROADCAST_HDR { public UInt32 dbch_size; public UInt32 dbch_devicetype; public UInt32 dbch_reserved; } [StructLayout(LayoutKind.Sequential)] struct DEV_BROADCAST_VOLUME { public UInt32 dbcv_size; public UInt32 dbcv_devicetype; public UInt32 dbcv_reserved; public UInt32 dbcv_unitmask; public UInt16 dbcv_flags; } protected override void DefWndProc(ref Message m) { if (m.Msg == 0x0219)//WM_DEVICECHANGE { switch (m.WParam.ToInt32()) { case 0x8000://DBT_DEVICEARRIVAL { DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME { DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); if (dbv.dbcv_flags == 0) AddVolumes(GetVolumes(dbv.dbcv_unitmask)); } break; } case 0x8004://DBT_DEVICEREMOVECOMPLETE { DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR)); if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME { DEV_BROADCAST_VOLUME dbv = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME)); if (dbv.dbcv_flags == 0) RemoveVolumes(GetVolumes(dbv.dbcv_unitmask)); } break; } } } base.DefWndProc(ref m); } /**//**//**//// <summary> /// 根据驱动器掩码返回驱动器号数组 /// </summary> /// <param name="Mask">掩码</param> /// <returns>返回驱动器号数组</returns> public static char[] GetVolumes(UInt32 Mask) { List<char> Volumes = new List<char>(); for (int i = 0; i < 32; i++) { uint p = (uint)Math.Pow(2, i); if ((p | Mask) == p) { Volumes.Add((char)('A' + i)); } } return Volumes.ToArray(); } public void AddVolumes(char[] Volumes) { foreach (char volume in Volumes) listBox1.Items.Add(volume); } public void RemoveVolumes(char[] Volumes) { foreach (char volume in Volumes) listBox1.Items.Remove(volume); } } }