• WinForm实现倒计时锁定程序完整源码附注释


    程序实现效果图

    在这里插入图片描述

    自定义锁定控件源码

    /// <summary>
        /// 监视鼠标键盘操作。
        /// </summary>
        public class UserStateMonitor : Component
        {
            //通过 Interval 属性调节扫描键盘鼠标状态的间隔时间,通过 Timeout 属性(无键盘操作时间间隔)控制判断是否离开
    
            /// <summary>
            /// 离开时间,单位:秒
            /// </summary>
            private int _timeout = 5000;
            private int _lastActiveTick;
            private Timer _timer;
            private int _interval = 1000;
            private bool _started;
            private bool _active = true;
    
            private static readonly object EventUseStateChanged = new object();
    
            public UserStateMonitor()
                : base()
            {
            }
    
            public UserStateMonitor(IContainer container)
                : base()
            {
                container.Add(this);
            }
    
            public event UserStateChangedEventHandler UserStateChanged
            {
                add { base.Events.AddHandler(EventUseStateChanged, value); }
                remove { base.Events.RemoveHandler(EventUseStateChanged, value); }
            }
    
            /// <summary>
            /// 默认值:30分钟
            /// </summary>
            [DefaultValue(1800000)]
            public int Timeout
            {
                get { return _timeout; }
                set
                {
                    _timeout = Math.Max(5000, value);
                }
            }
    
            [DefaultValue(1000)]
            public int Interval
            {
                get { return _interval; }
                set
                {
                    _interval = Math.Max(500, value);
                    if (_started)
                    {
                        Timer.Change(0, _interval);
                    }
                    else
                    {
                        Timer.Change(System.Threading.Timeout.Infinite, _interval);
                    }
                }
            }
    
            private Timer Timer
            {
                get 
                {
                    if (_timer == null)
                    {
                        _timer = new Timer(
                            new TimerCallback(delegate(object obj)
                            {
                                LASTINPUTINFO lii = new LASTINPUTINFO();
                                lii.size = 8;
    
                                if (GetLastInputInfo(ref lii))
                                {
                                    _lastActiveTick = Math.Max(lii.lastTick, _lastActiveTick);
                                    bool active = Environment.TickCount - _lastActiveTick < _timeout;
                                    if (_active != active)
                                    {
                                        _active = active;
                                        OnUserStateChanged(new UserStateChangedEventArgs(active));
                                    }
                                }
                            }),
                            null,
                            System.Threading.Timeout.Infinite,
                            _interval);
                    }
                    return _timer;
                }
            }
    
            public void Start()
            {
                if (!_started)
                {
                    Timer.Change(_interval, _interval);
                    _started = true;
                }
            }
    
            /// <summary>
            /// 开始监控
            /// </summary>
            /// <param name="timeOut">监控时间间隔,单位:分钟</param>
            public void Start(int timeOut)
            {
                if (!_started)
                {
                    
                    _timeout = timeOut  * 1000;//转换成秒
    
                    Timer.Change(_interval, _interval);
                    _started = true;
                }
            }
    
            public void Stop()
            {
                if (_started)
                {
                    Timer.Change(System.Threading.Timeout.Infinite, _interval);
                    _started = false;
                }
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct LASTINPUTINFO
            {
                public int size;
                public int lastTick;
            }
    
            [DllImport("user32.dll")]
            private static extern bool GetLastInputInfo(ref LASTINPUTINFO lii);
    
            protected virtual void OnUserStateChanged(UserStateChangedEventArgs e)
            {
                UserStateChangedEventHandler handler =
                    base.Events[EventUseStateChanged] as UserStateChangedEventHandler;
                if (handler != null)
                {
                    handler(this, e);
                }
            }
    
            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
                if (disposing)
                {
                    Stop();
                    if (_timer != null)
                    {
                        _timer.Dispose();
                        _timer = null;
                    }
                }
            }
        }
    
    • 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
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166

    自定义锁定控件之状态改变事件源码

      public delegate void UserStateChangedEventHandler(
            object sender,
            UserStateChangedEventArgs e);
    
        public class UserStateChangedEventArgs : EventArgs
        {
            private bool _active;
    
            public UserStateChangedEventArgs(bool active)
                : base()
            {
                _active = active;
            }
    
            public bool Active
            {
                get { return _active; }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    自定义锁定控件调用源码

      public Form1()
            {
                InitializeComponent();
                
                //判断是否长时间无操作
                userStateMonitor1.UserStateChanged += delegate (object sender, UserStateChangedEventArgs e)
                {
                    BeginInvoke(new MethodInvoker(delegate ()
                    {
                        bool isLock = e.Active ? false : true;
                        if (isLock)
                        {
                            userStateMonitor1.Stop();
                            DialogResult result = new FrmLockMsg().ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                FrmLock frm = new FrmLock();
    
                                this.Hide();
    
    
    
                                DialogResult result2 = frm.ShowDialog();
                                if (result2 == DialogResult.OK)
                                {
    
    
                                    this.Show();
    
                                }
    
    
                            }
                            userStateMonitor1.Start();
    
                        }
                    }));
                };
               
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int locktime = 0;
                //五秒无操作锁定 分钟*60
                int.TryParse("5", out locktime);
                if (locktime > 0)
                {
                    userStateMonitor1.Start(locktime  );
                }
            }
    
    • 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
  • 相关阅读:
    学了labview怎么系统的编一个程序?
    【Flink】
    以太坊若分叉 NFT也会“分叉”吗?
    Linux每日智囊
    数据同步到Redis消息队列,并实现消息发布/订阅
    给大家推荐一套 git 工作流
    【leetcode】【剑指offer Ⅱ】070. 排序数组中只出现一次的数字
    CCS9.1导入F28069M例子工程 遇到的一些问题
    2022广东网络安全省赛—代码渗透测试wp
    如何获取cat_get-淘宝分类详情API接口
  • 原文地址:https://blog.csdn.net/qq_37344688/article/details/125519455