我想创建一个winform应用程序,如果任何人没有使用计算机,例如10分钟,那么它应该显示一个弹出窗口(你是否在场)如果没有,那么PC将自动注销。
请给我一些代码或想法,让鼠标和键盘按键移动,即如果鼠标或键盘不使用10分钟,那么这个弹出窗口应显示.... 请帮帮我 感谢
我使用的这段代码但它不起作用。我使用了4秒来制作
Timer t = new Timer();
string x;
string y;
string z;
private void Form1_Load(object sender, EventArgs e)
{
z = transfer();
t.Interval = (4000);
t.Enabled = true;
t.Tick += new EventHandler(timer1_Tick);
t.Start();
}
string transfer()
{
x = Cursor.Position.X.ToString();
y = Cursor.Position.Y.ToString();
return x+y;
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
x = Cursor.Position.X.ToString();
y = Cursor.Position.Y.ToString();
string p = x + y;
if (z == p)
{
MessageBox.Show("Are you present", "Alert");
Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");
}
else
{
t.Stop();
this.Form1_Load(this, e);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
答案 0 :(得分:1)
你的逻辑似乎有点困惑。我不建议反复解雇你的表格。您可以尝试这样的事情,如果用户不移动鼠标4秒钟,它将触发一些代码:
Timer t = new Timer();
Point currPos;
Point oldPos;
private void Form1_Load(object sender, EventArgs e)
{
currPos = Cursor.Position;
t.Interval = (4000);
t.Enabled = true;
t.Tick += new EventHandler(timer1_Tick);
t.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
currPos = Cursor.Position;
if (oldPos == currPos)
{
t.Stop();
// I'm not clear what you want here - perhaps remove the messagebox and lock the workstation?
var res = MessageBox.Show("Are you present", "Alert");
if (res == DialogResult.OK)
{
t.Start();
}
// Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");
}
oldPos = currPos;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}