• 利用win32的GetLastInputInfo函数实现锁屏(C#)


    前两天看到群里面讨论这个问题,刚好我们上一家公司的系统也有这个功能,就研究了一下,我们这边实现这个功能的目的如下:当用户长时间不操作系统时,自动退出系统并退回到登录界面,想要使用系统,就得重新输入账号和密码。

    下面是网上搜集到的资料:

    https://www.cnblogs.com/riasky/p/3459030.html  (检测鼠标的位置及焦点事件)

    C#全局监听键盘事件_c#监听键盘输入_木已初夏的博客-CSDN博客  (使用钩子函数)

    测试环境:

    visual studio 2017

    .net framework 3.5

    操作步骤如下:

    1  新建winform项目,项目名称为:LockScreen

    2  新建名为LockScreenManager的操作类,并编辑如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Runtime.InteropServices;
    5. using System.Text;
    6. namespace LockScreen
    7. {
    8. public class LockScreenManager
    9. {
    10. ///
    11. /// 获取上一次输入的时间
    12. ///
    13. /// false为获取失败
    14. ///
    15. [DllImport("user32.dll")]
    16. private static extern bool GetLastInputInfo(ref LastInputInfo plii);
    17. ///
    18. /// 获取当前操作系统的时刻计数
    19. ///
    20. ///
    21. [DllImport("kernel32.dll")]
    22. private static extern ulong GetTickCount64();
    23. public static ulong GetLastInputTime()
    24. {
    25. LastInputInfo lastInputInfo = default(LastInputInfo);
    26. lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
    27. bool flag = !GetLastInputInfo(ref lastInputInfo);
    28. ulong result;
    29. if (flag)
    30. {
    31. result = 0UL;
    32. }
    33. else
    34. {
    35. //获取当前操作系统的时刻计数
    36. ulong currentOsTickCount = 0;
    37. try
    38. {
    39. currentOsTickCount=GetTickCount64();
    40. }
    41. catch (Exception)
    42. {
    43. currentOsTickCount = (ulong)((long)Environment.TickCount);
    44. }
    45. //获取最后一个输入事件时的时刻计数
    46. ulong lastInputEventTickCount = (ulong)lastInputInfo.dwTime;
    47. ulong gapTickCount = currentOsTickCount - lastInputEventTickCount;
    48. ulong gapSeconds = gapTickCount / 1000UL;
    49. bool flag2 = gapSeconds > 86400UL;
    50. if (flag2)
    51. {
    52. result = 0UL;
    53. }
    54. else
    55. {
    56. result = gapSeconds;
    57. }
    58. }
    59. return result;
    60. }
    61. }
    62. public struct LastInputInfo
    63. {
    64. ///
    65. /// 结构的大小
    66. ///
    67. [MarshalAs(UnmanagedType.U4)]
    68. public int cbSize;
    69. ///
    70. /// 收到最后一个输入事件时的时刻计数
    71. ///
    72. [MarshalAs(UnmanagedType.U4)]
    73. public uint dwTime;
    74. }
    75. }

    3  把默认的窗体名称修改为:FrmMain,最后界面如下图:

    4  新建名为LockScreenWindow的锁屏窗体,最后效果如下图:

    4  编辑FrmMain.cs如下图:

    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. namespace LockScreen
    10. {
    11. public partial class FrmMain : Form
    12. {
    13. int unlock = 0;
    14. //锁屏秒数
    15. ulong lockScreenSecond = 10;
    16. public FrmMain()
    17. {
    18. InitializeComponent();
    19. Timer timer = new Timer();
    20. timer.Interval = 1000;
    21. timer.Tick += Timer_Tick;
    22. timer.Start();
    23. }
    24. private void Timer_Tick(object sender, EventArgs e)
    25. {
    26. var lastInputTime=LockScreenManager.GetLastInputTime();
    27. Console.WriteLine("输出值:"+lastInputTime);
    28. if (lastInputTime > lockScreenSecond)
    29. {
    30. if(System.Threading.Interlocked.Exchange(ref unlock,1)==0)
    31. {
    32. LockScreenWindow lockScreenWindow = new LockScreenWindow();
    33. lockScreenWindow.ShowDialog();
    34. System.Threading.Interlocked.Exchange(ref unlock, 0);
    35. }
    36. }
    37. }
    38. }
    39. }

    上述代码启用了一个定时器,定时1秒,其中lockScreenSecond是锁屏的秒数,即超过lockScreenSecond秒都没操作电脑就弹出锁屏界面

    5  运行效果如下图:

    运行程序后,不做任何操作等待10秒左右弹出锁屏界面

  • 相关阅读:
    RISC-V “异军”突起,英特尔:消灭不了就加入它
    4种Javascript类型检测的方式
    2022年ccpc绵阳站E题 E. Hammer to Fall( 倒序dp + 分块优化)
    区间素数筛
    Spring Cloud项目(二)——集成Nacos作为配置中心
    tongweb使用之端口冲突处理办法
    [JMeter]maven利用jmeter-maven-plugin插件管理jmx脚本
    机器学习-模型评估
    25.Xaml DateGrid控件---->默认单选,可以多项选择的网格控件
    CTFshow web37 38 39 40
  • 原文地址:https://blog.csdn.net/zxy13826134783/article/details/134163868