首页 新闻 搜索 专区 学院

简单的hook,跪求!!

0
[已关闭问题]

功能不要南无复杂,只要将a建换成b建,注释详细些就可以了,因为是菜鸟,说已没分数,也希望大家能帮帮我,,,,昨天提问没人回答,5555555555555

麻烦吧源代码发我c375403293@126.com,,,,,谢谢了,

koma-chen的主页 koma-chen | 初学一级 | 园豆:6
提问于:2009-08-24 20:14
< >
分享
其他回答(1)
0

你想改别的进程的还是自己的。如果是自己的这样的功能有必要使用钩子嘛。重新处理下消息处理函数不就行了。这种百度或者GOOGLE一堆的东西。一般人不会发代码给你的。你自己GOOGLE或者百度吧。

jivi | 园豆:300 (菜鸟二级) | 2009-08-25 09:36
0

  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);
}
}
这个是鼠标钩子,可以参考下

吴畏 | 园豆:426 (菜鸟二级) | 2009-08-25 10:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册