楼主
问下你真名叫chenjin?
Class1.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MyHook
{
public class Class1
{
internal enum HookType //枚举,钩子的类型
{
MsgFilter = -1,
JournalRecord = 0,
JournalPlayback = 1,
Keyboard = 2,
GetMessage = 3,
CallWndProc = 4,
CBT = 5,
SysMsgFilter = 6,
Mouse = 7,
Hardware = 8,
Debug = 9,
Shell = 10,
ForegroundIdle = 11,
CallWndProcRet = 12,
KeyboardLL = 13,
MouseLL = 14
};
IntPtr _nextHookPtr; //记录Hook编号
int toda = 1;
bool da = false;
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId(); //取得当前线程编号的API
[DllImport("User32.dll")]
internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消Hook的API
[DllImport("User32.dll")]
internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置Hook的API
[DllImport("User32.dll")]
internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam); //取得下一个Hook的API
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //取得指定窗口的句柄
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID); //取得创建窗口的线程ID
public string str;
public IntPtr SetHook(int dwThreadid,HookProc myhookproc)
{
if (_nextHookPtr != IntPtr.Zero) //已经勾过了
return (IntPtr)0;
return SetWindowsHookEx((int)HookType.GetMessage, myhookproc, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), dwThreadid);
public void UnHook()
{
if (_nextHookPtr != IntPtr.Zero)
{
UnhookWindowsHookEx(_nextHookPtr); //从Hook链中取消
_nextHookPtr = IntPtr.Zero;
}
}
public IntPtr gethandle(string fname)
{
return FindWindow(null, fname);
}
public int getPID(IntPtr hwnd)
{
return GetWindowThreadProcessId(hwnd, 0);
}
public IntPtr callnext(IntPtr handle, int code, IntPtr wparam, IntPtr lparam)
{
return CallNextHookEx( handle,code, wparam, lparam);
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using MyHook;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Class1.HookProc mouseHookCallBack;
private IntPtr _nextHookPtr;
IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam)
{
Class1 myhook = new Class1();
if (code < 0) return myhook.callnext(_nextHookPtr, code, wparam, lparam); //返回,让后面的程序处理该消息
this.textBox1.Text += (char)wparam;
return (IntPtr)0; // 返回,让后面的程序处理该消息
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 myhook = new Class1();
mouseHookCallBack = (MyHook.Class1.HookProc)MyHookProc;
IntPtr hWnd;
hWnd = myhook.gethandle( "金山词霸 2005");
textBox2.Text = hWnd.ToString();
int ID = myhook.getPID(hWnd);
_nextHookPtr=myhook.SetHook(ID, mouseHookCallBack);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Class1 myhook = new Class1();
myhook.UnHook();
}