博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WM手机,如何实现震动

Posted on 2009-05-20 22:39  脚印  阅读(947)  评论(0编辑  收藏  举报

public class LedLib
    {
        private int m_count;
        private const int NLED_COUNT_INFO_ID = 0;
        private const int NLED_SUPPORTS_INFO_ID = 1;

        public LedLib()
        {
            NLED_COUNT_INFO pOutput = new NLED_COUNT_INFO();
            if (!NLedGetDeviceCount(0, ref pOutput))
            {
                throw new Exception("震动模块初始化错误!");
            }
            this.m_count = (int)pOutput.cLeds;
        }

        [DllImport("coredll.dll", EntryPoint = "NLedGetDeviceInfo")]
        private static extern bool NLedGetDeviceCount(short nID, ref NLED_COUNT_INFO pOutput);
        [DllImport("coredll.dll", EntryPoint = "NLedGetDeviceInfo")]
        private static extern bool NLedGetDeviceSupports(short nID, ref NLED_SUPPORTS_INFO pOutput);
        [DllImport("coredll.dll")]
        private static extern bool NLedSetDevice(short nID, ref NLED_SETTINGS_INFO pOutput);
        public void SetLedStatus(uint led, LedState newState)
        {
            NLED_SETTINGS_INFO pOutput = new NLED_SETTINGS_INFO();
            pOutput.LedNum = led;
            pOutput.OffOnBlink = (int)newState;
            bool flag = NLedSetDevice(2, ref pOutput);
        }

        public enum LedState
        {
            Off,
            On,
            Blink
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct NLED_COUNT_INFO
        {
            public uint cLeds;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct NLED_SETTINGS_INFO
        {
            public uint LedNum;
            public int OffOnBlink;
            public int TotalCycleTime;
            public int OnTime;
            public int OffTime;
            public int MetaCycleOn;
            public int MetaCycleOff;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct NLED_SUPPORTS_INFO
        {
            public uint LedNum;
            public int lCycleAdjust;
            public bool fAdjustTotalCycleTime;
            public bool fAdjustOnTime;
            public bool fAdjustOffTime;
            public bool fMetaCycleOn;
            public bool fMetaCycleOff;
        }
    }

        /// <summary>
        /// 实现震动
        /// </summary>
        /// <param name="setTimes">震动毫秒数</param>
        public static void Play(int setTimes)
        {
            try
            {
                LedLib led = new LedLib();
                led.SetLedStatus(1, LedLib.LedState.On);
                Thread.Sleep(setTimes);
                led.SetLedStatus(1, LedLib.LedState.Off);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

 

转:http://blog.csdn.net/zhaojiangang/archive/2009/01/12/3760993.aspx