Merge branch 'master' into pr/84

This commit is contained in:
NaBian committed 2019-05-29 22:07:18 +08:00
commit bdce2a84c7
160 files changed
+2186 -1603

No files matched your search

@@ -1,47 +0,0 @@
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using HandyControl.Interactivity;
namespace HandyControl.Controls
{
public class LinkElement
{
public static readonly DependencyProperty LinkProperty = DependencyProperty.RegisterAttached(
"Link", typeof(string), typeof(LinkElement), new PropertyMetadata(default(string), OnLinkChanged));
private static void OnLinkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement element)
{
CreateTrigger(element, (string) e.NewValue);
}
}
private static readonly DependencyProperty CommandBindingProperty = DependencyProperty.RegisterAttached(
"CommandBinding", typeof(CommandBinding), typeof(LinkElement), new PropertyMetadata(default(CommandBinding)));
private static void SetCommandBinding(DependencyObject element, CommandBinding value)
=> element.SetValue(CommandBindingProperty, value);
private static CommandBinding GetCommandBinding(DependencyObject element)
=> (CommandBinding) element.GetValue(CommandBindingProperty);
private static void CreateTrigger(UIElement element, string link)
{
element.CommandBindings.Remove(GetCommandBinding(element));
element.SetCurrentValue(CommandBindingProperty, DependencyProperty.UnsetValue);
if (string.IsNullOrEmpty(link)) return;
var commandBinding = new CommandBinding(ControlCommands.OpenLink, (sender, args) => Process.Start(link));
SetCommandBinding(element, commandBinding);
element.CommandBindings.Add(commandBinding);
}
public static void SetLink(DependencyObject element, string value)
=> element.SetValue(LinkProperty, value);
public static string GetLink(DependencyObject element)
=> (string) element.GetValue(LinkProperty);
}
}
@@ -453,7 +453,7 @@ namespace HandyControl.Controls
var button = new Button
{
Margin = new Thickness(0, 0, 12, 12),
Margin = new Thickness(6),
Style = ResourceHelper.GetResource<Style>(ResourceToken.ButtonCustom),
Content = new Border
{
@@ -137,11 +137,6 @@ namespace HandyControl.Controls
/// </summary>
private double _imgWidHeiScale;
/// <summary>
/// 是否已经初始化
/// </summary>
private bool _isLoaded;
/// <summary>
/// 图片是否倾斜
/// </summary>
@@ -168,8 +163,6 @@ namespace HandyControl.Controls
public ImageViewer()
{
Loaded += ImageViewer_OnLoaded;
CommandBindings.Add(new CommandBinding(ControlCommands.Save, ButtonSave_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.Open, ButtonWindowsOpen_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.Restore, ButtonActual_OnClick));
@@ -177,6 +170,8 @@ namespace HandyControl.Controls
CommandBindings.Add(new CommandBinding(ControlCommands.Enlarge, ButtonEnlarge_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.RotateLeft, ButtonRotateLeft_OnClick));
CommandBindings.Add(new CommandBinding(ControlCommands.RotateRight, ButtonRotateRight_OnClick));
Loaded += (s, e) => Init();
}
/// <summary>
@@ -217,7 +212,13 @@ namespace HandyControl.Controls
"ShowImgMap", typeof(bool), typeof(ImageViewer), new PropertyMetadata(ValueBoxes.FalseBox));
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register(
"ImageSource", typeof(BitmapFrame), typeof(ImageViewer), new PropertyMetadata(default(BitmapFrame)));
"ImageSource", typeof(BitmapFrame), typeof(ImageViewer), new PropertyMetadata(default(BitmapFrame), OnImageSourceChanged));
private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (ImageViewer) d;
ctl.Init();
}
public static readonly DependencyProperty IsFullScreenProperty = DependencyProperty.Register(
"IsFullScreen", typeof(bool), typeof(ImageViewer), new PropertyMetadata(ValueBoxes.FalseBox));
@@ -410,7 +411,7 @@ namespace HandyControl.Controls
if (_imageMain != null)
{
var t = new RotateTransform();
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) {Source = this});
BindingOperations.SetBinding(t, RotateTransform.AngleProperty, new Binding(ImageRotateProperty.Name) { Source = this });
_imageMain.LayoutTransform = t;
_imageMain.MouseLeftButtonDown += ImageMain_OnMouseLeftButtonDown;
}
@@ -422,6 +423,8 @@ namespace HandyControl.Controls
_canvasSmallImg.MouseMove += CanvasSmallImg_OnMouseMove;
_canvasSmallImg.MouseLeave += CanvasSmallImg_OnMouseLeave;
}
_borderSmallIsLoaded = false;
}
private static void OnImageScaleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -439,7 +442,7 @@ namespace HandyControl.Controls
/// </summary>
private void Init()
{
if (ImageSource == null) return;
if (ImageSource == null || !IsLoaded) return;
double width;
double height;
@@ -486,8 +489,10 @@ namespace HandyControl.Controls
ImageMargin = new Thickness((ActualWidth - ImageWidth) / 2, (ActualHeight - ImageHeight) / 2, 0, 0);
_imgActualScale = 1;
_imgActualScale = ImageScale;
_imgActualMargin = ImageMargin;
InitBorderSmall();
}
private void ButtonActual_OnClick(object sender, RoutedEventArgs e)
@@ -571,8 +576,11 @@ namespace HandyControl.Controls
{
base.OnRenderSizeChanged(sizeInfo);
if (!_isLoaded) return;
OnRenderSizeChanged();
}
private void OnRenderSizeChanged()
{
if (ImageWidth < 0.001 || ImageHeight < 0.001) return;
_canMoveX = true;
@@ -601,13 +609,6 @@ namespace HandyControl.Controls
MoveSmallImg(_imgSmallMouseDownMargin.Left, _imgSmallMouseDownMargin.Top);
}
private void ImageViewer_OnLoaded(object sender, RoutedEventArgs e)
{
if (_isLoaded) return;
Init();
_isLoaded = true;
}
private void ImageMain_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_imgMouseDownPoint = Mouse.GetPosition(_panelMain);
@@ -649,6 +650,7 @@ namespace HandyControl.Controls
/// </summary>
private void InitBorderSmall()
{
if (_canvasSmallImg == null) return;
var scaleWindow = _canvasSmallImg.MaxWidth / _canvasSmallImg.MaxHeight;
if (_imgWidHeiScale > scaleWindow)
{
@@ -773,7 +775,7 @@ namespace HandyControl.Controls
{
_imgActualRotate = rotate;
_isOblique = ((int) _imgActualRotate - 90) % 180 == 0;
_isOblique = ((int)_imgActualRotate - 90) % 180 == 0;
ShowSmallImgInternal = false;
Init();
InitBorderSmall();
@@ -159,6 +159,10 @@ namespace HandyControl.Controls
{
var ctl = (NumericUpDown) d;
var v = (double) e.NewValue;
if (ctl._textBox != null)
{
ctl._textBox.Text = v.ToString();
}
ctl.OnValueChanged(new FunctionEventArgs<double>(ValueChangedEvent, ctl)
{
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
@@ -30,7 +31,7 @@ namespace HandyControl.Controls
private TabPanel _headerPanel;
private ScrollViewer _scrollviewerOverflow;
private ScrollViewer _scrollViewerOverflow;
private Button _buttonScrollLeft;
@@ -146,6 +147,36 @@ namespace HandyControl.Controls
set => SetValue(IsScrollableProperty, value);
}
/// <summary>
/// 是否显示溢出按钮
/// </summary>
public static readonly DependencyProperty ShowOverflowButtonProperty = DependencyProperty.Register(
"ShowOverflowButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.TrueBox));
/// <summary>
/// 是否显示溢出按钮
/// </summary>
public bool ShowOverflowButton
{
get => (bool) GetValue(ShowOverflowButtonProperty);
set => SetValue(ShowOverflowButtonProperty, value);
}
/// <summary>
/// 是否显示滚动按钮
/// </summary>
public static readonly DependencyProperty ShowScrollButtonProperty = DependencyProperty.Register(
"ShowScrollButton", typeof(bool), typeof(TabControl), new PropertyMetadata(ValueBoxes.FalseBox));
/// <summary>
/// 是否显示滚动按钮
/// </summary>
public bool ShowScrollButton
{
get => (bool) GetValue(ShowScrollButtonProperty);
set => SetValue(ShowScrollButtonProperty, value);
}
/// <summary>
/// 可见的标签数量
/// </summary>
@@ -164,7 +195,7 @@ namespace HandyControl.Controls
if (!IsEnableTabFill)
{
_itemShowCount = (int)(ActualWidth / TabItemWidth);
_buttonOverflow.Show(Items.Count > 0 && Items.Count >= _itemShowCount);
_buttonOverflow?.Show(ShowOverflowButton && Items.Count > 0 && Items.Count >= _itemShowCount);
}
if (IsInternalAction)
@@ -214,7 +245,7 @@ namespace HandyControl.Controls
if (IsEnableTabFill) return;
_buttonOverflow = GetTemplateChild(OverflowButtonKey) as ContextMenuToggleButton;
_scrollviewerOverflow = GetTemplateChild(OverflowScrollviewer) as ScrollViewer;
_scrollViewerOverflow = GetTemplateChild(OverflowScrollviewer) as ScrollViewer;
_buttonScrollLeft = GetTemplateChild(ScrollButtonLeft) as Button;
_buttonScrollRight = GetTemplateChild(ScrollButtonRight) as Button;
@@ -224,7 +255,7 @@ namespace HandyControl.Controls
if (_buttonOverflow != null)
{
_itemShowCount = (int)(ActualWidth / TabItemWidth);
_buttonOverflow.Show(Items.Count > 0 && Items.Count >= _itemShowCount);
_buttonOverflow.Show(ShowOverflowButton && Items.Count > 0 && Items.Count >= _itemShowCount);
var menu = new ContextMenu
{
@@ -240,12 +271,12 @@ namespace HandyControl.Controls
private void Menu_Closed(object sender, RoutedEventArgs e) => _buttonOverflow.IsChecked = false;
private void ButtonScrollRight_Click(object sender, RoutedEventArgs e) =>
_scrollviewerOverflow.ScrollToHorizontalOffsetInternal(Math.Min(
_scrollviewerOverflow.CurrentHorizontalOffset + TabItemWidth, _scrollviewerOverflow.ScrollableWidth));
_scrollViewerOverflow.ScrollToHorizontalOffsetInternal(Math.Min(
_scrollViewerOverflow.CurrentHorizontalOffset + TabItemWidth, _scrollViewerOverflow.ScrollableWidth));
private void ButtonScrollLeft_Click(object sender, RoutedEventArgs e) =>
_scrollviewerOverflow.ScrollToHorizontalOffsetInternal(Math.Max(
_scrollviewerOverflow.CurrentHorizontalOffset - TabItemWidth, 0));
_scrollViewerOverflow.ScrollToHorizontalOffsetInternal(Math.Max(
_scrollViewerOverflow.CurrentHorizontalOffset - TabItemWidth, 0));
private void ButtonOverflow_Click(object sender, RoutedEventArgs e)
{
@@ -288,11 +319,37 @@ namespace HandyControl.Controls
}
}
internal double GetHorizontalOffset() => _scrollviewerOverflow?.CurrentHorizontalOffset ?? 0;
internal double GetHorizontalOffset() => _scrollViewerOverflow?.CurrentHorizontalOffset ?? 0;
internal void UpdateScroll() => _scrollviewerOverflow?.RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, Environment.TickCount, 0)
internal void UpdateScroll() => _scrollViewerOverflow?.RaiseEvent(new MouseWheelEventArgs(Mouse.PrimaryDevice, Environment.TickCount, 0)
{
RoutedEvent = MouseWheelEvent
});
internal void CloseAllItems() => CloseOtherItems(null);
internal void CloseOtherItems(TabItem currentItem)
{
IsInternalAction = true;
var enumerator = ((IEnumerable)Items).GetEnumerator();
while (enumerator.MoveNext())
{
var item = enumerator.Current;
if (!Equals(item, currentItem) && item != null)
{
var argsClosing = new CancelRoutedEventArgs(TabItem.ClosingEvent, item);
var elment = item as UIElement;
if (elment != null)
{
elment.RaiseEvent(argsClosing);
if (argsClosing.Cancel) return;
}
elment?.RaiseEvent(new RoutedEventArgs(TabItem.ClosedEvent, item));
Items.Remove(item);
enumerator = ((IEnumerable)Items).GetEnumerator();
}
}
}
}
}
@@ -1,10 +1,10 @@
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
@@ -130,26 +130,11 @@ namespace HandyControl.Controls
public TabItem()
{
CommandBindings.Add(new CommandBinding(ControlCommands.Close, Close));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll, (s, e) =>
{
TabControlParent.IsInternalAction = true;
TabControlParent.Items.Clear();
}));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther, (s, e) =>
{
TabControlParent.IsInternalAction = true;
var enumerator = ((IEnumerable)TabControlParent.Items).GetEnumerator();
while (enumerator.MoveNext())
{
var item = enumerator.Current;
if (!Equals(item) && item != null)
{
TabControlParent.Items.Remove(item);
enumerator = ((IEnumerable)TabControlParent.Items).GetEnumerator();
}
}
}));
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close()));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseAll,
(s, e) => { TabControlParent.CloseAllItems(); }));
CommandBindings.Add(new CommandBinding(ControlCommands.CloseOther,
(s, e) => { TabControlParent.CloseOtherItems(this); }));
}
private TabControl TabControlParent => new Lazy<TabControl>(() => ItemsControl.ItemsControlFromItemContainer(this) as TabControl).Value;
@@ -161,11 +146,12 @@ namespace HandyControl.Controls
Focus();
}
/// <summary>
/// 关闭
/// </summary>
private void Close(object sender, RoutedEventArgs e)
internal void Close()
{
var argsClosing = new CancelRoutedEventArgs(ClosingEvent, this);
RaiseEvent(argsClosing);
if (argsClosing.Cancel) return;
if (TabControlParent.IsEnableAnimation)
{
TabPanel.ClearValue(TabPanel.FluidMoveDurationProperty);
@@ -175,6 +161,7 @@ namespace HandyControl.Controls
TabPanel.FluidMoveDuration = new Duration(TimeSpan.FromSeconds(0));
}
TabControlParent.IsInternalAction = true;
RaiseEvent(new RoutedEventArgs(ClosedEvent, this));
TabControlParent.Items.Remove(this);
}
@@ -300,5 +287,21 @@ namespace HandyControl.Controls
return result > maxIndex ? maxIndex : result;
}
public static readonly RoutedEvent ClosingEvent = EventManager.RegisterRoutedEvent("Closing", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
public event EventHandler Closing
{
add => AddHandler(ClosingEvent, value);
remove => RemoveHandler(ClosingEvent, value);
}
public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(EventHandler), typeof(TabItem));
public event EventHandler Closed
{
add => AddHandler(ClosedEvent, value);
remove => RemoveHandler(ClosedEvent, value);
}
}
}
@@ -6,7 +6,6 @@ using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
namespace HandyControl.Controls
@@ -24,6 +24,8 @@ namespace HandyControl.Controls
private readonly Dictionary<object, TransferEntry> _entryDic = new Dictionary<object, TransferEntry>();
private readonly List<TransferEntry> _entryList = new List<TransferEntry>();
private SimpleItemsControl _itemsOrigin;
private SimpleItemsControl _itemsSelected;
@@ -79,8 +81,8 @@ namespace HandyControl.Controls
{
if (_initEnumerable != null)
{
OnItemsSourceChanged(null, _itemsSourceInternal);
_itemsSourceInternal = null;
OnItemsSourceChanged(null, _initEnumerable);
_initEnumerable = null;
}
if (_initArgs != null)
@@ -89,7 +91,10 @@ namespace HandyControl.Controls
_initArgs = null;
}
Refresh();
if (_itemsOrigin.Items.Count == 0)
{
Refresh();
}
}
}
@@ -110,6 +115,7 @@ namespace HandyControl.Controls
SelectedItem = selected
};
_entryDic.Add(item, entry);
_entryList.Add(entry);
_itemsOrigin.Items.Add(origin);
_itemsSelected.Items.Add(selected);
}
@@ -121,12 +127,14 @@ namespace HandyControl.Controls
_itemsOrigin.Items.Remove(entry.OriginItem);
_itemsSelected.Items.Remove(entry.SelectedItem);
_entryDic.Remove(item);
_entryList.Remove(entry);
}
}
private void ClearItems()
{
_entryDic.Clear();
_entryList.Clear();
_itemsOrigin.Items.Clear();
_itemsSelected.Items.Clear();
}
@@ -221,10 +229,10 @@ namespace HandyControl.Controls
protected override void Refresh()
{
if (_entryDic.Count > 0)
if (_entryList.Count > 0)
{
_entryDic.Select(item => item.Value.OriginItem).Do(item => _itemsOrigin.Items.Add(item));
_entryDic.Select(item => item.Value.SelectedItem).Do(item => _itemsSelected.Items.Add(item));
_entryList.Select(item => item.OriginItem).Do(item => _itemsOrigin.Items.Add(item));
_entryList.Select(item => item.SelectedItem).Do(item => _itemsSelected.Items.Add(item));
}
}
@@ -58,6 +58,7 @@ namespace HandyControl.Controls
{
_imageViewer.ImageSource = BitmapFrame.Create(uri);
_imageViewer.ImgPath = uri.AbsolutePath;
if (File.Exists(_imageViewer.ImgPath))
{
var info = new FileInfo(_imageViewer.ImgPath);
@@ -0,0 +1,18 @@
using System;
using System.Windows.Input;
namespace HandyControl.Data
{
internal class KeyboardHookEventArgs : EventArgs
{
public bool IsSystemKey { get; }
public Key Key { get; }
public KeyboardHookEventArgs(int virtualKey, bool isSystemKey)
{
IsSystemKey = isSystemKey;
Key = KeyInterop.KeyFromVirtualKey(virtualKey);
}
}
}
@@ -16,9 +16,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\IconElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\IconSwitchElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\InfoElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\LinkElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PanelElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PasswordBoxMonitor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\OpenLinkCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\GlobalShortcut.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\StatusSwitchElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\TipElement.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\TitleElement.cs" />
@@ -115,6 +116,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Window\Window.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\CancelRoutedEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\FunctionEventArgs!1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\KeyboardHookEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Args\MouseHookEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\ColorRange.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\Enum\BadgeStatus.cs" />
@@ -182,7 +184,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Behavior!1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Behavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\BehaviorCollection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\ControlCommands.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\ControlCommands.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\DefaultTriggerAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\EventToCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\EventTrigger.cs" />
@@ -201,9 +203,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\NameResolvedEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\NameResolver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\PreviewInvokeEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\PushMainWindow2TopCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\PushMainWindow2TopCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\RoutedEventTrigger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\ShutdownAppCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\Commands\ShutdownAppCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\TagType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\TransitionEffect.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\TriggerAction!1.cs" />
@@ -248,6 +250,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\SingleOpenHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\ValidateHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Helper\VisualHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Hook\KeyboardHook.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Hook\MouseHook.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BitmapHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\BITMAPINFO.cs" />
@@ -263,7 +266,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\UnsafeNativeMethods.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WNDCLASS.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Interop\WpfSafeHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\MouseDragElementBehaviorEx.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interactivity\MouseDragElementBehaviorEx.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularJudgment.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\RegularPatterns.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\StyleSelector\RadioGroupItemStyleSelector.cs" />
@@ -330,6 +333,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\GridSplitter.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\Badge.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -145,7 +145,7 @@ namespace HandyControl.Interactivity
/// <summary>
/// 打开链接
/// </summary>
public static RoutedCommand OpenLink { get; } = new RoutedCommand(nameof(OpenLink), typeof(ControlCommands));
public static OpenLinkCommand OpenLink { get; } = new OpenLinkCommand();
/// <summary>
/// 关闭程序
@@ -0,0 +1,21 @@
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace HandyControl.Interactivity
{
public class OpenLinkCommand : ICommand
{
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
if (parameter is string link)
{
Process.Start(link);
}
}
public event EventHandler CanExecuteChanged;
}
}
@@ -2,9 +2,9 @@
using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools;
namespace HandyControl.Tools
namespace HandyControl.Interactivity
{
/// <summary>
/// 鼠标拖动行为,该类是反编译微软的System.Windows.Interactivity程序集得到的,并对其做了些扩展
@@ -1,4 +1,5 @@
#if !Core
using System.Windows.Markup;
#if !Core
using System.Reflection;
using System.Runtime.InteropServices;
@@ -12,4 +13,14 @@ using System.Runtime.InteropServices;
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.5.5")]
#endif
#endif
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Controls")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Tools")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Tools.Converter")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Interactivity")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Expression.Shapes")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Expression.Media")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Data")]
[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Properties.Langs")]
[assembly: XmlnsPrefix("https://handyorg.github.io/handycontrol", "hc")]
@@ -5,9 +5,30 @@
<ResourceDictionary Source="../../Basic/Fonts.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" Opacity=".6" SnapsToDevicePixels="true" Stroke="{DynamicResource SecondaryBorderBrush}" StrokeThickness="2" StrokeDashArray="1 1" RadiusX="4" RadiusY="4"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="FocusVisualRadius0">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" Opacity=".6" SnapsToDevicePixels="true" Stroke="{DynamicResource SecondaryBorderBrush}" StrokeThickness="2" StrokeDashArray="1 1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="BaseStyle" TargetType="Control">
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="FontSize" Value="{StaticResource TextFontSize}"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
</Style>
</ResourceDictionary>
@@ -16,9 +16,6 @@
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
</Style>
</ResourceDictionary>
@@ -2,11 +2,26 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls">
<Style x:Key="CheckBoxBaseStyle" TargetType="CheckBox">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BaseStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CheckBoxBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="CheckBox">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-4" Opacity=".6" SnapsToDevicePixels="true" Stroke="{DynamicResource SecondaryBorderBrush}" StrokeThickness="2" StrokeDashArray="1 1" RadiusX="4" RadiusY="4"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
@@ -4,14 +4,26 @@
xmlns:system="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BaseStyle.xaml"/>
<ResourceDictionary Source="../../Basic/Sizes.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="RadioButtonBaseStyle" TargetType="RadioButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Style x:Key="RadioButtonBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="RadioButton">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-4" Opacity=".6" SnapsToDevicePixels="true" Stroke="{DynamicResource SecondaryBorderBrush}" StrokeThickness="2" StrokeDashArray="1 1" RadiusX="4" RadiusY="4"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{DynamicResource DefaultBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
@@ -94,12 +106,11 @@
</Setter>
</Style>
<Style x:Key="RadioButtonIconBaseStyle" TargetType="RadioButton">
<Style x:Key="RadioButtonIconBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="RadioButton">
<Setter Property="Height" Value="{StaticResource DefaultControlHeight}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualRadius0}"/>
<Setter Property="Background" Value="{DynamicResource DefaultBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
@@ -148,11 +159,9 @@
</Style.Triggers>
</Style>
<Style x:Key="RadioGroupItemBaseStyle" TargetType="RadioButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Style x:Key="RadioGroupItemBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="RadioButton">
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
@@ -11,6 +11,8 @@
</ItemsPanelTemplate>
<Style x:Key="RadioGroupBaseStyle" TargetType="controls:RadioGroup">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="ItemsPanel" Value="{StaticResource RadioGroupHorizontalItemsPanelTemplate}"/>
<Setter Property="controls:TitleElement.BorderBrush" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="controls:TitleElement.Background" Value="{DynamicResource RegionBrush}"/>
@@ -2,7 +2,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls">
<Style x:Key="SideMenuItemBaseStyle" TargetType="controls:SideMenuItem">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="BaseStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="SideMenuItemBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="controls:SideMenuItem">
<Setter Property="Role" Value="Item"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
@@ -82,7 +86,7 @@
</Setter>
</Style>
<Style x:Key="SideMenuItemHeaderBaseStyle" TargetType="controls:SideMenuItem">
<Style x:Key="SideMenuItemHeaderBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="controls:SideMenuItem">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource SideMenuItemBaseStyle}"/>
@@ -169,6 +173,8 @@
</Style>
<Style x:Key="SideMenuBaseStyle" TargetType="controls:SideMenu">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Background" Value="{DynamicResource RegionBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource SideMenuItemHeaderBaseStyle}"/>
@@ -51,11 +51,22 @@
</Style.Triggers>
</Style>
<Style x:Key="ToggleButtonSwitchBaseStyle" TargetType="ToggleButton">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Style x:Key="ToggleButtonSwitchBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="ToggleButton">
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" Opacity=".6" SnapsToDevicePixels="true" Stroke="{DynamicResource SecondaryBorderBrush}" StrokeThickness="2" StrokeDashArray="1 1" RadiusX="11" RadiusY="11"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{DynamicResource DefaultBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="6,0,0,0"/>
@@ -162,14 +173,13 @@
</Setter>
</Style>
<Style x:Key="ToggleButtonIconBaseStyle" TargetType="ToggleButton">
<Style x:Key="ToggleButtonIconBaseStyle" BasedOn="{StaticResource BaseStyle}" TargetType="ToggleButton">
<Setter Property="Foreground" Value="{DynamicResource TextIconBrush}"/>
<Setter Property="Width" Value="{StaticResource DefaultControlHeight}"/>
<Setter Property="Height" Value="{StaticResource DefaultControlHeight}"/>
<Setter Property="Padding" Value="8"/>
<Setter Property="controls:IconSwitchElement.Geometry" Value="{x:Null}"/>
<Setter Property="controls:IconSwitchElement.GeometrySelected" Value="{x:Null}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="controls:BorderElement.CornerRadius" Value="4"/>
<Setter Property="BorderBrush" Value="{DynamicResource DarkMaskBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
@@ -51,15 +51,11 @@
</Setter>
</Style>
<Style x:Key="ButtonCustom" TargetType="Button">
<Style x:Key="ButtonCustom" BasedOn="{StaticResource BaseStyle}" TargetType="Button">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
@@ -108,7 +108,7 @@
</Button>
</Grid>
<Border Grid.Row="2" BorderThickness="0,1,0,0" BorderBrush="{DynamicResource BorderBrush}">
<WrapPanel Name="PART_PanelColor" Margin="12,12,0,12" HorizontalAlignment="Center"/>
<WrapPanel Name="PART_PanelColor" Margin="6" HorizontalAlignment="Center"/>
</Border>
<Border Grid.Row="3" BorderThickness="0,1,0,0" BorderBrush="{DynamicResource BorderBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
@@ -0,0 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Style TargetType="GridSplitter">
<Setter Property="Focusable" Value="False"/>
</Style>
</ResourceDictionary>
@@ -51,15 +51,11 @@
</Setter>
</Style>
<Style x:Key="RepeatButtonCustom" TargetType="RepeatButton">
<Style x:Key="RepeatButtonCustom" BasedOn="{StaticResource BaseStyle}" TargetType="RepeatButton">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
@@ -63,6 +63,7 @@
<ResourceDictionary Source="Badge.xaml"/>
<ResourceDictionary Source="Dialog.xaml"/>
<ResourceDictionary Source="SideMenu.xaml"/>
<ResourceDictionary Source="GridSplitter.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
@@ -115,7 +115,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Visibility="{Binding Visibility,ElementName=PART_OverflowButton}" Margin="0,-1,0,0" Grid.Row="0" Grid.Column="0" BorderThickness="0,0,1,1" BorderBrush="{DynamicResource BorderBrush}" Background="{DynamicResource RegionBrush}" HorizontalAlignment="Left" Panel.ZIndex="2">
<Border Visibility="{TemplateBinding ShowScrollButton,Converter={StaticResource Boolean2VisibilityConverter}}" Margin="0,-1,0,0" Grid.Row="0" Grid.Column="0" BorderThickness="0,0,1,1" BorderBrush="{DynamicResource BorderBrush}" Background="{DynamicResource RegionBrush}" HorizontalAlignment="Left" Panel.ZIndex="2">
<Border.CacheMode>
<BitmapCache SnapsToDevicePixels="True"/>
</Border.CacheMode>
@@ -134,12 +134,13 @@
</controls:TabPanel>
</Border>
</controls:ScrollViewer>
<Border Visibility="{Binding Visibility,ElementName=PART_OverflowButton}" Margin="0,-1,0,0" Grid.Row="0" Grid.Column="2" BorderThickness="1,0,0,1" BorderBrush="{DynamicResource BorderBrush}" Background="{DynamicResource RegionBrush}" HorizontalAlignment="Right">
<Border Margin="0,-1,0,0" Grid.Row="0" Grid.Column="2" BorderThickness="1,0,0,1" BorderBrush="{DynamicResource BorderBrush}" Background="{DynamicResource RegionBrush}" HorizontalAlignment="Right">
<Border.CacheMode>
<BitmapCache SnapsToDevicePixels="True"/>
</Border.CacheMode>
<StackPanel Orientation="Horizontal">
<Button Style="{StaticResource ButtonIcon}" BorderBrush="{DynamicResource BorderBrush}" Padding="8" BorderThickness="0,0,1,0" controls:IconElement.Geometry="{StaticResource RightGeometry}" Foreground="{DynamicResource PrimaryTextBrush}" Background="{DynamicResource SecondaryRegionBrush}" x:Name="PART_ScrollButtonRight"/>
<Button Visibility="{TemplateBinding ShowScrollButton,Converter={StaticResource Boolean2VisibilityConverter}}" Style="{StaticResource ButtonIcon}" BorderBrush="{DynamicResource BorderBrush}" Padding="8" controls:IconElement.Geometry="{StaticResource RightGeometry}" Foreground="{DynamicResource PrimaryTextBrush}" Background="{DynamicResource SecondaryRegionBrush}" x:Name="PART_ScrollButtonRight"/>
<Border Visibility="{Binding Visibility,ElementName=PART_OverflowButton}" Width="1" Background="{DynamicResource BorderBrush}"/>
<controls:ContextMenuToggleButton x:Name="PART_OverflowButton" Visibility="Collapsed" Style="{StaticResource ToggleButtonIconTransparent}" controls:IconSwitchElement.Geometry="{StaticResource DownGeometry}" controls:IconSwitchElement.GeometrySelected="{StaticResource UpGeometry}"/>
</StackPanel>
</Border>
@@ -102,17 +102,13 @@
<Style x:Key="ToggleButtonSwitch" BasedOn="{StaticResource ToggleButtonSwitchBaseStyle}" TargetType="ToggleButton"/>
<!--自定义-->
<Style x:Key="ToggleButtonCustom" TargetType="ToggleButton">
<Style x:Key="ToggleButtonCustom" BasedOn="{StaticResource BaseStyle}" TargetType="ToggleButton">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
@@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Tools
{
public class GlobalShortcut
{
private static readonly Dictionary<string, KeyBinding> CommandDic = new Dictionary<string, KeyBinding>();
static GlobalShortcut()
{
KeyboardHook.KeyDown += KeyboardHook_KeyDown;
}
private static void KeyboardHook_KeyDown(object sender, KeyboardHookEventArgs e) => HitTest(e.Key);
private static void HitTest(Key key)
{
if (IsModifierKey(key)) return;
var modifierKeys = Keyboard.Modifiers;
var keyStr = modifierKeys != ModifierKeys.None ? $"{modifierKeys.ToString()}, {key.ToString()}" : key.ToString();
ExecuteCommand(keyStr);
}
private static void ExecuteCommand(string key)
{
if (string.IsNullOrEmpty(key)) return;
if (CommandDic.ContainsKey(key))
{
var keyBinding = CommandDic[key];
var command = keyBinding.Command;
if (command != null && command.CanExecute(keyBinding.CommandParameter))
{
command.Execute(keyBinding.CommandParameter);
}
}
}
private static bool IsModifierKey(Key key)
{
return key == Key.LeftCtrl || key == Key.LeftAlt || key == Key.LeftShift || key == Key.LWin ||
key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin;
}
public static void Init(DependencyObject host)
{
CommandDic.Clear();
if (host == null) return;
var keyBindings = GetKeyBindings(host);
if (keyBindings == null || keyBindings.Count == 0) return;
foreach (KeyBinding item in keyBindings)
{
if (item.Key == Key.None)
{
continue;
}
if (item.Modifiers == ModifierKeys.None)
{
CommandDic[item.Key.ToString()] = item;
}
else
{
var keyStr = $"{item.Modifiers.ToString()}, {item.Key.ToString()}";
CommandDic[keyStr] = item;
}
}
KeyboardHook.Start();
}
public static readonly DependencyProperty KeyBindingsProperty = DependencyProperty.RegisterAttached(
"KeyBindings", typeof(InputBindingCollection), typeof(GlobalShortcut), new PropertyMetadata(new InputBindingCollection()));
public static void SetKeyBindings(DependencyObject element, InputBindingCollection value)
=> element.SetValue(KeyBindingsProperty, value);
public static InputBindingCollection GetKeyBindings(DependencyObject element)
=> (InputBindingCollection) element.GetValue(KeyBindingsProperty);
}
}
@@ -0,0 +1,92 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using HandyControl.Data;
using HandyControl.Tools.Interop;
namespace HandyControl.Tools
{
internal class KeyboardHook
{
public static event EventHandler<KeyboardHookEventArgs> KeyDown;
public static event EventHandler<KeyboardHookEventArgs> KeyUp;
private static IntPtr HookId = IntPtr.Zero;
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
private static int VirtualKey;
private static readonly IntPtr KeyDownIntPtr = (IntPtr)NativeMethods.WM_KEYDOWN;
private static readonly IntPtr KeyUpIntPtr = (IntPtr)NativeMethods.WM_KEYUP;
private static readonly IntPtr SyskeyDownIntPtr = (IntPtr)NativeMethods.WM_SYSKEYDOWN;
private static readonly IntPtr SyskeyUpIntPtr = (IntPtr)NativeMethods.WM_SYSKEYUP;
public static void Start()
{
if (HookId != IntPtr.Zero)
{
Stop();
}
HookId = SetHook(Proc);
}
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
private static IntPtr SetHook(UnsafeNativeMethods.HookProc proc)
{
using (var curProcess = Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
if (curModule != null)
{
return UnsafeNativeMethods.SetWindowsHookEx((int)UnsafeNativeMethods.HookType.WH_KEYBOARD_LL, proc,
UnsafeNativeMethods.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 UnsafeNativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam);
}
}
}
@@ -14,7 +14,14 @@ namespace HandyControl.Tools
private static readonly UnsafeNativeMethods.HookProc Proc = HookCallback;
public static void Start() => HookId = SetHook(Proc);
public static void Start()
{
if (HookId != IntPtr.Zero)
{
Stop();
}
HookId = SetHook(Proc);
}
public static void Stop() => UnsafeNativeMethods.UnhookWindowsHookEx(HookId);
@@ -31,6 +31,10 @@ namespace HandyControl.Tools.Interop
NIIF_INFO = 0x00000001,
NIIF_WARNING = 0x00000002,
NIIF_ERROR = 0x00000003,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105,
WM_MOUSEMOVE = 0x0200,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
@@ -186,7 +186,8 @@ namespace HandyControl.Tools.Interop
public enum HookType
{
WH_MOUSE_LL = 14,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
[StructLayout(LayoutKind.Sequential)]