Skip to content

Commit b13a8c2

Browse files
committed
add in drag file.
1)update in issues. 2)add in drag file. Co-Authored-By: 闫驚鏵(Jinhua Yan) <yanjinhuamail@gmail.com>
1 parent ece0774 commit b13a8c2

File tree

5 files changed

+120
-23
lines changed

5 files changed

+120
-23
lines changed

SoftwareHelper/App.xaml.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ protected override void OnStartup(StartupEventArgs e)
4646
var autoUpdater = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AutoUpdater.exe");
4747
if (File.Exists(autoUpdater))
4848
{
49-
Process.Start(autoUpdater, System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
49+
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
50+
Log.Debug($"版本号:{version}");
51+
Process.Start(autoUpdater, version);
5052
}
5153
else
5254
Log.Warn("警告:未找到AutoUpdater.exe,无法及时更新到最新版。");

SoftwareHelper/Helpers/Common.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Win32;
1+
using Microsoft.Expression.Drawing.Core;
2+
using Microsoft.Win32;
23
using Newtonsoft.Json;
34
using SoftwareHelper.Models;
45
using System;
@@ -31,6 +32,10 @@ public partial class Common
3132
/// </summary>
3233
public static string TemporaryApplicationJson { get; set; }
3334
/// <summary>
35+
/// 自己推拽的应用程序json
36+
/// </summary>
37+
public static string LocalTemporaryApplicationJson { get; set; }
38+
/// <summary>
3439
/// 默认图标
3540
/// </summary>
3641
public static string ApplicationIcon { get; set; }
@@ -396,6 +401,7 @@ public static void TemporaryFiles()
396401
System.IO.Directory.CreateDirectory(TemporaryIconFile);
397402
}
398403
TemporaryApplicationJson = Path.Combine(TemporaryFile, "application.json");
404+
LocalTemporaryApplicationJson = Path.Combine(TemporaryFile, "localapplication.json");
399405
ApplicationIcon = Path.Combine(TemporaryIconFile, "ApplicationIcon.png");
400406
if (!File.Exists(ApplicationIcon))
401407
{
@@ -548,11 +554,16 @@ public static void Init()
548554
var json1 = JsonHelper.Serialize(Common.ApplicationListCache);
549555
FileHelper.WriteFile(ConvertJsonString(json1), Common.TemporaryApplicationJson);
550556
}
557+
var jsonLocal = FileHelper.ReadFile(LocalTemporaryApplicationJson);
558+
var applicationsLocal = JsonHelper.Deserialize<ObservableCollection<ApplicationModel>>(jsonLocal);
559+
Common.ApplicationListCache.AddRange(applicationsLocal);
560+
Common.ApplicationListCache = new ObservableCollection<ApplicationModel>(Common.ApplicationListCache.OrderBy(x => x.Group));
561+
551562
}
552563

553564

554565
#region 格式化json字符串
555-
static string ConvertJsonString(string str)
566+
public static string ConvertJsonString(string str)
556567
{
557568
//格式化json字符串
558569
JsonSerializer serializer = new JsonSerializer();
@@ -579,7 +590,7 @@ static string ConvertJsonString(string str)
579590
#endregion
580591

581592
#region 判断首字母是否为中文,如果为中文 首字母转为英语
582-
static bool IsChinese(string text)
593+
public static bool IsChinese(string text)
583594
{
584595
bool chinese = false;
585596
Regex regChina = new Regex("[^\x00-\xFF]");
@@ -600,7 +611,7 @@ static bool IsChinese(string text)
600611
/// <param name="CnChar">单个汉字</param>
601612
/// <returns>单个大写字母</returns>
602613

603-
static string GetCharSpellCode(string CnChar)
614+
public static string GetCharSpellCode(string CnChar)
604615
{
605616
long iCnChar;
606617
byte[] ZW = System.Text.Encoding.Default.GetBytes(CnChar);

SoftwareHelper/Models/ApplicationModel.cs

+4
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ public class ApplicationModel
2424
/// 是否移除
2525
/// </summary>
2626
public bool IsRemove { get; set; }
27+
/// <summary>
28+
/// 是否是拖拽进来的
29+
/// </summary>
30+
public bool IsDrag { get; set; }
2731
}
2832
}

SoftwareHelper/Views/EmbedDeasktopView.xaml

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
SnapsToDevicePixels="True" WindowStyle="None" Background="Transparent"
1919
Title="SoftwareHelper" Height="600" Width="90"
2020
ShowInTaskbar="False" x:Name="embedDeasktopView"
21-
AllowDrop="True" PreviewDragEnter="embedDeasktopView_DragEnter" PreviewDragLeave="embedDeasktopView_DragLeave">
21+
AllowDrop="True"
22+
PreviewDragEnter="embedDeasktopView_DragEnter"
23+
PreviewDragLeave="embedDeasktopView_DragLeave">
2224
<wpfdev:WindowChrome.WindowChrome>
2325
<wpfdev:WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/>
2426
</wpfdev:WindowChrome.WindowChrome>
@@ -356,12 +358,15 @@
356358
HorizontalAlignment="Center" VerticalAlignment="Center"
357359
FontSize="22" Foreground="{DynamicResource NormalWhiteBrush}" />
358360
</Border>
359-
<Canvas Background="Transparent" x:Name="DragCanvas"
360-
AllowDrop="True" DragOver="Grid_DragOver"
361+
<Canvas Background="Transparent"
362+
x:Name="DragCanvas"
363+
AllowDrop="True"
364+
DragOver="DragCanvas_DragOver"
361365
Drop="DragCanvas_Drop"
362366
Visibility="Collapsed">
363367
<StackPanel Orientation="Vertical" Name="DragStackPanel"
364-
RenderTransformOrigin=".5,.5">
368+
RenderTransformOrigin=".5,.5"
369+
Opacity=".5">
365370
<StackPanel.RenderTransform>
366371
<ScaleTransform x:Name="DragScaleTransform" ScaleX="1" ScaleY="1"/>
367372
</StackPanel.RenderTransform>

SoftwareHelper/Views/EmbedDeasktopView.xaml.cs

+89-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
using System.Windows.Media;
1212
using System.Windows.Media.Animation;
1313
using System.Windows.Media.Imaging;
14+
using Newtonsoft.Json.Linq;
15+
using System.Xml.Linq;
1416
using SoftwareHelper.Helpers;
17+
using SoftwareHelper.Models;
1518
using SoftwareHelper.ViewModels;
1619
using WPFDevelopers.Controls;
20+
using static System.Windows.Forms.LinkLabel;
21+
using System.IO;
1722

1823
namespace SoftwareHelper.Views
1924
{
@@ -28,6 +33,8 @@ public partial class EmbedDeasktopView : Window
2833
private Point anchorPoint;
2934
private bool inDrag;
3035
private readonly MainVM mainVM;
36+
private string filePath;
37+
private BitmapSource fileIcon;
3138

3239
public EmbedDeasktopView()
3340
{
@@ -133,6 +140,8 @@ private void EmbedDeasktopView_Loaded(object sender, RoutedEventArgs e)
133140
private void EmbedDeasktopView_Closing(object sender, CancelEventArgs e)
134141
{
135142
Win32Api.UnRegisterDesktop(true);
143+
string json = JsonHelper.Serialize(mainVM.ApplicationList.Where(x=>x.IsDrag == true).OrderBy(x => x.Group));
144+
FileHelper.WriteFile(Common.ConvertJsonString(json), Common.LocalTemporaryApplicationJson);
136145
}
137146

138147
protected override void OnSourceInitialized(EventArgs e)
@@ -294,16 +303,28 @@ private void UnToggleButtonMini_Checked(object sender, RoutedEventArgs e)
294303
#endregion
295304

296305

297-
private void Grid_DragOver(object sender, DragEventArgs e)
306+
private void DragCanvas_DragOver(object sender, DragEventArgs e)
298307
{
299-
if (e.Data.GetDataPresent(DataFormats.FileDrop))
308+
try
300309
{
301-
var msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
302-
DragTextBlock.Text = System.IO.Path.GetFileName(msg);
303-
DragImage.Source = (BitmapSource)Common.GetIcon(msg);
304-
var point = e.GetPosition(this);
305-
Canvas.SetLeft(DragStackPanel, point.X - DragStackPanel.ActualWidth / 2);
306-
Canvas.SetTop(DragStackPanel, point.Y - DragStackPanel.ActualHeight / 2);
310+
if (e.Data.GetDataPresent(DataFormats.FileDrop))
311+
{
312+
var msg = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
313+
filePath = msg;
314+
DragTextBlock.Text = System.IO.Path.GetFileName(filePath);
315+
var icon = (BitmapSource)Common.GetIcon(filePath);
316+
fileIcon = icon;
317+
DragImage.Source = fileIcon;
318+
var point = e.GetPosition(this);
319+
var x = point.X - DragStackPanel.ActualWidth / 2;
320+
var y = point.Y - DragStackPanel.ActualHeight / 2;
321+
Canvas.SetLeft(DragStackPanel, x);
322+
Canvas.SetTop(DragStackPanel, y);
323+
}
324+
}
325+
catch (Exception ex)
326+
{
327+
Log.Error("DragCanvas_DragOver:" + ex.Message);
307328
}
308329

309330
}
@@ -312,14 +333,26 @@ private void embedDeasktopView_DragEnter(object sender, DragEventArgs e)
312333
{
313334
AppSwitchListEmbedded.IsHitTestVisible = false;
314335
AppSwitchList.IsHitTestVisible = false;
315-
DragScaleTransform.ScaleX = 1;
316-
DragScaleTransform.ScaleY = 1;
336+
var doubleXAnimation = new DoubleAnimation
337+
{
338+
From = 0,
339+
To = 1,
340+
Duration = new Duration(TimeSpan.FromSeconds(0)),
341+
};
342+
DragScaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty,doubleXAnimation);
343+
var doubleYAnimation = new DoubleAnimation
344+
{
345+
From = 0,
346+
To = 1,
347+
Duration = new Duration(TimeSpan.FromSeconds(0)),
348+
};
349+
DragScaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, doubleXAnimation);
317350
DragCanvas.Visibility = Visibility.Visible;
318351
}
319352

320353
private void embedDeasktopView_DragLeave(object sender, DragEventArgs e)
321354
{
322-
//DragInitial();
355+
DragInitial();
323356
}
324357
void DisposeDrag()
325358
{
@@ -329,6 +362,7 @@ void DisposeDrag()
329362
From = 1,
330363
To = 0,
331364
Duration = new Duration(TimeSpan.FromSeconds(0.5)),
365+
EasingFunction = new BackEase { EasingMode = EasingMode.EaseIn },
332366
};
333367
Storyboard.SetTargetName(doubleXAnimation, "DragStackPanel");
334368
Storyboard.SetTargetProperty(doubleXAnimation, new PropertyPath("(StackPanel.RenderTransform).(ScaleTransform.ScaleX)"));
@@ -337,6 +371,7 @@ void DisposeDrag()
337371
From = 1,
338372
To = 0,
339373
Duration = new Duration(TimeSpan.FromSeconds(0.5)),
374+
EasingFunction = new BackEase { EasingMode = EasingMode.EaseIn },
340375
};
341376
Storyboard.SetTargetName(doubleYAnimation, "DragStackPanel");
342377
Storyboard.SetTargetProperty(doubleYAnimation, new PropertyPath("(StackPanel.RenderTransform).(ScaleTransform.ScaleY)"));
@@ -345,18 +380,58 @@ void DisposeDrag()
345380
storyboard.Completed += delegate
346381
{
347382
DragInitial();
383+
var model = new ApplicationModel();
384+
model.ExePath = filePath;
385+
model.Name = DragTextBlock.Text;
386+
var iconPath = System.IO.Path.Combine(Common.TemporaryIconFile, model.Name);
387+
iconPath = iconPath + ".png";
388+
model.IconPath = iconPath;
389+
model.IsDrag = true;
390+
var firstModel = mainVM.ApplicationList.FirstOrDefault(x => x.Name == model.Name && x.ExePath == model.ExePath);
391+
if (firstModel != null) return;
392+
string first = model.Name.Substring(0, 1);
393+
if (!Common.IsChinese(first))
394+
{
395+
if (char.IsUpper(first.ToCharArray()[0]))
396+
model.Group = first;
397+
model.Group = model.Name.Substring(0, 1).ToUpper();
398+
}
399+
else
400+
{
401+
model.Group = Common.GetCharSpellCode(first);
402+
}
403+
mainVM.ApplicationList.Insert(0, model);
404+
if (File.Exists(iconPath))
405+
return;
406+
Common.SaveImage(fileIcon, iconPath);
348407
};
349408
storyboard.Begin(DragStackPanel);
350409
}
351410
void DragInitial()
352411
{
353-
DragCanvas.Visibility = Visibility.Collapsed;
354-
AppSwitchListEmbedded.IsHitTestVisible = true;
355-
AppSwitchList.IsHitTestVisible = true;
412+
try
413+
{
414+
DragCanvas.Visibility = Visibility.Collapsed;
415+
AppSwitchListEmbedded.IsHitTestVisible = true;
416+
AppSwitchList.IsHitTestVisible = true;
417+
}
418+
catch (Exception ex)
419+
{
420+
421+
Log.Error("DragInitial:" + ex.Message);
422+
}
356423
}
357424
private void DragCanvas_Drop(object sender, DragEventArgs e)
358425
{
426+
if (string.IsNullOrWhiteSpace(filePath))
427+
{
428+
DragInitial();
429+
return;
430+
}
359431
DisposeDrag();
360432
}
433+
434+
435+
361436
}
362437
}

0 commit comments

Comments
 (0)