zoukankan      html  css  js  c++  java
  • 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 ;
            }

        }

    }
  • 相关阅读:
    MFC project for a non-Unicode character set is deprecated
    关于Visual Studio 2013 编译 multi-byte character set MFC程序出现 MSB8031 错误的解决办法
    字符串比较自实现
    各种语言里获取当前模块的方法:ABAP,ABSL,C,nodejs
    SAP CRM product attachment的document template功能
    ABAP, UI5和webpack的处理入口
    ABAP, Maven, CF App和Webpack的build
    json格式的字符串序列化和反序列化的一些高级用法
    SAP ABAP Netweaver容器化, 不可能完成的任务吗?
    UI Component in CRM WebUI and Hybris
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204956.html
Copyright © 2011-2022 走看看