chore: use file-scoped namespace
This commit is contained in:
1 parent
99d2fafb72
commit
bbf1b7d4c6
748 files changed
+57929
-58678
No files matched your search
@@ -2,56 +2,55 @@
|
||||
using System.Windows.Interop;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
namespace HandyControl.Tools;
|
||||
|
||||
public class ClipboardHook
|
||||
{
|
||||
public class ClipboardHook
|
||||
public static event Action ContentChanged;
|
||||
|
||||
private static HwndSource HWndSource;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
public static event Action ContentChanged;
|
||||
|
||||
private static HwndSource HWndSource;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
if (HookId == IntPtr.Zero)
|
||||
{
|
||||
if (HookId == IntPtr.Zero)
|
||||
HookId = WindowHelper.CreateHandle();
|
||||
HWndSource = HwndSource.FromHwnd(HookId);
|
||||
if (HWndSource != null)
|
||||
{
|
||||
HookId = WindowHelper.CreateHandle();
|
||||
HWndSource = HwndSource.FromHwnd(HookId);
|
||||
if (HWndSource != null)
|
||||
{
|
||||
HWndSource.AddHook(WinProc);
|
||||
InteropMethods.AddClipboardFormatListener(HookId);
|
||||
}
|
||||
}
|
||||
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count++;
|
||||
HWndSource.AddHook(WinProc);
|
||||
InteropMethods.AddClipboardFormatListener(HookId);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
HWndSource.RemoveHook(WinProc);
|
||||
InteropMethods.RemoveClipboardFormatListener(HookId);
|
||||
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
|
||||
{
|
||||
if (msg == InteropValues.WM_CLIPBOARDUPDATE)
|
||||
{
|
||||
ContentChanged?.Invoke();
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
HWndSource.RemoveHook(WinProc);
|
||||
InteropMethods.RemoveClipboardFormatListener(HookId);
|
||||
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
|
||||
{
|
||||
if (msg == InteropValues.WM_CLIPBOARDUPDATE)
|
||||
{
|
||||
ContentChanged?.Invoke();
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
@@ -4,102 +4,101 @@ using System.Runtime.InteropServices;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
namespace HandyControl.Tools;
|
||||
|
||||
public class KeyboardHook
|
||||
{
|
||||
public class KeyboardHook
|
||||
public static event EventHandler<KeyboardHookEventArgs> KeyDown;
|
||||
|
||||
public static event EventHandler<KeyboardHookEventArgs> KeyUp;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static readonly InteropValues.HookProc Proc = HookCallback;
|
||||
|
||||
private static int VirtualKey;
|
||||
|
||||
private static readonly IntPtr KeyDownIntPtr = (IntPtr) InteropValues.WM_KEYDOWN;
|
||||
|
||||
private static readonly IntPtr KeyUpIntPtr = (IntPtr) InteropValues.WM_KEYUP;
|
||||
|
||||
private static readonly IntPtr SyskeyDownIntPtr = (IntPtr) InteropValues.WM_SYSKEYDOWN;
|
||||
|
||||
private static readonly IntPtr SyskeyUpIntPtr = (IntPtr) InteropValues.WM_SYSKEYUP;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
public static event EventHandler<KeyboardHookEventArgs> KeyDown;
|
||||
|
||||
public static event EventHandler<KeyboardHookEventArgs> KeyUp;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static readonly InteropValues.HookProc Proc = HookCallback;
|
||||
|
||||
private static int VirtualKey;
|
||||
|
||||
private static readonly IntPtr KeyDownIntPtr = (IntPtr) InteropValues.WM_KEYDOWN;
|
||||
|
||||
private static readonly IntPtr KeyUpIntPtr = (IntPtr) InteropValues.WM_KEYUP;
|
||||
|
||||
private static readonly IntPtr SyskeyDownIntPtr = (IntPtr) InteropValues.WM_SYSKEYDOWN;
|
||||
|
||||
private static readonly IntPtr SyskeyUpIntPtr = (IntPtr) InteropValues.WM_SYSKEYUP;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
if (HookId == IntPtr.Zero)
|
||||
{
|
||||
if (HookId == IntPtr.Zero)
|
||||
{
|
||||
HookId = SetHook(Proc);
|
||||
}
|
||||
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count++;
|
||||
}
|
||||
HookId = SetHook(Proc);
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
InteropMethods.UnhookWindowsHookEx(HookId);
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr SetHook(InteropValues.HookProc proc)
|
||||
{
|
||||
using var curProcess = Process.GetCurrentProcess();
|
||||
using var curModule = curProcess.MainModule;
|
||||
|
||||
if (curModule != null)
|
||||
{
|
||||
return InteropMethods.SetWindowsHookEx((int) InteropValues.HookType.WH_KEYBOARD_LL, proc,
|
||||
InteropMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode >= 0)
|
||||
{
|
||||
if (wParam == KeyDownIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
|
||||
}
|
||||
}
|
||||
else if (wParam == SyskeyDownIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
|
||||
}
|
||||
}
|
||||
else if (wParam == KeyUpIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
VirtualKey = -1;
|
||||
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
|
||||
}
|
||||
else if (wParam == SyskeyUpIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
VirtualKey = -1;
|
||||
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
|
||||
}
|
||||
}
|
||||
return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
InteropMethods.UnhookWindowsHookEx(HookId);
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr SetHook(InteropValues.HookProc proc)
|
||||
{
|
||||
using var curProcess = Process.GetCurrentProcess();
|
||||
using var curModule = curProcess.MainModule;
|
||||
|
||||
if (curModule != null)
|
||||
{
|
||||
return InteropMethods.SetWindowsHookEx((int) InteropValues.HookType.WH_KEYBOARD_LL, proc,
|
||||
InteropMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode >= 0)
|
||||
{
|
||||
if (wParam == KeyDownIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
|
||||
}
|
||||
}
|
||||
else if (wParam == SyskeyDownIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
if (VirtualKey != virtualKey)
|
||||
{
|
||||
VirtualKey = virtualKey;
|
||||
KeyDown?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
|
||||
}
|
||||
}
|
||||
else if (wParam == KeyUpIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
VirtualKey = -1;
|
||||
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, false));
|
||||
}
|
||||
else if (wParam == SyskeyUpIntPtr)
|
||||
{
|
||||
var virtualKey = Marshal.ReadInt32(lParam);
|
||||
VirtualKey = -1;
|
||||
KeyUp?.Invoke(null, new KeyboardHookEventArgs(virtualKey, true));
|
||||
}
|
||||
}
|
||||
return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
}
|
||||
}
|
||||
@@ -4,65 +4,64 @@ using System.Runtime.InteropServices;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
namespace HandyControl.Tools;
|
||||
|
||||
internal class MouseHook
|
||||
{
|
||||
internal class MouseHook
|
||||
public static event EventHandler<MouseHookEventArgs> StatusChanged;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static readonly InteropValues.HookProc Proc = HookCallback;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
public static event EventHandler<MouseHookEventArgs> StatusChanged;
|
||||
|
||||
private static IntPtr HookId = IntPtr.Zero;
|
||||
|
||||
private static readonly InteropValues.HookProc Proc = HookCallback;
|
||||
|
||||
private static int Count;
|
||||
|
||||
public static void Start()
|
||||
if (HookId == IntPtr.Zero)
|
||||
{
|
||||
if (HookId == IntPtr.Zero)
|
||||
{
|
||||
HookId = SetHook(Proc);
|
||||
}
|
||||
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count++;
|
||||
}
|
||||
HookId = SetHook(Proc);
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
if (HookId != IntPtr.Zero)
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
InteropMethods.UnhookWindowsHookEx(HookId);
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr SetHook(InteropValues.HookProc proc)
|
||||
{
|
||||
using var curProcess = Process.GetCurrentProcess();
|
||||
using var curModule = curProcess.MainModule;
|
||||
|
||||
if (curModule != null)
|
||||
{
|
||||
return InteropMethods.SetWindowsHookEx((int) InteropValues.HookType.WH_MOUSE_LL, proc,
|
||||
InteropMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode < 0) return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
var hookStruct = (InteropValues.MOUSEHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(InteropValues.MOUSEHOOKSTRUCT));
|
||||
StatusChanged?.Invoke(null, new MouseHookEventArgs
|
||||
{
|
||||
MessageType = (MouseHookMessageType) wParam,
|
||||
Point = new InteropValues.POINT(hookStruct.pt.X, hookStruct.pt.Y)
|
||||
});
|
||||
|
||||
return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
Count--;
|
||||
if (Count < 1)
|
||||
{
|
||||
InteropMethods.UnhookWindowsHookEx(HookId);
|
||||
HookId = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr SetHook(InteropValues.HookProc proc)
|
||||
{
|
||||
using var curProcess = Process.GetCurrentProcess();
|
||||
using var curModule = curProcess.MainModule;
|
||||
|
||||
if (curModule != null)
|
||||
{
|
||||
return InteropMethods.SetWindowsHookEx((int) InteropValues.HookType.WH_MOUSE_LL, proc,
|
||||
InteropMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
if (nCode < 0) return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
var hookStruct = (InteropValues.MOUSEHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(InteropValues.MOUSEHOOKSTRUCT));
|
||||
StatusChanged?.Invoke(null, new MouseHookEventArgs
|
||||
{
|
||||
MessageType = (MouseHookMessageType) wParam,
|
||||
Point = new InteropValues.POINT(hookStruct.pt.X, hookStruct.pt.Y)
|
||||
});
|
||||
|
||||
return InteropMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
|
||||
}
|
||||
}
|
||||
@@ -4,50 +4,49 @@ using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using HandyControl.Tools.Interop;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
namespace HandyControl.Tools;
|
||||
|
||||
public class SystemMenuHook
|
||||
{
|
||||
public class SystemMenuHook
|
||||
private static readonly Dictionary<int, HwndSource> DataDic = new();
|
||||
|
||||
public static event Action<int> Click;
|
||||
|
||||
public static void Insert(int index, int id, string text, Window window)
|
||||
{
|
||||
private static readonly Dictionary<int, HwndSource> DataDic = new();
|
||||
|
||||
public static event Action<int> Click;
|
||||
|
||||
public static void Insert(int index, int id, string text, Window window)
|
||||
var hookId = window.GetHandle();
|
||||
var source = HwndSource.FromHwnd(hookId);
|
||||
if (source != null)
|
||||
{
|
||||
var hookId = window.GetHandle();
|
||||
var source = HwndSource.FromHwnd(hookId);
|
||||
if (source != null)
|
||||
{
|
||||
DataDic[id] = source;
|
||||
InteropMethods.InsertMenu(InteropMethods.GetSystemMenu(hookId, false), index, InteropValues.MF_BYPOSITION, id, text);
|
||||
source.AddHook(WinProc);
|
||||
}
|
||||
}
|
||||
|
||||
public static void InsertSeperator(int index, Window window) => InteropMethods.InsertMenu(
|
||||
InteropMethods.GetSystemMenu(window.GetHandle(), false), index,
|
||||
InteropValues.MF_BYPOSITION | InteropValues.MF_SEPARATOR, 0, "");
|
||||
|
||||
public static void Remove(int id)
|
||||
{
|
||||
if (DataDic.TryGetValue(id, out var data))
|
||||
{
|
||||
data.RemoveHook(WinProc);
|
||||
DataDic.Remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
|
||||
{
|
||||
if (msg == InteropValues.WM_SYSCOMMAND)
|
||||
{
|
||||
var id = wparam.ToInt32();
|
||||
if (DataDic.ContainsKey(id))
|
||||
{
|
||||
Click?.Invoke(id);
|
||||
}
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
DataDic[id] = source;
|
||||
InteropMethods.InsertMenu(InteropMethods.GetSystemMenu(hookId, false), index, InteropValues.MF_BYPOSITION, id, text);
|
||||
source.AddHook(WinProc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void InsertSeperator(int index, Window window) => InteropMethods.InsertMenu(
|
||||
InteropMethods.GetSystemMenu(window.GetHandle(), false), index,
|
||||
InteropValues.MF_BYPOSITION | InteropValues.MF_SEPARATOR, 0, "");
|
||||
|
||||
public static void Remove(int id)
|
||||
{
|
||||
if (DataDic.TryGetValue(id, out var data))
|
||||
{
|
||||
data.RemoveHook(WinProc);
|
||||
DataDic.Remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
private static IntPtr WinProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
|
||||
{
|
||||
if (msg == InteropValues.WM_SYSCOMMAND)
|
||||
{
|
||||
var id = wparam.ToInt32();
|
||||
if (DataDic.ContainsKey(id))
|
||||
{
|
||||
Click?.Invoke(id);
|
||||
}
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user