请教,C#winform判断鼠标30秒不动就关闭窗口
请教,C#winform判断鼠标30秒不动就关闭窗口c#winform
如果连续30秒鼠标和键盘都不动,就关闭整个软件。
如果,中途有动,就重新计算。
请教,各位写个详细代码,谢谢。 --------------------编程问答-------------------- hook鼠标动作,然后记时.可以参考:
http://wenku.baidu.com/view/fa84206858fafab069dc02a5.html --------------------编程问答--------------------
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace Test
{
public class App
{
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public Int32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
static uint GetLastInputTime()
{
uint idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
uint envTicks = (uint)Environment.TickCount;
if (GetLastInputInfo(ref lastInputInfo))
{
uint lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : 0);
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem( (o) =>
{
while (true)
{
Console.WriteLine(String.Format("键盘鼠标停止{0}秒", GetLastInputTime()));
Thread.Sleep(1000);
}
});
Console.Read();
}
}
}
--------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/system.windows.forms.application.idle
楼主研究下这个 --------------------编程问答-------------------- 开个记时线程,达到时间关闭窗口
响应鼠标移动事件,计时数清零
--------------------编程问答-------------------- 开个记时线程,达到时间关闭窗口
响应鼠标移动事件,计时数清零
--------------------编程问答-------------------- 让你的所有窗口都继承BaseForm 就可以了。
public partial class BaseForm : Form--------------------编程问答-------------------- --------------------编程问答-------------------- Idle事件。 --------------------编程问答-------------------- 安装鼠标键盘全局钩子进行监控即可,楼主网上搜一下setwindowhookex的用法就都清楚了 --------------------编程问答--------------------
{
private Timer timer;
int x, y;
DateTime start;
bool ff = true;
public BaseForm()
{
timer = new Timer();
x = Control.MousePosition.X;
y = Control.MousePosition.Y;
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
protected void timer_Tick(object sender, EventArgs e)
{
int x1 = Control.MousePosition.X;
int y1 = Control.MousePosition.Y;
if ((x == x1) && (y == y1) && ff)
{
start = DateTime.Now;
ff = false;
}
if (x != x1 || y != y1)
{
x = x1;
y = y1;
start = DateTime.Now;
ff = true;
}
TimeSpan ts = DateTime.Now.Subtract(start);
if (ts.Seconds > 5) Environment.Exit(0); //把5改成30,就是30秒
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
start = DateTime.Now;
return base.ProcessCmdKey(ref msg, keyData);
}
}
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace Test
{
public class App
{
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public Int32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
static uint GetLastInputTime()
{
uint idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
uint envTicks = (uint)Environment.TickCount;
if (GetLastInputInfo(ref lastInputInfo))
{
uint lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : 0);
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem( (o) =>
{
while (true)
{
Console.WriteLine(String.Format("键盘鼠标停止{0}秒", GetLastInputTime()));
Thread.Sleep(1000);
}
});
Console.Read();
}
}
}
--------------------编程问答--------------------
如果鼠标不在当前窗体上,或者当前窗体不是最前置,你这个方法还好用么? --------------------编程问答--------------------
这个方法不错! --------------------编程问答-------------------- 那怎么才能显示出倒计时来呢?
比如,你10多秒都不动了。我想知道我还剩余多少秒就关闭。
想在状态栏里出现倒计时秒钟。 --------------------编程问答--------------------
可以弄一个 qq 新闻那样的弹出窗口 --------------------编程问答-------------------- 不要再弹出来了。
让他在任务栏里显示倒计时。
如果鼠标动了,倒计时消失。
如果鼠标超过20秒不动,任务栏就显示倒计时10秒。
--------------------编程问答--------------------
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace Test
{
public class App
{
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public Int32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
static uint GetLastInputTime()
{
uint idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
uint envTicks = (uint)Environment.TickCount;
if (GetLastInputInfo(ref lastInputInfo))
{
uint lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : 0);
}
private static readonly Int32 X = 20;
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem((o) =>
{
UInt32 i =0;
while (true)
{
if(i>=X)
{
return;
}
i = GetLastInputTime();
Console.WriteLine(String.Format("键盘鼠标停止{0}秒,剩余{1}秒",i,X - i ));
Thread.Sleep(1000);
}
});
Console.Read();
}
}
}
10楼的代码,再使用幼儿园的减法,就行了
--------------------编程问答--------------------
提醒你把握好规划时一个基本原则,必须防止那种造成主线程很卡、造成大量cpu占用的,滥用循环和定时器的问题。由于这个问题很容易犯类似错误,一定要把程序(特别是复杂winform操作)响应性能放在首位,否则很容易胡乱编程。 --------------------编程问答-------------------- 感谢 (向上吧!青鸟)
但是,您的代码,那个是让 倒计时在任务栏里显示的呢??
--------------------编程问答--------------------
懒得理你
--------------------编程问答-------------------- 软件里的 任务栏中显示倒计时。 --------------------编程问答-------------------- 好用! --------------------编程问答--------------------
这个不错!顶一个!!但是这个 貌似 有什么弊端吧?
补充:.NET技术 , C#