檢測當前程序是否有人操作應該再實際中還是有用的。最簡單的方法時判斷鼠標位置是否改變了。winform中也是一樣。直接看代碼:
[csharp]
public class CheckUsedManager
{
public static event EventHandler TimeToFinishedEvent = null;
private static DispatcherTimer checkUsedTimer = new DispatcherTimer();
private static Point mousePosition = GetMousePoint();
static CheckUsedManager()
{
checkUsedTimer.Interval = TimeSpan.FromSeconds(秒數);
checkUsedTimer.Tick += new EventHandler(CheckUsedTimer_Tick);
checkUsedTimer.Start();
}
static void CheckUsedTimer_Tick(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
if (TimeToFinishedEvent != null)
{
TimeToFinishedEvent(null, null);
}
}
}
private static bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
}
[StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y;
public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt);
/// <summary>
/// 獲取當前屏幕鼠標位置
/// </summary>
/// <returns></returns>
public static Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}
public class CheckUsedManager
{
public static event EventHandler TimeToFinishedEvent = null;
private static DispatcherTimer checkUsedTimer = new DispatcherTimer();
private static Point mousePosition = GetMousePoint();
static CheckUsedManager()
{
checkUsedTimer.Interval = TimeSpan.FromSeconds(秒數);
checkUsedTimer.Tick += new EventHandler(CheckUsedTimer_Tick);
checkUsedTimer.Start();
}
static void CheckUsedTimer_Tick(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
if (TimeToFinishedEvent != null)
{
TimeToFinishedEvent(null, null);
}
}
}
private static bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
}
[StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y;
public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt);
/// <summary>
/// 獲取當前屏幕鼠標位置
/// </summary>
/// <returns></returns>
public static Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
} 然後綁定到該類的事件即可。如下用WPF實現的完整代碼:
判斷的類:
[csharp]
public class CheckUsedManager
{
public static event EventHandler TimeToFinishedEvent = null;
private static DispatcherTimer checkUsedTimer = new DispatcherTimer();
private static Point mousePosition = GetMousePoint();
static CheckUsedManager()
{
checkUsedTimer.Interval = TimeSpan.FromSeconds(ReadConfig.AutoCheckUsedTime);
checkUsedTimer.Tick += new EventHandler(CheckUsedTimer_Tick);
checkUsedTimer.Start();
}
static void CheckUsedTimer_Tick(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
if (TimeToFinishedEvent != null)
{
TimeToFinishedEvent(null, null);
}
}
}
private static bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
}
[StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y;
public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt);
/// <summary>
/// 獲取當前屏幕鼠標位置
/// </summary>
/// <returns></returns>
public static Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}
public class CheckUsedManager
{
public static event EventHandler TimeToFinishedEvent = null;
private static DispatcherTimer checkUsedTimer = new DispatcherTimer();
private static Point mousePosition = GetMousePoint();
static CheckUsedManager()
{
checkUsedTimer.Interval = TimeSpan.FromSeconds(ReadConfig.AutoCheckUsedTime);
checkUsedTimer.Tick += new EventHandler(CheckUsedTimer_Tick);
checkUsedTimer.Start();
}
static void CheckUsedTimer_Tick(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
if (TimeToFinishedEvent != null)
{
TimeToFinishedEvent(null, null);
}
}
}
private static bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
}
[StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y;
public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt);
/// <summary>
/// 獲取當前屏幕鼠標位置
/// </summary>
/// <returns></returns>
public static Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}
讀配置裡的時間:
[csharp]
public class ReadConfig
{
/// <summary>
/// 檢測是否使用間隔時間
/// </summary>
public static int AutoCheckUsedTime
{
get
{
int time = 10;
try
{
string timeStr = ConfigurationManager.AppSettings["AutoCheckUsedTime"];
if (string.IsNullOrEmpty(timeStr))
{
timeStr= "10";
}
time = int.Parse(timeStr);
}
catch { }
return time;
}
}
}
public class ReadConfig
{
/// <summary>
/// 檢測是否使用間隔時間
/// </summary>
public static int AutoCheckUsedTime
{
get
{
int time = 10;
try
{
string timeStr = ConfigurationManager.AppSettings["AutoCheckUsedTime"];
if (string.IsNullOrEmpty(timeStr))
{
timeStr= "10";
}
time = int.Parse(timeStr);
}
catch { }
return time;
}
}
}配置App.config:
[csharp]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AutoCheckUsedTime" value="10"/>
</appSettings>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AutoCheckUsedTime" value="10"/>
</appSettings>
</configuration>界面調用:
[csharp]
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CheckUsedManager.TimeToFinishedEvent -= new EventHandler(CheckUsedManager_TimeToFinishedEvent);
CheckUsedManager.TimeToFinishedEvent += new EventHandler(CheckUsedManager_TimeToFinishedEvent);
}
void CheckUsedManager_TimeToFinishedEvent(object sender, EventArgs e)
{
MessageBox.Show("無人使用!");
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CheckUsedManager.TimeToFinishedEvent -= new EventHandler(CheckUsedManager_TimeToFinishedEvent);
CheckUsedManager.TimeToFinishedEvent += new EventHandler(CheckUsedManager_TimeToFinishedEvent);
}
void CheckUsedManager_TimeToFinishedEvent(object sender, EventArgs e)
{
MessageBox.Show("無人使用!");
}
}結果: