主题 : 蜂鸣器C#的源代码(CE6) 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 11287
精华: 0
发帖: 25
金钱: 140 两
威望: 28 点
贡献值: 0 点
综合积分: 50 分
注册时间: 2009-12-03
最后登录: 2013-03-05
楼主  发表于: 2010-06-22 10:19

 蜂鸣器C#的源代码(CE6)

老外的网搜来的!!!!


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace SmartDeviceProject6
{
  
    public partial class Form1 : Form
    {
        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern int DeviceIoControlCE(int hDevice, int dwIoControlCode, byte[] lpInBuffer, int nInBufferSize, byte[] lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);

        [DllImport("coredll", SetLastError = true)]
        private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

        private static IntPtr _pwmFile = CreateFile("PWM1:", 0x40000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
        
        public Form1()
        {
            InitializeComponent();
            
        }

        public static void Beep(uint Frequency, int DurationMS)
        {
        
            uint frequency = Frequency;
            byte[] buffer = new byte[4];
            int accessType = 2;
            buffer[0] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[1] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[2] = (byte)(frequency & 0xff);
            frequency = frequency >> 8;
            buffer[3] = (byte)(frequency & 0xff);
            DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
           // var t = new System.Threading.Timer(TurnOff, null, DurationMS, System.Threading.Timeout.Infinite);
        }

        private static void TurnOff(Object obj)
        {
            byte[] buffer = new byte[4];
            int accessType = 1;
            DeviceIoControlCE((int)_pwmFile, accessType, buffer, 4, buffer, 0, ref accessType, IntPtr.Zero);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Beep(200, 20000);
        
        }
}
}
级别: 新手上路
UID: 23286
精华: 0
发帖: 3
金钱: 15 两
威望: 3 点
贡献值: 0 点
综合积分: 6 分
注册时间: 2010-06-17
最后登录: 2010-06-24
1楼  发表于: 2010-06-24 16:34
有C#捕获 用户按键状态的代码 吗?