我想实现这么个功能,鼠标在FORM中不动5秒后,就隐藏掉。然后移动鼠标就显示。
请问怎么完成?
3Q!

解决方案 »

  1.   

    我现在想问的是为何鼠标显示的过程中,如果鼠标在控件上,就不能显示,
    要把鼠标移到FORM中才可以!
    为什么呢?
    3Q!
      

  2.   

    API:
           
    [DllImport("user32.dll" , EntryPoint = "ShowCursor" , CharSet = CharSet.Auto)]
    public static extern void ShowCursor(int status);status:0/1 :隐藏/显示
      

  3.   

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WindowsApplication5
    {
        public partial class Form1 : Form
        {
            /// <summary>
            /// 获取鼠标闲置时间
            /// </summary>
            [StructLayout(LayoutKind.Sequential)]
            public struct LASTINPUTINFO
            {
                [MarshalAs(UnmanagedType.U4)]
                public int cbSize;
                [MarshalAs(UnmanagedType.U4)]
                public uint dwTime;
            }
            /// <summary>
            /// 获取鼠标闲置时间
            /// </summary>
            /// <param name="plii"></param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern bool GetLastInputInfo(ref   LASTINPUTINFO plii);        /// <summary>
            /// 设置鼠标状态的计数器(非状态)
            /// </summary>
            /// <param name="bShow">状态</param>
            /// <returns>状态技术</returns>
            [DllImport("user32.dll", EntryPoint = "ShowCursor", CharSet = CharSet.Auto)]
            public static extern int ShowCursor(bool bShow);
            public Form1()
            {
                InitializeComponent();
                //定时期
                System.Windows.Forms.Timer timer = new Timer();
                timer.Enabled = true;
                timer.Interval = 100;
                timer.Tick += new EventHandler(timer_Tick);        }
            //鼠标状态计数器
            int iCount = 0;
            void timer_Tick(object sender, EventArgs e)
            {
                //鼠标状态计数器>=0的情况下鼠标可见,<0不可见,并不是直接受api函数影响而改变
                long i=getIdleTick() ;
                if (i > 5000)
                {
                    while (iCount >= 0)
                    {
                        iCount=ShowCursor(false);
                    }
                }
                else
                {
                    while (iCount < 0)
                    {
                        iCount = ShowCursor(true);
                    }
                }
            }
            /// <summary>
            /// 获取闲置时间
            /// </summary>
            /// <returns></returns>
            public long getIdleTick()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref   vLastInputInfo)) return 0;
                return Environment.TickCount - (long)vLastInputInfo.dwTime;
            }       
        }
    }
      

  4.   

                Cursor.Hide();
                Cursor.Show();