请教,C#winform判断鼠标30秒不动就关闭窗口

xiehuangda 2012-09-12 07:05:57
请教,C#winform判断鼠标30秒不动就关闭窗口

c#winform

如果连续30秒鼠标和键盘都不动,就关闭整个软件。

如果,中途有动,就重新计算。

请教,各位写个详细代码,谢谢。
...全文
2350 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
敌敌畏耶 2013-08-08
  • 打赏
  • 举报
回复
引用 2 楼 SocketUpEx 的回复:
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();
        }
    }
}
这个不错!顶一个!!但是这个 貌似 有什么弊端吧?
litongji90410 2013-08-07
  • 打赏
  • 举报
回复
引用 10 楼 SocketUpEx 的回复:
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();
        }
    }
}
好用!
xiehuangda 2012-09-17
  • 打赏
  • 举报
回复
软件里的 任务栏中显示倒计时。
xiehuangda 2012-09-16
  • 打赏
  • 举报
回复
感谢 (向上吧!青鸟)
但是,您的代码,那个是让 倒计时在任务栏里显示的呢??
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
请教,C#winform判断鼠标30秒不动就关闭窗口

c#winform

如果连续30秒鼠标和键盘都不动,就关闭整个软件。

如果,中途有动,就重新计算。

请教,各位写个详细代码,谢谢。
[/Quote]

提醒你把握好规划时一个基本原则,必须防止那种造成主线程很卡、造成大量cpu占用的,滥用循环和定时器的问题。由于这个问题很容易犯类似错误,一定要把程序(特别是复杂winform操作)响应性能放在首位,否则很容易胡乱编程。
SocketUpEx 2012-09-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

不要再弹出来了。

让他在任务栏里显示倒计时。
如果鼠标动了,倒计时消失。
如果鼠标超过20秒不动,任务栏就显示倒计时10秒。
[/Quote]


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楼的代码,再使用幼儿园的减法,就行了


xiehuangda 2012-09-16
  • 打赏
  • 举报
回复
不要再弹出来了。

让他在任务栏里显示倒计时。
如果鼠标动了,倒计时消失。
如果鼠标超过20秒不动,任务栏就显示倒计时10秒。
SocketUpEx 2012-09-16
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]

感谢 (向上吧!青鸟)
但是,您的代码,那个是让 倒计时在任务栏里显示的呢??
[/Quote]

懒得理你


csdn_风中雪狼 2012-09-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
那怎么才能显示出倒计时来呢?
比如,你10多秒都不动了。我想知道我还剩余多少秒就关闭。

想在状态栏里出现倒计时秒钟。
[/Quote]
可以弄一个 qq 新闻那样的弹出窗口
xiehuangda 2012-09-13
  • 打赏
  • 举报
回复
那怎么才能显示出倒计时来呢?
比如,你10多秒都不动了。我想知道我还剩余多少秒就关闭。

想在状态栏里出现倒计时秒钟。
shizhu820228 2012-09-12
  • 打赏
  • 举报
回复
让你的所有窗口都继承BaseForm 就可以了。
    public partial class BaseForm : Form
{
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);
}
}
xiepanqi123 2012-09-12
  • 打赏
  • 举报
回复
开个记时线程,达到时间关闭窗口

响应鼠标移动事件,计时数清零

xiepanqi123 2012-09-12
  • 打赏
  • 举报
回复
开个记时线程,达到时间关闭窗口

响应鼠标移动事件,计时数清零

SocketUpEx 2012-09-12
  • 打赏
  • 举报
回复
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();
}
}
}



只在此山中 2012-09-12
  • 打赏
  • 举报
回复
hook鼠标动作,然后记时.可以参考:
http://wenku.baidu.com/view/fa84206858fafab069dc02a5.html
bigbaldy 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

C# code
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Test
{
public class App
{
[StructLayout(LayoutKind.Sequential)]
struct LASTINPU……
[/Quote]
这个方法不错!
风吹腚腚凉 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
让你的所有窗口都继承BaseForm 就可以了。

C# code

public partial class BaseForm : Form
{
private Timer timer;
int x, y;
DateTime start;
bool ff = true;

pu……
[/Quote]
如果鼠标不在当前窗体上,或者当前窗体不是最前置,你这个方法还好用么?
SocketUpEx 2012-09-12
  • 打赏
  • 举报
回复
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();
}
}
}



bigbaldy 2012-09-12
  • 打赏
  • 举报
回复
安装鼠标键盘全局钩子进行监控即可,楼主网上搜一下setwindowhookex的用法就都清楚了
加载更多回复(2)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧