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]
参考资料
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
原文始发于微信公众号(WPF开发者):一款 Windows 软件快捷助手
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/54934.html