• c#中通过设置钩子监视鼠标移动


    这个问题来自论坛提问,C#的大致代码如下

    using  System;
    using  System.Windows.Forms;
    using  System.Runtime.InteropServices;
    namespace  WindowsApplication1
    {
        
    public   partial   class  Form1 : Form
        
    {
            
    public  Form1()
            
    {
                InitializeComponent();
            }

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


            
    void  hook_onMouseChange( object  sender, EventArgs e)
            
    {
                
    this .Text  =  Cursor.Position.ToString();
            }

        }

        
    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(
    null null );
                }

                
    return   0 ;
            }

        }

    }
  • 相关阅读:
    MySQL令人咋舌的隐式转换
    MySQL 数据库基础(二)(MySQL 服务基础与使用 MySQL 数据库)
    以友盟+U-Push为例,深度解读消息推送的筛选架构解决方案应用与实践
    逆向工程,调试Hello World !程序(更新中)
    520了,用32做个简单的小程序
    postgresql 数据库 update更新慢的原因(已解决)
    面试题单例模式的五种写法(枚举妙用)
    人工智能能力提升指导总结
    数据结构-队列(2)-循环队列
    数据结构-队列(1)
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204956.html
Copyright © 2020-2023  润新知