Hide mouse cursor after an idle time

前端 未结 3 1679
南笙
南笙 2020-12-02 19:25

I want to hide my mouse cursor after an idle time and it will be showed up when I move the mouse. I tried to use a timer but it didn\'t work well. Can anybody help me? Pleas

3条回答
  •  情话喂你
    2020-12-02 19:54

    Need to account for Environment.Tickcount being negative:

    public static class User32Interop
    {
    
        public static TimeSpan GetLastInput()
        {
            var plii = new LASTINPUTINFO();
            plii.cbSize = (uint)Marshal.SizeOf(plii);
    
            if (GetLastInputInfo(ref plii))
            {
                int idleTime = unchecked(Environment.TickCount - (int)plii.dwTime);
                return TimeSpan.FromMilliseconds(idleTime);
            }
            else
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    
        struct LASTINPUTINFO
        {
            public uint cbSize;
            public uint dwTime;
        }
    }
    

提交回复
热议问题