摘自http://blog.csdn.net/jinjazz/archive/2008/04/16/2298699.aspx

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace HotelManage
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }
         
        
void hook_onMouseChange(object sender, EventArgs e)
        {
            
this.Text = Cursor.Position.ToString();
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            Win32Hook hook 
= new Win32Hook();
            hook.onMouseChange 
+= new EventHandler(hook_onMouseChange);
            hook.SetHook();
        }
    }

    
public class Win32Hook
    {
        [DllImport(
"kernel32")]
        
public static extern int GetCurrentThreadId();

        [DllImport(
"user32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        
public static extern int SetWindowsHookEx(
            HookType idHook,
            HOOKPROC lpfn,
            
int hmod,
            
int dwThreadId);

        
public enum HookType
        {
            WH_GETMESSAGE 
= 3
        }

        
public delegate int HOOKPROC(int nCode, int wParam, int lParam);

        
public event System.EventHandler onMouseChange;

        
public void SetHook()
        {
            SetWindowsHookEx(HookType.WH_GETMESSAGE,
                
new HOOKPROC(this.MyKeyboardProc),
                
0,
                GetCurrentThreadId());
        }

        
public int MyKeyboardProc(int nCode, int wParam, int lParam)
        {
            
if (onMouseChange != null)
            {
                onMouseChange(
nullnull);
            }
            
return 0;
        }
    }
}

相关文章:

  • 2021-11-24
  • 2021-08-07
  • 2022-12-23
  • 2021-08-08
  • 2021-10-26
  • 2021-11-06
  • 2022-01-19
  • 2022-02-01
猜你喜欢
  • 2022-12-23
  • 2021-07-23
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案