|
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; // mouse move
private const int WM_LBUTTONDOWN = 0x201; // left mouse button pressed
private const int WM_RBUTTONDOWN = 0x204; // right mouse click
private const int WM_MBUTTONDOWN = 0x207; // mouse roller pressed
private const int WM_LBUTTONUP = 0x202; // the left mouse button is raised
private const int WM_RBUTTONUP = 0x205; // right mouse button raised
private const int WM_MBUTTONUP = 0x208; // mouse up
private const int WM_LBUTTONDBLCLK = 0x203; // Double click with the left mouse button
private const int WM_RBUTTONDBLCLK = 0x206; // right-click and double-click
private const int WM_MBUTTONDBLCLK = 0x209; // double click with mouse
[StructLayout (LayoutKind.Sequential)] // Declares a marshaling type
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 is easy to use
hHook = SetWindowsHookEx (hHook, new mouse_key_delegate (Mouse_Key_Proc), Marshal.GetHINSTANCE (Assembly.GetExecutingAssembly (). GetModules () [0]), 0); // Why is this sentence not easy
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 ();
}
}
}
Please help me to see why global hooks are not easy to use? ? ? ? ? ? ? ? ? ? |
|