添加项目文件。
This commit is contained in:
20
DotNet.Revit.Hook/Achieve/GlobalMouseHook.cs
Normal file
20
DotNet.Revit.Hook/Achieve/GlobalMouseHook.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.Achieve
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局鼠标钩子
|
||||
/// </summary>
|
||||
/// <seealso cref="DotNet.Hook.Achieve.MouseHookBase" />
|
||||
public class GlobalMouseHook : MouseHookBase
|
||||
{
|
||||
public GlobalMouseHook(IntPtr processId)
|
||||
: base(processId)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
98
DotNet.Revit.Hook/Achieve/HookBase.cs
Normal file
98
DotNet.Revit.Hook/Achieve/HookBase.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using DotNet.Revit.Hook.DataStructure;
|
||||
using DotNet.Revit.Hook.Helper;
|
||||
using DotNet.Revit.Hook.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.Achieve
|
||||
{
|
||||
public abstract class HookBase : IHook
|
||||
{
|
||||
private static Dictionary<int, IHook> m_Hooks;
|
||||
private IntPtr m_ProcessId;
|
||||
private int m_ThreadId;
|
||||
private HookType m_HookType;
|
||||
private HookProc m_HookProc;
|
||||
|
||||
protected internal int m_HookId;
|
||||
|
||||
static HookBase()
|
||||
{
|
||||
m_Hooks = new Dictionary<int, IHook>();
|
||||
}
|
||||
|
||||
private HookBase(HookType hookType)
|
||||
{
|
||||
m_HookType = hookType;
|
||||
m_HookProc = HookProc;
|
||||
}
|
||||
|
||||
protected HookBase(IntPtr processId, HookType hookType)
|
||||
: this(hookType)
|
||||
{
|
||||
m_ProcessId = processId;
|
||||
if (m_ProcessId == IntPtr.Zero)
|
||||
{
|
||||
m_ProcessId = HookHelper.GetCurrentProcessId();
|
||||
}
|
||||
}
|
||||
|
||||
protected HookBase(int threadId, HookType hookType)
|
||||
: this(hookType)
|
||||
{
|
||||
m_ThreadId = threadId;
|
||||
if (m_ThreadId == 0)
|
||||
{
|
||||
m_ThreadId = HookHelper.GetCurrentThreadId();
|
||||
}
|
||||
}
|
||||
|
||||
public void Install()
|
||||
{
|
||||
if (m_ThreadId != 0)
|
||||
{
|
||||
m_HookId = HookHelper.SetWindowsHookEx(m_HookType, m_HookProc, IntPtr.Zero, m_ThreadId);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_ProcessId == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_HookId = HookHelper.SetWindowsHookEx(m_HookType, m_HookProc, m_ProcessId, 0);
|
||||
}
|
||||
|
||||
if (m_HookId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_Hooks.ContainsKey(m_HookId))
|
||||
{
|
||||
m_Hooks.Add(m_HookId, this);
|
||||
}
|
||||
}
|
||||
|
||||
public void Uninstall()
|
||||
{
|
||||
if (m_HookId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var flag = HookHelper.UnhookWindowsHookEx(m_HookId);
|
||||
if (flag)
|
||||
{
|
||||
if (m_Hooks.Remove(m_HookId))
|
||||
{
|
||||
m_HookId = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
}
|
||||
|
||||
}
|
||||
21
DotNet.Revit.Hook/Achieve/MouseHook.cs
Normal file
21
DotNet.Revit.Hook/Achieve/MouseHook.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.Achieve
|
||||
{
|
||||
/// <summary>
|
||||
/// 线程鼠标Hook.
|
||||
/// </summary>
|
||||
/// <seealso cref="DotNet.Hook.Achieve.MouseHookBase" />
|
||||
public class MouseHook : MouseHookBase
|
||||
{
|
||||
public MouseHook(int threadId = 0)
|
||||
: base(threadId)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
145
DotNet.Revit.Hook/Achieve/MouseHookBase.cs
Normal file
145
DotNet.Revit.Hook/Achieve/MouseHookBase.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using DotNet.Revit.Hook.DataStructure;
|
||||
using DotNet.Revit.Hook.Helper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DotNet.Revit.Hook.Achieve
|
||||
{
|
||||
public abstract class MouseHookBase : HookBase
|
||||
{
|
||||
protected MouseHookBase(IntPtr processId)
|
||||
: base(processId, HookType.WH_MOUSE_LL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected MouseHookBase(int threadId)
|
||||
: base(threadId, HookType.WH_MOUSE)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标双击
|
||||
/// </summary>
|
||||
public event HookHandler<MouseEventArgs> MouseDoubleClick;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标移动
|
||||
/// </summary>
|
||||
public event HookHandler<MouseEventArgs> MouseMove;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标按下
|
||||
/// </summary>
|
||||
public event HookHandler<MouseEventArgs> MouseDown;
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标弹起
|
||||
/// </summary>
|
||||
public event HookHandler<MouseEventArgs> MouseUp;
|
||||
|
||||
protected override int HookProc(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode < 0)
|
||||
{
|
||||
return HookHelper.CallNextHookEx(m_HookId, nCode, wParam, lParam);
|
||||
}
|
||||
|
||||
var mouseMsg = (MouseMessage)wParam.ToInt32();
|
||||
var mouseHookStruct = lParam.ToStruct<MOUSEHOOKSTRUCT>();
|
||||
|
||||
var button = this.GetMouseButtons(mouseMsg);
|
||||
|
||||
switch (mouseMsg)
|
||||
{
|
||||
case MouseMessage.WM_LBUTTONDOWN:
|
||||
case MouseMessage.WM_RBUTTONDOWN:
|
||||
case MouseMessage.WM_MBUTTONDOWN:
|
||||
|
||||
return this.OnRaiseMouseDown(button, 1, mouseHookStruct.pt.X, mouseHookStruct.pt.Y, mouseHookStruct.mouseData);
|
||||
|
||||
case MouseMessage.WM_LBUTTONUP:
|
||||
case MouseMessage.WM_MBUTTONUP:
|
||||
case MouseMessage.WM_RBUTTONUP:
|
||||
|
||||
return this.OnRaiseMouseUp(button, 1, mouseHookStruct.pt.X, mouseHookStruct.pt.Y, mouseHookStruct.mouseData);
|
||||
|
||||
case MouseMessage.WM_LBUTTONDBLCLK:
|
||||
case MouseMessage.WM_RBUTTONDBLCLK:
|
||||
case MouseMessage.WM_MBUTTONDBLCLK:
|
||||
|
||||
return this.OnRaiseMouseDoubleClick(button, 2, mouseHookStruct.pt.X, mouseHookStruct.pt.Y, mouseHookStruct.mouseData);
|
||||
|
||||
case MouseMessage.WM_MOUSEMOVE:
|
||||
|
||||
return this.OnRaiseMouseMove(MouseButtons.None, 0, mouseHookStruct.pt.X, mouseHookStruct.pt.Y, mouseHookStruct.mouseData);
|
||||
default:
|
||||
return HookHelper.CallNextHookEx(m_HookId, nCode, wParam, lParam);
|
||||
}
|
||||
}
|
||||
|
||||
private MouseButtons GetMouseButtons(MouseMessage mouseMsg)
|
||||
{
|
||||
MouseButtons result = MouseButtons.None;
|
||||
switch (mouseMsg)
|
||||
{
|
||||
case MouseMessage.WM_LBUTTONDBLCLK:
|
||||
case MouseMessage.WM_LBUTTONDOWN:
|
||||
case MouseMessage.WM_LBUTTONUP:
|
||||
result = MouseButtons.Left;
|
||||
break;
|
||||
case MouseMessage.WM_MBUTTONDBLCLK:
|
||||
case MouseMessage.WM_MBUTTONDOWN:
|
||||
case MouseMessage.WM_MBUTTONUP:
|
||||
result = MouseButtons.Middle;
|
||||
break;
|
||||
case MouseMessage.WM_RBUTTONDBLCLK:
|
||||
case MouseMessage.WM_RBUTTONDOWN:
|
||||
case MouseMessage.WM_RBUTTONUP:
|
||||
result = MouseButtons.Right;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private int OnRaiseMouseDoubleClick(MouseButtons button, int clicks, int x, int y, int delta)
|
||||
{
|
||||
if (this.MouseDoubleClick != null)
|
||||
{
|
||||
return this.MouseDoubleClick(this, new MouseEventArgs(button, clicks, x, y, delta));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int OnRaiseMouseDown(MouseButtons button, int clicks, int x, int y, int delta)
|
||||
{
|
||||
if (this.MouseDown != null)
|
||||
{
|
||||
return this.MouseDown(this, new MouseEventArgs(button, clicks, x, y, delta));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int OnRaiseMouseUp(MouseButtons button, int clicks, int x, int y, int delta)
|
||||
{
|
||||
if (this.MouseUp != null)
|
||||
{
|
||||
return this.MouseUp(this, new MouseEventArgs(button, clicks, x, y, delta));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int OnRaiseMouseMove(MouseButtons button, int clicks, int x, int y, int delta)
|
||||
{
|
||||
if (this.MouseMove != null)
|
||||
{
|
||||
return this.MouseMove(this, new MouseEventArgs(button, clicks, x, y, delta));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user