掌握最即時的股市資訊

2010年6月20日 星期日

[C#] 取得滑鼠鍵盤的最後使用時間(類似螢幕保護程式功能)

 
今天在工作時  想讓App能具有閒置幾分鐘後 做什麼事的功能~
 
當User控制完Reader Control後 閒置幾分鐘後不動 自動縮到常駐程式裡
 
此方法是利用 User32.dll 裡面的GetLastInputInfo函數 來獲取滑鼠鍵盤的最後使用時間
 
透過取得最後一次時間後 再與目前的系統時間做比較 就可以知道目前系統閒置多久
 
進一步縮小到常駐裡~
 
以下為記錄
 
1:[StructLayout(LayoutKind.Sequential)]   
2: public struct LASTINPUTINOF
3: {
4: [MarshalAs(UnmanagedType.U4)]
5: public int cbSize;
6: [MarshalAs(UnmanagedType.U4)]
7: public uint dwTime;
8: }
9:
10: [DllImport("user32.dll")]
11: public static extern Boolean GetLastInputInfo(ref LASTINPUTINOF plii);
12:
13: private void MainUI_Load(object sender, EventArgs e)
14: {
15: System.Timers.Timer TimeOutCountDown = new System.Timers.Timer();
16: TimeOutCountDown.Enabled = true;
17: TimeOutCountDown.Interval = 1000; //ReadTimeOut為毫秒時間
18: TimeOutCountDown.Elapsed += new ElapsedEventHandler(TimeOutCountDown_Elapsed);
19: }
20:
21: private void TimeOutCountDown_Elapsed(Object sender, ElapsedEventArgs e)
22: {
23: LASTINPUTINOF aa = new LASTINPUTINOF();
24: aa.cbSize = Marshal.SizeOf(aa);
25: int current = Environment.TickCount; //此為系統時間
26: if (GetLastInputInfo(ref aa)) // true 為成功取得最後操作時間 false 為失敗
27: {
28: if (current - (int)aa.dwTime>5000) // 當閒置五秒後則進行以下動作
29: {
30: Resident.Visible = true;
31: Resident.ShowBalloonTip(1000);
32: GetUI();
33: }
34: }
35: }

沒有留言:

張貼留言