?WPF 開發的 Windows ?軟件快捷助手
Windows ?軟件快捷助手
作者:WPFDevelopersOrg - 驚鏵
原文鏈接:https://github.com/WPFDevelopersOrg/SoftwareHelper
框架使用
.NET40
;Visual Studio 2019
;項目使用 MIT 開源許可協議;
項目使用
MVVM
模式來實現詳細學習和理解WPF
;新增功能:
通過托盤設置是否開機啟動。
增加外部文件可拖放入到應用。
項目中技術使用到
WindowsAPI
、Style
、CustomControl
、Json 序列化和反序列化
、換膚 Dark|Light
、動畫
、Hook 按鍵與鼠標
、顏色拾取
、截圖工具
、DPI縮放
、開機啟動
、NLog
、轉換器
、禁止程序多開并喚醒之前程序
等;歡迎下載項目進行魔改;
更多效果可以通過GitHub[1]|碼云[2]下載源碼;
需注意程序不能以管理員身份運行,因為管理員身份運行
Drop
操作無效。
1)要允許控件Drop
操作,首先必須設置控件屬性AllowDrop="True"
,這樣控件才能產生DragOver/DragEnter/Drop
等相關事件代碼如下:
<Canvas?Background="Transparent"?x:Name="DragCanvas"AllowDrop="True"?DragOver="DragCanvas_DragOver"Drop="DragCanvas_Drop"Visibility="Collapsed"><StackPanel?Orientation="Vertical"?Name="DragStackPanel"RenderTransformOrigin=".5,.5"Opacity=".5"><StackPanel.RenderTransform><ScaleTransform?x:Name="DragScaleTransform"?ScaleX="1"?ScaleY="1"/></StackPanel.RenderTransform><Image?x:Name="DragImage"?Stretch="Uniform"/><TextBlock?Name="DragTextBlock"MaxWidth="70"?TextWrapping="Wrap"VerticalAlignment="Center"Block.TextAlignment="Center"?HorizontalAlignment="Center"TextTrimming="CharacterEllipsis"MaxHeight="40"Foreground="{DynamicResource?NormalWhiteBrush}"/></StackPanel></Canvas>
2) 后臺實現拖拽代碼如下:
private?void?DragCanvas_DragOver(object?sender,?DragEventArgs?e){try{if?(e.Data.GetDataPresent(DataFormats.FileDrop)){var?msg?=?((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();filePath?=?msg;DragTextBlock.Text?=?System.IO.Path.GetFileName(filePath);var?icon?=?(BitmapSource)Common.GetIcon(filePath);fileIcon?=?icon;DragImage.Source?=?fileIcon;var?point?=?e.GetPosition(this);var?x?=?point.X?-?DragStackPanel.ActualWidth?/?2;var?y?=?point.Y?-?DragStackPanel.ActualHeight?/?2;Canvas.SetLeft(DragStackPanel,?x);Canvas.SetTop(DragStackPanel,?y);}}catch?(Exception?ex){Log.Error("DragCanvas_DragOver:"?+?ex.Message);}}private?void?embedDeasktopView_DragEnter(object?sender,?DragEventArgs?e){AppSwitchListEmbedded.IsHitTestVisible?=?false;AppSwitchList.IsHitTestVisible?=?false;var?doubleXAnimation?=?new?DoubleAnimation{From?=?0,To?=?1,Duration?=?new?Duration(TimeSpan.FromSeconds(0)),};DragScaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty,doubleXAnimation);var?doubleYAnimation?=?new?DoubleAnimation{From?=?0,To?=?1,Duration?=?new?Duration(TimeSpan.FromSeconds(0)),};DragScaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty,?doubleXAnimation);DragCanvas.Visibility?=?Visibility.Visible;}private?void?embedDeasktopView_DragLeave(object?sender,?DragEventArgs?e){DragInitial();}void?DisposeDrag(){var?storyboard?=?new?Storyboard();var?doubleXAnimation?=?new?DoubleAnimation{From?=?1,To?=?0,Duration?=?new?Duration(TimeSpan.FromSeconds(0.5)),EasingFunction?=?new?BackEase?{?EasingMode?=?EasingMode.EaseIn?},};Storyboard.SetTargetName(doubleXAnimation,?"DragStackPanel");Storyboard.SetTargetProperty(doubleXAnimation,?new?PropertyPath("(StackPanel.RenderTransform).(ScaleTransform.ScaleX)"));var?doubleYAnimation?=?new?DoubleAnimation{From?=?1,To?=?0,Duration?=?new?Duration(TimeSpan.FromSeconds(0.5)),EasingFunction?=?new?BackEase?{?EasingMode?=?EasingMode.EaseIn?},};Storyboard.SetTargetName(doubleYAnimation,?"DragStackPanel");Storyboard.SetTargetProperty(doubleYAnimation,?new?PropertyPath("(StackPanel.RenderTransform).(ScaleTransform.ScaleY)"));storyboard.Children.Add(doubleXAnimation);storyboard.Children.Add(doubleYAnimation);storyboard.Completed?+=?delegate?{DragInitial();var?model?=?new?ApplicationModel();model.ExePath?=?filePath;model.Name?=?DragTextBlock.Text;var?iconPath?=?System.IO.Path.Combine(Common.TemporaryIconFile,?model.Name);iconPath?=?iconPath?+?".png";model.IconPath?=?iconPath;model.IsDrag?=?true;var?firstModel?=?mainVM.ApplicationList.FirstOrDefault(x?=>?x.Name?==?model.Name?&&?x.ExePath?==?model.ExePath);if?(firstModel?!=?null)?return;string?first?=?model.Name.Substring(0,?1);if?(!Common.IsChinese(first)){if?(char.IsUpper(first.ToCharArray()[0]))model.Group?=?first;model.Group?=?model.Name.Substring(0,?1).ToUpper();}else{model.Group?=?Common.GetCharSpellCode(first);}mainVM.ApplicationList.Insert(0,?model);if?(File.Exists(iconPath))return;Common.SaveImage(fileIcon,?iconPath);};storyboard.Begin(DragStackPanel);}void?DragInitial(){try{DragCanvas.Visibility?=?Visibility.Collapsed;AppSwitchListEmbedded.IsHitTestVisible?=?true;AppSwitchList.IsHitTestVisible?=?true;}catch?(Exception?ex){Log.Error("DragInitial:"?+?ex.Message);}}private?void?DragCanvas_Drop(object?sender,?DragEventArgs?e){if?(string.IsNullOrWhiteSpace(filePath)){DragInitial();return;?}DisposeDrag();}
體驗地址1[3]
體驗地址2[4]
參考資料
[1]
GitHub: https://github.com/WPFDevelopersOrg/SoftwareHelper
[2]碼云: https://gitee.com/WPFDevelopersOrg/SoftwareHelper
[3]體驗地址1: https://github.com/WPFDevelopersOrg/SoftwareHelper/releases/download/1.0.0.1/SoftwareHelper_Setup1.0.0.1.exe
[4]體驗地址2: https://gitee.com/WPFDevelopersOrg/SoftwareHelper/releases/download/1.0.0.1/SoftwareHelper_Setup1.0.0.1.exe