[英]automatic logoff application when pc not used C# windows application
我想制作一個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());
}
}
}
您的邏輯似乎有些混亂。 我不建議您重復觸發表單加載。 您可以嘗試執行以下操作,如果用戶在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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.