using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using System.Timers;
namespace HookGlobal
{
/// <summary>
/// 這個類可以讓你得到一個在運行中程序的所有
/// 並且引發一個帶MouseEventArgs參數的.Net鼠標事件以便你很容易使用這些
/// </summary>
///
public class LoadMouseHook
{
//該函數用於執行鼠標
[DllImport("user32.dll", EntryPoint="mouse_event")]
public static extern void mouse_event (
int dwFlags,
int dx,
int dy,
int cButtons,
int dwExtraInfo
);
//該函數用於執行鼠標定位
[DllImport("user32.dll" ,
EntryPoint="SetCursorPos")]
public extern static int SetCursorPos ( int x , int y );
//鼠標消息數組
string[,] actions;
//自定義TIMER事件
System.Timers.Timer tmr;
//時間執行位子標記
int actionflag=0;
//默認事件文件
public const string RecordFile="MouseRecorder.mrd";
//默認執行速度
public const int runspeed=30;
//
private int interval;
public LoadMouseHook(string fname,int mSpeed)
{
ActionInitialize(fname,mSpeed);
}
public LoadMouseHook(string fname)
{
ActionInitialize(fname,runspeed);
}
public LoadMouseHook()
{
ActionInitialize(RecordFile,runspeed);
}
//初始化鼠標消息數組 actions
public void ActionInitialize(string fname,int mSpeed)
{
actionflag=0;
StreamReader sr=new StreamReader(fname);
string Filetxt=sr.ReadToEnd();
sr.Close();
string[] txtarr=Filetxt.Split(''\n'');
string[] linearr;
actions=new string[txtarr.Length, for(int i=0;i<txtarr.Length;i++)
{
linearr=txtarr[i].Split('','');
for (int j=0;j<linearr.Length;j++)
{
actions[i,j]=linearr[j];
}
}
//添加TIMER事件,執行鼠標消息。
tmr=new System.Timers.Timer(mSpeed);
tmr.Elapsed+=new System.Timers.ElapsedEventHandler(this.RunActions);
}
//開始執行事件
public void StartAction()
{
tmr.Enabled=true;
}
//停止執行事件
public void StopAction()
{
tmr.Elapsed-=new System.Timers.ElapsedEventHandler(this.RunActions);
tmr.Enabled=false;
}
//時間執行過程。 public void RunActions(object sender, System.Timers.ElapsedEventArgs e)
{
//將鼠標定位到指定位置
SetCursorPos(int.Parse(actions[actionflag,1]),int.Parse(actions[actionflag,2]));
//如果鼠標執行了其他時間(比如按下,彈起等),就調用mouse_event函數執行相應的操作。
if (actions[actionflag,3]!=null)
{
switch(int.Parse(actions[actionflag,3])-512)
{
case 1://鼠標左鍵按下
mouse_event(2,0,0,0,0);
break;
case 2://鼠標左鍵彈起
mouse_event(4,0,0,0,0);
break;
case 4://鼠標右鍵按下
mouse_event(8,0,0,0,0);
break;
case 5://鼠標右鍵彈起
mouse_event(16,0,0,0,0);
break;
}
}
if (actionflag<actions.Length)
{
actionflag++;
}
else
{
actionflag=0;
tmr.Elapsed-=new System.Timers.ElapsedEventHandler(this.RunActions);
tmr.Enabled=false;
}
}
}
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;
static int hMouseHook = 0; //鼠標鉤子句柄
//鼠標常量
public const int WH_MOUSE_LL = 14; //mouse hook constant
HookProc MouseHookProcedure; //聲明鼠標鉤子事件類型.
//默認事件文件
public const string RecordFile="MouseRecorder.mrd";
//聲明一個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()
{
//Start();
}
//析構函數.
~MouseHook()
{
Stop();
}
public void Start()
{
//安裝鼠標鉤子
if(hMouseHook == 0)
{
//生成一個HookProc的實例.
MouseHookProcedure = new HookProc(MouseHookProc);
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(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)
{
//如果正常運行並且用戶要監聽鼠標的消息
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
int mousestate=wParam;
//將鼠標事件保存到文件,格式為 [時間,x,y,鼠標狀態]。
StreamWriter sw;
if (!File.Exists(RecordFile))
sw = new StreamWriter(RecordFile);
else
sw= File.AppendText(RecordFile);
sw.Write(DateTime.Now.ToShortTimeString()+",");
sw.Write(MyMouseHookStruct.pt.x.ToString());
sw.Write(",");
sw.Write(MyMouseHookStruct.pt.y.ToString());
if (mousestate!=WM_MOUSEMOVE)
sw.Write(","+mousestate.ToString());
sw.WriteLine("");
sw.Close();
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
}
}