首页 > 代码库 > 获取键盘或鼠标多久没有对屏幕进行操作了

获取键盘或鼠标多久没有对屏幕进行操作了

屏幕保护相关代码:

  1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Linq;  7 using System.Text;  8 using System.Windows.Forms;  9 using System.Threading; 10 using System.Runtime.InteropServices; 11 using WindowsFormsApplication1.Bus; 12  13 namespace WindowsFormsApplication1 14 { 15     public partial class Screen : Form 16     { 17         public Screen() 18         { 19             InitializeComponent(); 20         } 21  22         private static Screen scr = null; 23  24         /// <summary> 25         /// 屏保窗体 26         /// </summary> 27         /// <returns></returns> 28         public static Screen GetScreen() { 29             if (scr == null) { 30                 scr = new Screen(); 31             } 32             return scr; 33         } 34  35         ProductInfoWS.ProductInfoService ws = new WindowsFormsApplication1.ProductInfoWS.ProductInfoService(); 36  37        // KeyboardHook keyHook = new KeyboardHook();   //键盘钩子 38  39         public string FormNum =string.Empty;   40  41         Thread t;  //定义一个线程 42         private void Screen_Load(object sender, EventArgs e) 43         { 44    45             Max(); 46             t = new Thread(new ThreadStart(RunImage)); 47             t.Start();             //启动线程 48             timer1.Start(); 49  50         } 51  52     53  54         DataTable dt = null; 55       56         string aa = "227"; 57         //图片轮播方法 58         public void RunImage()    //换图片的方法   59         { 60            61             string xml=ws.GetImage(Common.DeptID); 62  63             if (!string.IsNullOrEmpty(xml)) 64             { 65                 dt = UserBus.ConvertXMLToDataSet(xml).Tables[0]; 66             } 67  68  69             if (dt != null && dt.Rows.Count > 0) 70             { 71                 while (true)      //循环```` 72                 { 73                     for (int i = 0; i < dt.Rows.Count; i++) 74                     { 75                         pictureBox1.ImageLocation = dt.Rows[i]["ImageURL"].ToString(); 76                        // string ccc = dt.Rows[i]["Minute"].ToString(); 77                         Thread.Sleep(2000);            //换一次图片让线程休息多少时间具体修改里面的参数例如一秒换一次填1000 78                     } 79                 } 80  81             } 82             else  83             { 84                 pictureBox1.ImageLocation = "http://erp.ozz99.com.cn/SCImg/新浪微博登陆1.jpg"; 85             } 86         } 87  88  89  90         [StructLayout(LayoutKind.Sequential)] 91         struct LASTINPUTINFO 92         { 93             [MarshalAs(UnmanagedType.U4)] 94             public int cbSize; 95             [MarshalAs(UnmanagedType.U4)] 96             public uint dwTime; 97         } 98         [DllImport("user32.dll")] 99         static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);100         /// <summary>101         ///获取键盘和鼠标没有操作的时间方法102         /// </summary>103         /// <returns></returns>104         static long GetLastInputTime()105         {106             LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();107             vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);108             if (!GetLastInputInfo(ref vLastInputInfo)) 109                 return 0;110             return Environment.TickCount - (long)vLastInputInfo.dwTime;111 112         }113 114        115         private void Screen_Deactivate(object sender, EventArgs e)116         {117 118 119 120         }121 122       123         public void timer1_Tick(object sender, EventArgs e)124         {125 126 127             if (dt != null && dt.Rows.Count > 0)128             {129 130 131                 if (GetLastInputTime() > (Convert.ToInt32(dt.Rows[0]["Minute"].ToString()) * 60000))132                 {133                     this.TopMost = true;134                     this.WindowState = FormWindowState.Maximized;135 136                 }137 138             }139             else140             {141 142                 if (GetLastInputTime() >60000)143                 {144                     this.TopMost = true;145                     this.WindowState = FormWindowState.Maximized;146 147                 }148                149             }150 151 152 153 154         }155 156       157         private void Screen_KeyDown(object sender, KeyEventArgs e)158         {159             this.TopMost = false;160             this.WindowState = FormWindowState.Minimized;161             162         }163  164 165         //窗体最大化166         public void Max() {167 168            // this.Opacity = 0.9;169             if (this.WindowState == FormWindowState.Maximized)170             {171                 this.WindowState = FormWindowState.Normal;172             }173             else174             {175                 this.FormBorderStyle = FormBorderStyle.None;176                 this.WindowState = FormWindowState.Maximized;177             }178         179         }180 181         /// <summary>182         /// 鼠标点击事件183         /// </summary>184         /// <param name="sender"></param>185         /// <param name="e"></param>186         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)187         {188             this.TopMost = false;189             this.WindowState = FormWindowState.Minimized;190  191 192         }193 194 195 196 197     }198 }

 1. 让屏幕永远在最前面:TopMost;

 2. 接口写好后,需要更新引用;

 3. TextBox密码显示:PasswordChar为*;

获取键盘或鼠标多久没有对屏幕进行操作了