Winfrom 定时锁屏

 #region 锁屏
        public struct LASTINPUTINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }
        [DllImport("user32.dll")]
        public static extern bool GetLastInputInfo(ref    LASTINPUTINFO plii);
        public long getIdleTick()
        {
            LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref    vLastInputInfo)) return 0;
            return Environment.TickCount - (long)vLastInputInfo.dwTime;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            long i = getIdleTick() / 1000;
            if (i > 1200)//(i > 10 * 1000)//超过一分钟
            {
                suoping();
            }
        }
        private void butsp_Click(object sender, EventArgs e)
        {
            suoping();
        }
        void suoping()
        {
            this.timer2.Enabled = false;
            frmpassword frm = new frmpassword();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
                this.timer2.Enabled = true;
            else
                Application.ExitThread();
        }
        #endregion

 

posted @ 2018-12-24 10:08  世人皆萌  阅读(281)  评论(0编辑  收藏  举报