■ 사용자 비활성을 감지하는 방법을 보여준다.
▶ LASTINPUTINFORMATION.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 마지막 입력 정보 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct LASTINPUTINFORMATION { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 크기 /// </summary> public uint Size; /// <summary> /// 시간 /// </summary> public uint Time; #endregion } } |
▶ InactiveTimeDetector.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비활성 시간 탐지자 /// </summary> public class InactiveTimeDetector { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 마지막 입력 정보 구하기 - GetLastInputInfo(lastInputInformation) /// <summary> /// 마지막 입력 정보 구하기 /// </summary> /// <param name="lastInputInformation">마지막 입력 정보</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool GetLastInputInfo(ref LASTINPUTINFORMATION lastInputInformation); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 비활성 시간 구하기 - GetInactiveTime() /// <summary> /// 비활성 시간 구하기 /// </summary> /// <returns>비활성 시간</returns> public TimeSpan? GetInactiveTime() { LASTINPUTINFORMATION lastInputInformation = new LASTINPUTINFORMATION(); lastInputInformation.Size = (uint)Marshal.SizeOf(lastInputInformation); if(GetLastInputInfo(ref lastInputInformation)) { return TimeSpan.FromMilliseconds(Environment.TickCount - lastInputInformation.Time); } else { return null; } } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
using System; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 비활성 시간 탐지자 /// </summary> private InactiveTimeDetector detector; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.detector = new InactiveTimeDetector(); this.timer.Tick += timer_Tick; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 타이머 틱 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { TimeSpan? inactiveTimeSpan = this.detector.GetInactiveTime(); if(inactiveTimeSpan == null) { this.messageLabel.Text = "알 수 없음"; this.messageLabel.BackColor = Color.Yellow; } else if(inactiveTimeSpan.Value.TotalSeconds > 5) { this.messageLabel.Text = $"{(int)inactiveTimeSpan.Value.TotalSeconds}초 동안 비활성됨"; this.messageLabel.BackColor = Color.Red; } else { this.messageLabel.Text = "활성"; this.messageLabel.BackColor = Color.Green; } } #endregion } } |