程序有问题,帮看看(vs2005)
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.Reflection;
namespace a4
{
public partial class Form1 : Form
{
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; //鼠标滚轴双击
[StructLayout(LayoutKind.Sequential)] //声明一个封送类型
public class MouseHookStruct
{
public Point pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
int hHook=0;
private delegate int mouse_key_delegate(
int nCode,int wParam,IntPtr lParam);
[DllImport("kernel32.dll" , CharSet = CharSet.Auto , CallingConvention = CallingConvention.StdCall, SetLastError = true , EntryPoint = "GetCurrentThreadId")]
private static extern int GetCurrentThreadId();
[DllImport("user32.dll", CharSet = CharSet.Auto , CallingConvention = CallingConvention.StdCall, SetLastError = true , EntryPoint = "SetWindowsHookExA")]
private static extern int SetWindowsHookEx(int hHook, mouse_key_delegate mkDelegate, IntPtr wParam, int lParam);
[DllImport("user32.dll" , CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true , EntryPoint = "UnhookWindowsHookEx")]
private static extern bool UnhookWindowsHookEx(int hHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true , EntryPoint = "CallNextHookEx")]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, IntPtr lParam);
public Form1()
{
InitializeComponent();
}
public Form1(int hHook)
{
this.hHook = hHook;
this.InitializeComponent();
}
private int Mouse_Key_Proc(int nCode, int wParam, IntPtr lParam)
{
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
if (nCode > 0 && wParam == WM_LBUTTONDOWN)
{
this.toolStripStatusLabel1.Text = " X= " + MyMouseHookStruct.pt.X + ", Y= " + MyMouseHookStruct.pt.Y;
return 1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
private bool Start()
{
//hHook = SetWindowsHookEx(hHook, new mouse_key_delegate(Mouse_Key_Proc), IntPtr.Zero, GetCurrentThreadId());//hHook=2好用
hHook = SetWindowsHookEx(hHook, new mouse_key_delegate(Mouse_Key_Proc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);//这句为什么不好用
MessageBox.Show("hHook= " + hHook);
if (hHook != 0)
return true;
return false;
}
private bool Stop()
{
if (hHook == 0)
{
UnhookWindowsHookEx(hHook);
return true;
}
return false;
}
private void Form1_Load(object sender, EventArgs e)
{
hHook = 14;
Start();
}
}
} --------------------编程问答-------------------- 请帮我看看全局钩子为什么不好用?????????? --------------------编程问答--------------------
补充:.NET技术 , C#