添加项目文件。
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
42
DotNet.Revit.Hook/CommandTest.cs
Normal file
42
DotNet.Revit.Hook/CommandTest.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.Attributes;
|
||||
|
||||
namespace DotNet.Revit.Hook
|
||||
{
|
||||
/// <summary>
|
||||
/// 鼠标双击元素拦截事件.
|
||||
/// </summary>
|
||||
/// <seealso cref="Autodesk.Revit.UI.IExternalCommand" />
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class MouseHookTest : IExternalCommand
|
||||
{
|
||||
Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
||||
{
|
||||
if (ElementMonitor.Instance == null)
|
||||
{
|
||||
ElementMonitor.Register(commandData.Application);
|
||||
}
|
||||
|
||||
ElementMonitor.Instance.DoubleClickElement += OnRaiseDoubleClickElement;
|
||||
|
||||
return Result.Succeeded;
|
||||
}
|
||||
|
||||
private int OnRaiseDoubleClickElement(object sender, DoubleClickElementEventArgs e)
|
||||
{
|
||||
if (e.Element == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
System.Windows.Forms.MessageBox.Show(string.Format("双击击元素Id: {0}", e.Element.Id));
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
DotNet.Revit.Hook/DataStructure/HookType.cs
Normal file
27
DotNet.Revit.Hook/DataStructure/HookType.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.DataStructure
|
||||
{
|
||||
public enum HookType : int
|
||||
{
|
||||
WH_MSGFILTER = -1,
|
||||
WH_JOURNALRECORD = 0,
|
||||
WH_JOURNALPLAYBACK = 1,
|
||||
WH_KEYBOARD = 2,
|
||||
WH_GETMESSAGE = 3,
|
||||
WH_CALLWNDPROC = 4,
|
||||
WH_CBT = 5,
|
||||
WH_SYSMSGFILTER = 6,
|
||||
WH_MOUSE = 7,
|
||||
WH_HARDWARE = 8,
|
||||
WH_DEBUG = 9,
|
||||
WH_SHELL = 10,
|
||||
WH_FOREGROUNDIDLE = 11,
|
||||
WH_CALLWNDPROCRET = 12,
|
||||
WH_KEYBOARD_LL = 13,
|
||||
WH_MOUSE_LL = 14,
|
||||
}
|
||||
}
|
||||
33
DotNet.Revit.Hook/DataStructure/MOUSEHOOKSTRUCT.cs
Normal file
33
DotNet.Revit.Hook/DataStructure/MOUSEHOOKSTRUCT.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.DataStructure
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局鼠标消息结构体
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct MOUSEHOOKSTRUCT
|
||||
{
|
||||
/// <summary>
|
||||
/// 屏幕坐标
|
||||
/// </summary>
|
||||
public POINT pt;
|
||||
|
||||
/// <summary>
|
||||
/// 如果消息是WM_MOUSEWHEEL,则此成员的高位字是wheel delta。保留低位字。正值表示车轮向前旋转,远离用户; 负值表示车轮向后旋转,朝向用户。
|
||||
/// XBUTTON1 == 0x0001 如果按下或释放第一个X按钮。
|
||||
/// XBUTTON2 == 0x0002 如果按下或释放第一个X按钮。
|
||||
/// </summary>
|
||||
public int mouseData;
|
||||
|
||||
public int flags;
|
||||
|
||||
public uint wHitTestCode;
|
||||
|
||||
public uint dwExtraInfo;
|
||||
}
|
||||
}
|
||||
99
DotNet.Revit.Hook/DataStructure/MouseMessage.cs
Normal file
99
DotNet.Revit.Hook/DataStructure/MouseMessage.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.DataStructure
|
||||
{
|
||||
[Flags]
|
||||
public enum MouseMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// 鼠标左键按下
|
||||
/// </summary>
|
||||
WM_LBUTTONDOWN = 0x0201,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标左键双击
|
||||
/// </summary>
|
||||
WM_LBUTTONDBLCLK = 0x0203,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标左键弹起
|
||||
/// </summary>
|
||||
WM_LBUTTONUP = 0x0202,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标右键单击
|
||||
/// </summary>
|
||||
WM_RBUTTONDOWN = 0x0204,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标右键双击
|
||||
/// </summary>
|
||||
WM_RBUTTONDBLCLK = 0x0206,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标右键弹起
|
||||
/// </summary>
|
||||
WM_RBUTTONUP = 0x0205,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标中键双击
|
||||
/// </summary>
|
||||
WM_MBUTTONDBLCLK = 0x0209,
|
||||
/// <summary>
|
||||
/// 鼠标中键单击
|
||||
/// </summary>
|
||||
WM_MBUTTONDOWN = 0x0207,
|
||||
/// <summary>
|
||||
/// 鼠标中键弹起
|
||||
/// </summary>
|
||||
WM_MBUTTONUP = 0x0208,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标侧键双击时触发.
|
||||
/// </summary>
|
||||
WM_XBUTTONDBLCLK = 0x020D,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标侧键单击时触发
|
||||
/// </summary>
|
||||
WM_XBUTTONDOWN = 0x020B,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标侧键弹起时触发.
|
||||
/// </summary>
|
||||
WM_XBUTTONUP = 0x020C,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标移动
|
||||
/// </summary>
|
||||
WM_MOUSEMOVE = 0x0200,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标滚动
|
||||
/// </summary>
|
||||
WM_MOUSEWHEEL = 0x020A,
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标滚动
|
||||
/// </summary>
|
||||
WM_MOUSEHWHEEL = 0x020E,
|
||||
|
||||
/// <summary>
|
||||
/// 正在失去鼠标捕获的窗口.
|
||||
/// </summary>
|
||||
WM_CAPTURECHANGED = 0x0215,
|
||||
|
||||
/// <summary>
|
||||
/// 当鼠标在非激活窗体按下时.
|
||||
/// </summary>
|
||||
WM_MOUSEACTIVATE = 0x0021,
|
||||
|
||||
/// <summary>
|
||||
/// 当光标悬停在窗口的客户端区域上达到在先前调用TrackMouseEvent中指定的时间段时,发布到窗口。
|
||||
/// </summary>
|
||||
WM_MOUSEHOVER = 0x02A1,
|
||||
}
|
||||
}
|
||||
15
DotNet.Revit.Hook/DataStructure/POINT.cs
Normal file
15
DotNet.Revit.Hook/DataStructure/POINT.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.DataStructure
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct POINT
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
}
|
||||
71
DotNet.Revit.Hook/DotNet.Revit.Hook.csproj
Normal file
71
DotNet.Revit.Hook/DotNet.Revit.Hook.csproj
Normal file
@@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{957A91DD-D04D-4811-82B7-7350A444CB12}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>DotNet.Revit.Hook</RootNamespace>
|
||||
<AssemblyName>DotNet.Revit.Hook</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="RevitAPI">
|
||||
<HintPath>D:\Program Files\Autodesk\Revit 2016\RevitAPI.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="RevitAPIUI">
|
||||
<HintPath>D:\Program Files\Autodesk\Revit 2016\RevitAPIUI.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Achieve\GlobalMouseHook.cs" />
|
||||
<Compile Include="Achieve\HookBase.cs" />
|
||||
<Compile Include="Achieve\MouseHook.cs" />
|
||||
<Compile Include="Achieve\MouseHookBase.cs" />
|
||||
<Compile Include="CommandTest.cs" />
|
||||
<Compile Include="DataStructure\MOUSEHOOKSTRUCT.cs" />
|
||||
<Compile Include="DataStructure\POINT.cs" />
|
||||
<Compile Include="DoubleClickElementEventArgs.cs" />
|
||||
<Compile Include="ElementMonitor.cs" />
|
||||
<Compile Include="Helper\HookHelper.cs" />
|
||||
<Compile Include="DataStructure\HookType.cs" />
|
||||
<Compile Include="Interface\IHook.cs" />
|
||||
<Compile Include="DataStructure\MouseMessage.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
15
DotNet.Revit.Hook/DoubleClickElementEventArgs.cs
Normal file
15
DotNet.Revit.Hook/DoubleClickElementEventArgs.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
|
||||
namespace DotNet.Revit.Hook
|
||||
{
|
||||
public class DoubleClickElementEventArgs : EventArgs
|
||||
{
|
||||
public DoubleClickElementEventArgs(Element elem)
|
||||
{
|
||||
this.Element = elem;
|
||||
}
|
||||
|
||||
public Element Element { get; private set; }
|
||||
}
|
||||
}
|
||||
139
DotNet.Revit.Hook/ElementMonitor.cs
Normal file
139
DotNet.Revit.Hook/ElementMonitor.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Autodesk.Revit.UI;
|
||||
using DotNet.Revit.Hook.Achieve;
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// 元素监控.
|
||||
/// </summary>
|
||||
public class ElementMonitor
|
||||
{
|
||||
private static ElementMonitor m_Instance;
|
||||
private MouseHook m_MouseHook;
|
||||
private bool m_IsMonitor;
|
||||
private UIApplication m_UIApplication;
|
||||
|
||||
private ElementMonitor(UIApplication uiApp)
|
||||
{
|
||||
m_Instance = this;
|
||||
m_UIApplication = uiApp;
|
||||
|
||||
m_MouseHook = new MouseHook();
|
||||
m_MouseHook.Install();
|
||||
|
||||
m_MouseHook.MouseDoubleClick += OnRaiseMouseDoubleClick;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 静态实例,可在入口类判断此实例是否为null,防止重复注册.
|
||||
/// </summary>
|
||||
public static ElementMonitor Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否监控.
|
||||
/// </summary>
|
||||
public bool IsMonitor
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_IsMonitor;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
m_IsMonitor = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当鼠标双击元素时触发此事件.
|
||||
/// </summary>
|
||||
public event HookHandler<DoubleClickElementEventArgs> DoubleClickElement;
|
||||
|
||||
/// <summary>
|
||||
/// 注册元素监控,并指定是否立即监控.
|
||||
/// </summary>
|
||||
public static void Register(UIApplication uiApp)
|
||||
{
|
||||
if (uiApp == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(uiApp));
|
||||
}
|
||||
|
||||
new ElementMonitor(uiApp)
|
||||
{
|
||||
m_IsMonitor = true
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册元素监控,并指定是否立即监控.
|
||||
/// </summary>
|
||||
public static void Register(UIControlledApplication uiControllApp)
|
||||
{
|
||||
if (uiControllApp == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(uiControllApp));
|
||||
}
|
||||
|
||||
var flag = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod;
|
||||
|
||||
var uiApp = (UIApplication)uiControllApp.GetType().InvokeMember("getUIApplication", flag, Type.DefaultBinder, uiControllApp, null);
|
||||
|
||||
Register(uiApp);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 返回1,则拦截鼠标消息,返回0则传递给真正消息接收者.
|
||||
/// </summary>
|
||||
private int OnRaiseMouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
|
||||
{
|
||||
if (this.DoubleClickElement == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!m_IsMonitor || e.Button != MouseButtons.Left || e.Clicks != 2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var uiDoc = m_UIApplication.ActiveUIDocument;
|
||||
|
||||
if (uiDoc == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var elemIds = uiDoc.Selection.GetElementIds();
|
||||
|
||||
if (elemIds.Count == 1)
|
||||
{
|
||||
var elem = uiDoc.Document.GetElement(elemIds.First());
|
||||
|
||||
if (elem == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return this.DoubleClickElement(this, new DoubleClickElementEventArgs(elem));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
56
DotNet.Revit.Hook/Helper/HookHelper.cs
Normal file
56
DotNet.Revit.Hook/Helper/HookHelper.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using DotNet.Revit.Hook.DataStructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.Helper
|
||||
{
|
||||
static class HookHelper
|
||||
{
|
||||
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern int GetCurrentThreadId();
|
||||
|
||||
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern int SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, int dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern int CallNextHookEx(int idHook, int code, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern bool UnhookWindowsHookEx(int idHook);
|
||||
|
||||
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
|
||||
[DllImport("kernel32", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr GetCurrentProcessId();
|
||||
|
||||
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern int GetKeyboardState(byte[] pbKeyState);
|
||||
|
||||
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
|
||||
|
||||
public static int HiWord(IntPtr dword)
|
||||
{
|
||||
return dword.ToInt32() >> 16 & 65535;
|
||||
}
|
||||
|
||||
public static int LoWord(IntPtr dword)
|
||||
{
|
||||
return dword.ToInt32() & 65535;
|
||||
}
|
||||
|
||||
public static T ToStruct<T>(this IntPtr lParam) where T : struct
|
||||
{
|
||||
return (T)Marshal.PtrToStructure(lParam, typeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
public delegate int HookHandler<TEvent>(object sender, TEvent e) where TEvent : EventArgs;
|
||||
}
|
||||
23
DotNet.Revit.Hook/Interface/IHook.cs
Normal file
23
DotNet.Revit.Hook/Interface/IHook.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DotNet.Revit.Hook.Interface
|
||||
{
|
||||
/// <summary>
|
||||
/// IHook
|
||||
/// </summary>
|
||||
public interface IHook
|
||||
{
|
||||
/// <summary>
|
||||
/// Installs this Hook.
|
||||
/// </summary>
|
||||
void Install();
|
||||
|
||||
/// <summary>
|
||||
/// Uninstalls this Hook.
|
||||
/// </summary>
|
||||
void Uninstall();
|
||||
}
|
||||
}
|
||||
36
DotNet.Revit.Hook/Properties/AssemblyInfo.cs
Normal file
36
DotNet.Revit.Hook/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("DotNet.Revit.Hook")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("DotNet.Revit.Hook")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2017")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("957a91dd-d04d-4811-82b7-7350a444cb12")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user