鼠标或键盘静止超时动作

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace FWM

{

    static class Program

    {

        private static Form1 vf = null;

        private static System.Windows.Forms.Timer timer1 = null;

        /// <summary>   

        /// 应用程序的主入口点。   

        /// </summary>   

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            //启用定时器   

            if (timer1 == null)

            {

                timer1 = new Timer();

            }

            timer1.Interval = 60 * 1000;

            timer1.Tick += new EventHandler(timer1_Tick);

            timer1.Start();

            Application.Run(new Main());

        }

        private static void timer1_Tick(object sender, EventArgs e)

        {

            //判断空闲时间是否超过15分钟,超过则自动弹出视频播放窗口   

            if (GetIdleTick() / 1000 >= 1 * 10)

            {

                ShowVidioForm();

            }

        }

        /// <summary>   

        /// 打开视频播放窗口   

        /// </summary>   

        private static void ShowVidioForm()

        {

            try

            {

                if (vf == null)

                {

                    vf = new Form1();

                }

                vf.ShowDialog();

            }

            catch

            {

            }

        }

        /// <summary>   

        /// 获取鼠标键盘空闲时间   

        /// </summary>   

        /// <returns></returns>   

        public static long GetIdleTick()

        {

            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();

            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);

            if (!GetLastInputInfo(ref lastInputInfo)) return 0;

            return Environment.TickCount - (long)lastInputInfo.dwTime;

        }

        [StructLayout(LayoutKind.Sequential)]

        private struct LASTINPUTINFO

        {

            [MarshalAs(UnmanagedType.U4)]

            public int cbSize;

            [MarshalAs(UnmanagedType.U4)]

            public uint dwTime;

        }

        /// <summary>   

        /// 调用windows API获取鼠标键盘空闲时间   

        /// </summary>   

        /// <param name="plii"></param>   

        /// <returns></returns>   

        [DllImport("user32.dll")]

        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

    }

}

posted @ 2011-09-20 08:51  第7笔画  阅读(284)  评论(0编辑  收藏  举报