如题,不论鼠标在哪里单击都可以捕获到,谢谢!

解决方案 »

  1.   

    调用这个API
    [DllImport("user32.dll", SetLastError = true)]
            static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
      

  2.   

    放一个ApplicationEvents控件(在additional上)   
        
      procedure   TForm1.ApplicationEvents1Message(var   Msg:   tagMSG;   
          var   Handled:   Boolean);   
      begin   
          if   Msg.message=WM_RBUTTONDOWN   then   
            Showmessage('Right   button   down');   
      end;
    或者可以通过系统API 调用windows消息,检测消息是否为鼠标单击事件
    模块:   
      Option   Explicit   
      Declare   Function   SetWindowsHookEx   Lib   "user32"   Alias   "SetWindowsHookExA"   (ByVal   idHook   As   Long,   ByVal   lpfn   As   Long,   ByVal   hmod   As   Long,   ByVal   dwThreadId   As   Long)   As   Long   
      Declare   Function   UnhookWindowsHookEx   Lib   "user32"   (ByVal   hHook   As   Long)   As   Long   
      Declare   Function   CallNextHookEx   Lib   "user32"   (ByVal   hHook   As   Long,   ByVal   nCode   As   Long,   ByVal   wParam   As   Long,   lparam   As   Any)   As   Long   
      Public   Const   HC_ACTION   =   0   
      Public   Const   WH_MOUSE_LL   As   Long   =   14   
      Public   Const   WM_LBUTTONDOWN   =   &H201   
      Public   Const   WM_LBUTTONUP   =   &H202   
      Private   Type   POINTAPI   
              X   As   Long   
              Y   As   Long   
      End   Type   
      Private   Declare   Function   GetCursorPos   Lib   "user32"   (lpPoint   As   POINTAPI)   As   Long   
      Public   hHook   As   Long   
        
      Public   Sub   EnableHook()   
      If   hHook   =   0   Then   
            hHook   =   SetWindowsHookEx(WH_MOUSE_LL,   AddressOf   HookProc,   App.hInstance,   0)   
      End   If   
      End   Sub   
      Public   Sub   FreeHook()   
      If   hHook   <>   0   Then   
            Call   UnhookWindowsHookEx(hHook)   
            hHook   =   0   
      End   If   
      End   Sub   
        
      Public   Function   HookProc(ByVal   nCode   As   Long,   ByVal   wParam   As   Long,   ByVal   lparam   As   Long)   As   Long   
              If   nCode   <   0   Then   
                    HookProc   =   CallNextHookEx(hHook,   nCode,   wParam,   lparam)   
                    Exit   Function   
              End   If   
              If   wParam   =   WM_LBUTTONDOWN   Then   
                      Dim   PT   As   POINTAPI   
                      GetCursorPos   PT   
                      Form1.Caption   =   "mouse   click   at   "   +   CStr(PT.X)   +   ","   +   CStr(PT.Y)   
              End   If   
              HookProc   =   0   '令待完成的动作继续完成,若为1,则取消原本要完成的动作   
      End   Function   
        
        
      窗体(form1):   
      Option   Explicit   
      Const   HWND_TOPMOST   =   -1   
      Const   HWND_NOTOPMOST   =   -2   
      Const   SWP_NOSIZE   =   &H1   
      Const   SWP_NOMOVE   =   &H2   
      Const   SWP_NOACTIVATE   =   &H10   
      Const   SWP_SHOWWINDOW   =   &H40   
      Private   Declare   Sub   SetWindowPos   Lib   "user32"   (ByVal   hWnd   As   Long,   ByVal   hWndInsertAfter   As   Long,   ByVal   X   As   Long,   ByVal   Y   As   Long,   ByVal   cx   As   Long,   ByVal   cy   As   Long,   ByVal   wFlags   As   Long)   
        
      Private   Sub   Form_Activate()   
              SetWindowPos   Me.hWnd,   HWND_TOPMOST,   0,   0,   0,   0,   SWP_NOACTIVATE   Or   SWP_SHOWWINDOW   Or   SWP_NOMOVE   Or   SWP_NOSIZE   
      End   Sub   
      Private   Sub   Form_Load()   
              EnableHook   
      End   Sub   
        
      Private   Sub   Form_Unload(Cancel   As   Integer)   
              FreeHook   
      End   Sub简单解释一下,用的是WH_MOUSE_LL钩子,这个全局钩子比较特殊,它特殊在不需要dll,其它的就没什么可说的了,和处理其它的钩子没什么区别
    定时器不是很合适,会有漏判。全局钩子可以,但机器必须是2000或XP系统。
      

  3.   

    这个API可以达到目的吗?刚查了一下好像不行啊
      

  4.   

      protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_LBUTTONUP)
                {
                    MessageBox.Show("aaa");
                }
                base.WndProc(ref m);
            }
    这样好像只能捕捉到自己窗体内的
      

  5.   

    用获取系统消息的方法
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_LBUTTONDOWN)
                {
                    string controlName = GetControlName();
                    if (controlName == "Edit")
                    {
                        this.Show();
                    }
                    else
                    {
                        this.Hide();
                    }
                }
                base.WndProc(ref m);
            }