程序实现效果图

自定义锁定控件源码
public class UserStateMonitor : Component
{
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); }
}
[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;
}
}
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;
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