Neuerstellung
This commit is contained in:
10
NHotkey/NHotkey.WindowsForms/ExtensionAttribute.cs
Normal file
10
NHotkey/NHotkey.WindowsForms/ExtensionAttribute.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
// Enables extension methods in assembly that targets .NET 2.0
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Method)]
|
||||
internal sealed class ExtensionAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
12
NHotkey/NHotkey.WindowsForms/Extensions.cs
Normal file
12
NHotkey/NHotkey.WindowsForms/Extensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace NHotkey.WindowsForms
|
||||
{
|
||||
static class Extensions
|
||||
{
|
||||
public static bool HasFlag(this Keys keys, Keys flag)
|
||||
{
|
||||
return (keys & flag) == flag;
|
||||
}
|
||||
}
|
||||
}
|
||||
91
NHotkey/NHotkey.WindowsForms/HotkeyManager.cs
Normal file
91
NHotkey/NHotkey.WindowsForms/HotkeyManager.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace NHotkey.WindowsForms
|
||||
{
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage(
|
||||
"Microsoft.Design",
|
||||
"CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
|
||||
Justification = "This is a singleton; disposing it would break it")]
|
||||
public class HotkeyManager : HotkeyManagerBase
|
||||
{
|
||||
#region Singleton implementation
|
||||
|
||||
public static HotkeyManager Current { get { return LazyInitializer.Instance; } }
|
||||
|
||||
private static class LazyInitializer
|
||||
{
|
||||
static LazyInitializer() { }
|
||||
public static readonly HotkeyManager Instance = new HotkeyManager();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
|
||||
private readonly MessageWindow _messageWindow;
|
||||
|
||||
private HotkeyManager()
|
||||
{
|
||||
_messageWindow = new MessageWindow(this);
|
||||
SetHwnd(_messageWindow.Handle);
|
||||
}
|
||||
|
||||
public void AddOrReplace(string name, Keys keys, bool noRepeat, EventHandler<HotkeyEventArgs> handler)
|
||||
{
|
||||
var flags = GetFlags(keys, noRepeat);
|
||||
var vk = unchecked((uint)(keys & ~Keys.Modifiers));
|
||||
AddOrReplace(name, vk, flags, handler);
|
||||
}
|
||||
|
||||
public void AddOrReplace(string name, Keys keys, EventHandler<HotkeyEventArgs> handler)
|
||||
{
|
||||
AddOrReplace(name, keys, false, handler);
|
||||
}
|
||||
|
||||
private static HotkeyFlags GetFlags(Keys hotkey, bool noRepeat)
|
||||
{
|
||||
var noMod = hotkey & ~Keys.Modifiers;
|
||||
var flags = HotkeyFlags.None;
|
||||
if (hotkey.HasFlag(Keys.Alt))
|
||||
flags |= HotkeyFlags.Alt;
|
||||
if (hotkey.HasFlag(Keys.Control))
|
||||
flags |= HotkeyFlags.Control;
|
||||
if (hotkey.HasFlag(Keys.Shift))
|
||||
flags |= HotkeyFlags.Shift;
|
||||
if (noMod == Keys.LWin || noMod == Keys.RWin)
|
||||
flags |= HotkeyFlags.Windows;
|
||||
if (noRepeat)
|
||||
flags |= HotkeyFlags.NoRepeat;
|
||||
return flags;
|
||||
}
|
||||
|
||||
class MessageWindow : ContainerControl
|
||||
{
|
||||
private readonly HotkeyManager _hotkeyManager;
|
||||
|
||||
public MessageWindow(HotkeyManager hotkeyManager)
|
||||
{
|
||||
_hotkeyManager = hotkeyManager;
|
||||
}
|
||||
|
||||
protected override CreateParams CreateParams
|
||||
{
|
||||
get
|
||||
{
|
||||
var parameters = base.CreateParams;
|
||||
parameters.Parent = HwndMessage;
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void WndProc(ref Message m)
|
||||
{
|
||||
bool handled = false;
|
||||
Hotkey hotkey;
|
||||
m.Result = _hotkeyManager.HandleHotkeyMessage(Handle, m.Msg, m.WParam, m.LParam, ref handled, out hotkey);
|
||||
if (!handled)
|
||||
base.WndProc(ref m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
NHotkey/NHotkey.WindowsForms/NHotkey.WindowsForms.csproj
Normal file
12
NHotkey/NHotkey.WindowsForms/NHotkey.WindowsForms.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net40;net45;netcoreapp3.0</TargetFrameworks>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Package properties">
|
||||
<Description>A managed library to handle global hotkeys in Windows Forms applications. This package contains the concrete HotkeyManager implementation for Windows Forms.</Description>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NHotkey\NHotkey.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user