首页 > CSharp > 通过C#实现报警续

通过C#实现报警续

2009年4月17日

前一篇c#蜂鸣报警系列声音函数
今天要说的是如何实现连续播放报警声音和停止报警.

        #region 报警
        [DllImport("winmm.dll", EntryPoint = "PlaySound")]
        private static extern bool Win32_PlaySound(string pszSound, IntPtr hmod, uint fdwSound);   
 
        /// <summary>
        /// 播放一个wav音频文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="asynchronous"></param>
        /// <param name="loop"></param>
        /// <param name="doNotStopPlay"></param>
        public static void PlaySound(string path, bool asynchronous, bool loop, bool doNotStopPlay)
        {
            Win32_PlaySound(path, IntPtr.Zero, (uint)((asynchronous ?
          PlaySoundMessage.SND_ASYNC : PlaySoundMessage.SND_SYNC) | (loop ?
          PlaySoundMessage.SND_LOOP : 0) | (doNotStopPlay ?
          PlaySoundMessage.SND_NOSTOP : 0) | PlaySoundMessage.SND_FILENAME));
        }
 
        /// <summary>
        /// 停止播放
        /// </summary> 
        public static void StopSound()
        {
            Win32_PlaySound(null, IntPtr.Zero, 0);
        }
 
        [Flags()]
        internal enum PlaySoundMessage
        {
            SND_SYNC = 0x0000,
            SND_ASYNC = 0x0001,
            SND_LOOP = 0x0008,
            SND_NOSTOP = 0x0010,
            SND_FILENAME = 0x00020000
        }
        #endregion

当例如遥测水位中测量的水位大于预警水位, 通过PlaySound(”alarm8.wav”, true, true, false)方法实现连续报警, 你还可以同时弹出窗口提醒之类的信息.
关闭报警通过方法StopSound()实现就可以了.

admin CSharp

  1. 2009年4月20日10:39 | #1

    呵呵,我是初学者,虽然现在贵搏的文章对我来说很生涩,不过这是每个学习过程中都要经历的阶段

  1. 本文目前尚无任何 trackbacks 和 pingbacks.