VM中實現手機震動功能的類

freshairpeng發表於2009-03-04

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace AnotherTodayScreenItem.Utl
{
    ///


    /// 手機震動功能
    ///

    class ShakeOperation
    {
        /*
          利用C#開發Pocket PC程式,想在程式裡呼叫裝置的震動功能,至少再wm5中還沒有這樣的API,上網查了不少例子,都只能再c++下呼叫。既然這樣,那唯一的辦法是運用P/Invoke了,在“coredll.dll”中有這樣兩個函式
        BOOL WINAPI NLedGetDeviceInfo( UINT nInfoId, void *pOutput );
        BOOL WINAPI NLedSetDevice( UINT nDeviceId, void *pInput );
        NLedGetDeviceInfo是獲得LED數量,NLedSetDevice是來設定LED狀態的,我們只有通過它來啟動或者關閉Pocket PC裝置的震動與否。
        說明一下:一般PPC裝置都有兩個LED,一個就是揚聲器0(Radio LED),另一個則是振動器1(Vibrator)了。
        在我的PPC裝置上發現第二個是Vibrator,不知道是不是所有的PPC都是這樣子的。後來查到“On the HTC Himalaya the vibration device is implemented at index 1。”也就是HTC核心的都是1.*/

        class NLED_SETTINGS_INFO
        {
            public uint LedNum;
            public uint OffOnBlink;
            public int TotalCycleTime;
            public int OnTime;
            public int OffTime;
            public int MetaCycleOn;
            public int MetaCycleOff;
        }

        class NLED_COUNT_INFO
        {
            public int cLeds;
        }

        const int NLED_COUNT_INFO_ID = 0;
        const int NLED_SETTINGS_INFO_ID = 2;

        //振動器狀態
        public enum Status
        {
            FF = 0,
            ON,
            BLINK
        }

        /**/
        ///


            /// 獲得LED個數
            ///

            [DllImport("coredll.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode,
                            EntryPoint = "NLedGetDeviceInfo", PreserveSig = true, SetLastError = true)]
            extern static bool NLedGetDeviceInfo(uint nID, NLED_COUNT_INFO pOutput);


            /**/
            ///


            /// 設定LED狀態
            ///

            [DllImport("coredll.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode,
                   EntryPoint = "NLedSetDevice", PreserveSig = true, SetLastError = false)]
            extern static bool NLedSetDevice(uint nID, NLED_SETTINGS_INFO pOutput);

            /**/
            ///


            /// 獲得LED個數
            ///

            public int GetLedCount()
            {
                int wCount = 0;
                NLED_COUNT_INFO nci = new NLED_COUNT_INFO();
                if (NLedGetDeviceInfo(NLED_COUNT_INFO_ID, nci))
                {
                    wCount = nci.cLeds;
                }
                return wCount;
            }

            /**/
            ///


            /// 設定LED狀態
            ///

            /// Led(1/0),一般是1,即第二個LED
            /// 狀態
            public void SetLedStatus(int wLed, Status wStatus)
            {
                NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();
                nsi.LedNum = (uint)wLed;
                nsi.OffOnBlink = (uint)wStatus;
                NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
            }

            /**/
            ///


            /// 設定LED狀態
            ///

            /// Led(1/0)
            /// 狀態
            /// 持續時間
            public void SetLedStatus(int wLed, Status wStatus, int millisecondsTimeout)
            {
                this.SetLedStatus(wLed, wStatus);
                System.Threading.Thread.Sleep(millisecondsTimeout);
                //關閉震動
                NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();
                nsi.LedNum = (uint)wLed;
                nsi.OffOnBlink = (uint)Status.OFF;
                NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
            }

            /**/
            ///


            /// 設定LED狀態
            ///

            /// 狀態
            public void SetLedStatus(Status wStatus)
            {
                NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();
                nsi.OffOnBlink = (uint)wStatus;
                //自動查詢震動LED
                for (int i = 0; i < this.GetLedCount(); i++)
                {
                    nsi.LedNum = (uint)i;
                    NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
                }
            }

            /**/
            ///


            /// 設定LED狀態
            ///

            /// 狀態
            /// 持續時間
            public void SetLedStatus(Status wStatus, int millisecondsTimeout)
            {
                this.SetLedStatus(wStatus);
                System.Threading.Thread.Sleep(millisecondsTimeout);
                //關閉震動
                NLED_SETTINGS_INFO nsi = new NLED_SETTINGS_INFO();
                nsi.OffOnBlink = (uint)Status.OFF;
                for (int i = 0; i < this.GetLedCount(); i++)
                {
                    nsi.LedNum = (uint)i;
                    NLedSetDevice(NLED_SETTINGS_INFO_ID, nsi);
                }
            }

            /**/
            ///


            /// 迴圈震動
            ///

            /// 次數
            public void CycleVibrate(int times)
            {
                for (int i = 0; i < times; i++)
                {
                    this.SetLedStatus(Status.ON);
                    Thread.Sleep(400);
                    this.SetLedStatus(Status.OFF);
                    Thread.Sleep(200);
                }
            }
    }

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14766526/viewspace-561382/,如需轉載,請註明出處,否則將追究法律責任。

相關文章