134 lines
3.8 KiB
C#
134 lines
3.8 KiB
C#
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
using Autodesk.Windows;
|
|
|
|
namespace ShrlAlgo.Addin.Test;
|
|
|
|
/// <summary>
|
|
/// 全局鼠标监视器类,用于监视全局鼠标事件。
|
|
/// </summary>
|
|
internal class GlobalMouseWatcher
|
|
{
|
|
private const int WH_MOUSE_LL = 14;
|
|
private const int WM_LBUTTONDOWN = 0x0201;
|
|
private const int WM_RBUTTONDOWN = 0x0204;
|
|
|
|
private IntPtr _hookID = IntPtr.Zero;
|
|
|
|
/// <summary>
|
|
/// 构造函数,设置鼠标钩子。
|
|
/// </summary>
|
|
public GlobalMouseWatcher()
|
|
{
|
|
_hookID = SetHook(MouseHookCallback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 析构函数,移除鼠标钩子。
|
|
/// </summary>
|
|
~GlobalMouseWatcher()
|
|
{
|
|
UnhookWindowsHookEx(_hookID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置鼠标钩子。
|
|
/// </summary>
|
|
/// <param name="proc">钩子过程。</param>
|
|
/// <returns>钩子的句柄。</returns>
|
|
private IntPtr SetHook(HookProc proc)
|
|
{
|
|
using (var curProcess = Process.GetCurrentProcess())
|
|
using (var curModule = curProcess.MainModule)
|
|
{
|
|
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 钩子过程的委托。
|
|
/// </summary>
|
|
private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
|
|
/// <summary>
|
|
/// 鼠标钩子回调函数。
|
|
/// </summary>
|
|
private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
|
{
|
|
if (nCode >= 0 && wParam == (IntPtr)WM_RBUTTONDOWN)
|
|
{
|
|
CompleteMultiSelection();
|
|
}
|
|
return CallNextHookEx(_hookID, nCode, wParam, lParam);
|
|
}
|
|
|
|
#region Win32 API
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern IntPtr GetModuleHandle(string lpModuleName);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
|
|
private static extern bool EnumChildWindows(IntPtr hwndParent, CallBack lpEnumFunc, IntPtr lParam);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
|
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);
|
|
|
|
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
|
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 回调函数的委托。
|
|
/// </summary>
|
|
public delegate bool CallBack(IntPtr hwnd, int lParam);
|
|
|
|
/// <summary>
|
|
/// 完成多选操作。
|
|
/// </summary>
|
|
private static void CompleteMultiSelection()
|
|
{
|
|
var rvtwindow = ComponentManager.ApplicationWindow;
|
|
var list = new List<IntPtr>();
|
|
var flag = EnumChildWindows(
|
|
rvtwindow,
|
|
(hwnd, l) =>
|
|
{
|
|
var windowText = new StringBuilder(200);
|
|
GetWindowText(hwnd, windowText, windowText.Capacity);
|
|
var className = new StringBuilder(200);
|
|
GetClassName(hwnd, className, className.Capacity);
|
|
if (
|
|
(
|
|
windowText.ToString().Equals("完成", StringComparison.Ordinal)
|
|
|| windowText.ToString().Equals("Finish", StringComparison.Ordinal)
|
|
) && className.ToString().Contains("Button")
|
|
)
|
|
{
|
|
list.Add(hwnd);
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
new IntPtr(0)
|
|
);
|
|
|
|
var complete = list.FirstOrDefault();
|
|
SendMessage(complete, 245, 0, 0);
|
|
}
|
|
}
|