c# 判断鼠标是否空闲
来源:互联网 发布:淘宝优惠群 编辑:程序博客网 时间:2024/05/16 08:53
首先需要一个鼠标钩子了指导鼠标在什么位置
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GlobalMouseHook
{
public class MouseHook
{
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONUP = 0x208;
private const int WM_LBUTTONDBLCLK = 0x203;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDBLCLK = 0x209;
//全局的事件
public event MouseEventHandler OnMouseActivity;
static int hMouseHook = 0; //鼠标钩子句柄
//鼠标常量
public const int WH_MOUSE_LL = 14; //mouse hook constant
HookProc MouseHookProcedure; //声明鼠标钩子事件类型.
//声明一个Point的封送类型
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
//声明鼠标钩子的封送结构类型
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hWnd;
public int wHitTestCode;
public int dwExtraInfo;
}
//装置钩子的函数
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸下钩子的函数
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//下一个钩挂的函数
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
/// <summary>
/// 墨认的构造函数构造当前类的实例.
/// </summary>
public MouseHook()
{
}
//析构函数.
~MouseHook()
{
Stop();
}
public void Start()
{
//安装鼠标钩子
if(hMouseHook == 0)
{
//生成一个HookProc的实例.
MouseHookProcedure = new HookProc(MouseHookProc);
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), 0);
//如果装置失败停止钩子
if(hMouseHook == 0 )
{
Stop();
throw new Exception("SetWindowsHookEx failed.");
}
}
}
public void Stop()
{
bool retMouse =true;
if(hMouseHook != 0)
{
retMouse = UnhookWindowsHookEx(hMouseHook);
hMouseHook = 0;
}
//如果卸下钩子失败
if (!(retMouse)) throw new Exception("UnhookWindowsHookEx failed.");
}
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
//如果正常运行并且用户要监听鼠标的消息
if ((nCode >= 0) && (OnMouseActivity!=null))
{
MouseButtons button=MouseButtons.None;
int clickCount=0;
switch (wParam)
{
case WM_LBUTTONDOWN:
button=MouseButtons.Left;
clickCount=1;
break;
case WM_LBUTTONUP:
button=MouseButtons.Left;
clickCount=1;
break;
case WM_LBUTTONDBLCLK:
button=MouseButtons.Left;
clickCount=2;
break;
case WM_RBUTTONDOWN:
button=MouseButtons.Right;
clickCount=1;
break;
case WM_RBUTTONUP:
button=MouseButtons.Right;
clickCount=1;
break;
case WM_RBUTTONDBLCLK:
button=MouseButtons.Right;
clickCount=2;
break;
}
//从回调函数中得到鼠标的信息
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
MouseEventArgs e=new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0 );
//if(e.X>700)return 1;//如果想要限制鼠标在屏幕中的移动区域可以在此处设置
OnMouseActivity(this, e);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
}
}
然后用timer触发器来判断当前位置和以前位置的difference
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlobalMouseHook;
using System.Timers;
namespace testMouseHook
{
public partial class Form1 : Form
{
MouseHook mouse = new MouseHook();
System.Timers.Timer t = new System.Timers.Timer(5000);
//实例化Timer类,设置间隔时间为10000毫秒;
bool first = true;
Point pre;
Point cur;
public Form1()
{
InitializeComponent();
mouse.OnMouseActivity += new MouseEventHandler(mouse_OnMouseActivity);
mouse.Start();
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
//到达时间的时候执行事件;
t.AutoReset = true;
//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;
//是否执行System.Timers.Timer.Elapsed事件;
}
void mouse_OnMouseActivity(object sender, MouseEventArgs e)
{
string str = "X:" + e.X + " Y:" + e.Y;
textBox1.Text = str;
cur.X = e.X;
cur.Y = e.Y;
}
public void theout( object source, System.Timers.ElapsedEventArgs e)
{
//MessageBox.Show("OK!");
if( first ==true )
{
pre = cur;
first = false;
}
else
{
//cur 是最新的
if( cur.X == pre.X && cur.Y == pre.Y )
{
MessageBox.Show("idle");
}
pre = cur;
}
}
}
}
- c# 判断鼠标是否空闲
- C# 判断系统空闲(键盘、鼠标不操作一段时间)
- C# 判断系统空闲(键盘、鼠标不操作一段时间)
- C# 判断系统空闲(键盘、鼠标不操作一段时间)
- C# 判断系统空闲(键盘、鼠标不操作一段时间)
- 判断系统CPU是否空闲
- C# 判断鼠标是否在picturebox上
- 判断空闲时间是否超过阀值
- C#判断按钮控件是否在鼠标上
- C#判断鼠标是否在自己程序的NotifyIcon上
- 判断鼠标是否在窗体
- c#判断是否联网
- C#判断是否连网
- c# 判断是否联网
- C# 判断是否图片格式
- 判断鼠标是否移到了窗口外
- MFC 判断鼠标是否在控件范围内
- MFC 判断鼠标是否在控件范围内
- 轻量级的C语言面向对象编程框架(约100行代码)
- Windows对时
- 自然框架的源代码、Demo、数据库、配置信息管理程序下载(2010.01.25更新)
- sql 2000 复制表
- 教你如何在你的iPhone应用中使用Thumb指令集
- c# 判断鼠标是否空闲
- 2010年第一篇日志(2010-01-21 04:24)
- 关于计算机程序的学习经历(2010-1-22)
- 我准备做个Java学习试验(2010-01-23)
- 员工管理系统(2010-01-24 )
- 错误 132 未能找到元数据文件“C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/11月18日/1a52cf13/8b557b50/App_Web_oec5ltb
- 每天,我们都在不停地奋斗!
- 今天终于写了一篇烂东西交上去了,内容空洞,语言乏味~~
- Terracotta的新项目Terracotta Darwin 已经进入测试版。