RightCardControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control.Other;
  4. using GeekDesk.Util;
  5. using GeekDesk.ViewModel;
  6. using HandyControl.Controls;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. using System.Windows.Media.Animation;
  17. using System.Windows.Media.Imaging;
  18. namespace GeekDesk.Control.UserControls.PannelCard
  19. {
  20. /// <summary>
  21. /// RightCardControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class RightCardControl : UserControl
  24. {
  25. private AppData appData = MainWindow.appData;
  26. public RightCardControl()
  27. {
  28. InitializeComponent();
  29. }
  30. #region 图标拖动
  31. DelegateCommand<int[]> _swap;
  32. public DelegateCommand<int[]> SwapCommand
  33. {
  34. get
  35. {
  36. if (_swap == null)
  37. _swap = new DelegateCommand<int[]>(
  38. (indexes) =>
  39. {
  40. int fromS = indexes[0];
  41. int to = indexes[1];
  42. ObservableCollection<IconInfo> iconList = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  43. var elementSource = iconList[to];
  44. var dragged = iconList[fromS];
  45. iconList.Remove(dragged);
  46. iconList.Insert(to, dragged);
  47. }
  48. );
  49. return _swap;
  50. }
  51. }
  52. #endregion 图标拖动
  53. /// <summary>
  54. /// 图标点击事件
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. private void IconClick(object sender, MouseButtonEventArgs e)
  59. {
  60. IconInfo icon = (IconInfo)((SimpleStackPanel)sender).Tag;
  61. if (icon.AdminStartUp)
  62. {
  63. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  64. }
  65. else
  66. {
  67. StartIconApp(icon, IconStartType.DEFAULT_STARTUP);
  68. }
  69. }
  70. /// <summary>
  71. /// 管理员方式启动
  72. /// </summary>
  73. /// <param name="sender"></param>
  74. /// <param name="e"></param>
  75. private void IconAdminStart(object sender, RoutedEventArgs e)
  76. {
  77. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  78. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  79. }
  80. /// <summary>
  81. /// 打开文件所在位置
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void ShowInExplore(object sender, RoutedEventArgs e)
  86. {
  87. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  88. StartIconApp(icon, IconStartType.SHOW_IN_EXPLORE);
  89. }
  90. private void StartIconApp(IconInfo icon, IconStartType type)
  91. {
  92. try
  93. {
  94. Process p = new Process();
  95. p.StartInfo.FileName = icon.Path;
  96. if (icon.IconType == IconType.OTHER)
  97. {
  98. if (!File.Exists(icon.Path) && !Directory.Exists(icon.Path))
  99. {
  100. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
  101. return;
  102. }
  103. p.StartInfo.WorkingDirectory = icon.Path.Substring(0, icon.Path.LastIndexOf("\\"));
  104. switch (type)
  105. {
  106. case IconStartType.ADMIN_STARTUP:
  107. p.StartInfo.Arguments = "1";//启动参数
  108. p.StartInfo.Verb = "runas";
  109. p.StartInfo.CreateNoWindow = false; //设置显示窗口
  110. p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动进程
  111. p.StartInfo.ErrorDialog = false;
  112. if (appData.AppConfig.AppHideType == AppHideType.START_EXE)
  113. {
  114. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  115. if (appData.AppConfig.MarginHide)
  116. {
  117. if (!MainWindow.hide.IsMargin())
  118. {
  119. MainWindow.HideApp();
  120. }
  121. }
  122. else
  123. {
  124. MainWindow.HideApp();
  125. }
  126. }
  127. break;// c#好像不能case穿透
  128. case IconStartType.DEFAULT_STARTUP:
  129. if (appData.AppConfig.AppHideType == AppHideType.START_EXE)
  130. {
  131. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  132. if (appData.AppConfig.MarginHide)
  133. {
  134. if (!MainWindow.hide.IsMargin())
  135. {
  136. MainWindow.HideApp();
  137. }
  138. } else
  139. {
  140. MainWindow.HideApp();
  141. }
  142. }
  143. break;
  144. case IconStartType.SHOW_IN_EXPLORE:
  145. p.StartInfo.FileName = "Explorer.exe";
  146. p.StartInfo.Arguments = "/e,/select," + icon.Path;
  147. break;
  148. }
  149. }
  150. p.Start();
  151. icon.Count++;
  152. }
  153. catch (Exception)
  154. {
  155. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
  156. }
  157. }
  158. private void Wrap_Drop(object sender, DragEventArgs e)
  159. {
  160. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  161. if (dropObject == null) return;
  162. foreach (object obj in dropObject)
  163. {
  164. string path = (string)obj;
  165. //string base64 = ImageUtil.FileImageToBase64(path, ImageFormat.Png);
  166. string ext = "";
  167. if (!ImageUtil.IsSystemItem(path))
  168. {
  169. ext = System.IO.Path.GetExtension(path).ToLower();
  170. }
  171. if (".lnk".Equals(ext))
  172. {
  173. string targetPath = FileUtil.GetTargetPathByLnk(path);
  174. if (targetPath!=null)
  175. {
  176. path = targetPath;
  177. }
  178. }
  179. BitmapImage bi = ImageUtil.GetBitmapIconByPath(path);
  180. IconInfo iconInfo = new IconInfo
  181. {
  182. Path = path,
  183. BitmapImage = bi
  184. };
  185. iconInfo.DefaultImage = iconInfo.ImageByteArr;
  186. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(path);
  187. if (StringUtil.IsEmpty(iconInfo.Name))
  188. {
  189. iconInfo.Name = path;
  190. }
  191. MainWindow.appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Add(iconInfo);
  192. CommonCode.SaveAppData(MainWindow.appData);
  193. }
  194. }
  195. /// <summary>
  196. /// 从列表删除图标
  197. /// </summary>
  198. /// <param name="sender"></param>
  199. /// <param name="e"></param>
  200. private void RemoveIcon(object sender, RoutedEventArgs e)
  201. {
  202. appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
  203. }
  204. private void SystemContextMenu(object sender, RoutedEventArgs e)
  205. {
  206. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  207. DirectoryInfo[] folders = new DirectoryInfo[1];
  208. folders[0] = new DirectoryInfo(icon.Path);
  209. ShellContextMenu scm = new ShellContextMenu();
  210. System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
  211. p.X -= 80;
  212. p.Y -= 80;
  213. scm.ShowContextMenu(folders, p);
  214. }
  215. /// <summary>
  216. /// 弹出Icon属性修改面板
  217. /// </summary>
  218. /// <param name="sender"></param>
  219. /// <param name="e"></param>
  220. private void PropertyConfig(object sender, RoutedEventArgs e)
  221. {
  222. IconInfo info = (IconInfo)((MenuItem)sender).Tag;
  223. switch (info.IconType)
  224. {
  225. case IconType.URL:
  226. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog(info);
  227. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  228. break;
  229. default:
  230. IconInfoDialog dialog = new IconInfoDialog(info);
  231. dialog.dialog = HandyControl.Controls.Dialog.Show(dialog, "IconInfoDialog");
  232. break;
  233. }
  234. }
  235. private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
  236. {
  237. double width = appData.AppConfig.ImageWidth;
  238. double height = appData.AppConfig.ImageHeight;
  239. width += width * 0.15;
  240. height += height * 0.15;
  241. ImgStoryBoard(sender, (int)width, (int)height, 1, true);
  242. }
  243. private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
  244. {
  245. ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 220);
  246. }
  247. private void ImgStoryBoard(object sender, int height, int width, int milliseconds, bool checkRmStoryboard = false)
  248. {
  249. if (appData.AppConfig.PMModel) return;
  250. Panel sp = sender as Panel;
  251. DependencyObject dos = sp.Parent;
  252. Image img = sp.Children[0] as Image;
  253. double afterHeight = img.Height;
  254. double afterWidth = img.Width;
  255. //动画定义
  256. Storyboard myStoryboard = new Storyboard();
  257. DoubleAnimation heightAnimation = new DoubleAnimation
  258. {
  259. From = afterHeight,
  260. To = height,
  261. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  262. };
  263. DoubleAnimation widthAnimation = new DoubleAnimation
  264. {
  265. From = afterWidth,
  266. To = width,
  267. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  268. };
  269. Timeline.SetDesiredFrameRate(heightAnimation, 60);
  270. Timeline.SetDesiredFrameRate(widthAnimation, 60);
  271. Storyboard.SetTarget(widthAnimation, img);
  272. Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));
  273. Storyboard.SetTarget(heightAnimation, img);
  274. Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("Height"));
  275. myStoryboard.Children.Add(heightAnimation);
  276. myStoryboard.Children.Add(widthAnimation);
  277. CheckRemoveStoryboard crs = new CheckRemoveStoryboard
  278. {
  279. sb = myStoryboard,
  280. sp = sp,
  281. heightAnimation = heightAnimation,
  282. widthAnimation = widthAnimation,
  283. img = img,
  284. isMouseOver = !checkRmStoryboard
  285. };
  286. heightAnimation.Completed += (s, e) =>
  287. {
  288. if (checkRmStoryboard)
  289. {
  290. ThreadStart ts = new ThreadStart(crs.Remove);
  291. System.Threading.Thread t = new System.Threading.Thread(ts);
  292. t.Start();
  293. } else
  294. {
  295. img.BeginAnimation(WidthProperty, null);
  296. img.BeginAnimation(HeightProperty, null);
  297. }
  298. };
  299. img.BeginAnimation(WidthProperty, widthAnimation);
  300. img.BeginAnimation(HeightProperty, heightAnimation);
  301. //myStoryboard.Completed += (s, e) =>
  302. //{
  303. // if (checkRmStoryboard || true)
  304. // {
  305. // ThreadStart ts = new ThreadStart(crs.Remove);
  306. // System.Threading.Thread t = new System.Threading.Thread(ts);
  307. // t.Start();
  308. // }
  309. // else
  310. // {
  311. // myStoryboard.Remove();
  312. // }
  313. //};
  314. //myStoryboard.Begin();
  315. }
  316. private class CheckRemoveStoryboard
  317. {
  318. public Storyboard sb;
  319. public Panel sp;
  320. public Image img;
  321. public DoubleAnimation heightAnimation;
  322. public DoubleAnimation widthAnimation;
  323. public bool isMouseOver;
  324. public void Remove()
  325. {
  326. while (true)
  327. {
  328. if (sp.IsMouseOver == isMouseOver)
  329. {
  330. App.Current.Dispatcher.Invoke((Action)(() =>
  331. {
  332. img.BeginAnimation(WidthProperty, null);
  333. img.BeginAnimation(HeightProperty, null);
  334. //heightAnimation.FillBehavior = FillBehavior.Stop;
  335. //widthAnimation.FillBehavior = FillBehavior.Stop;
  336. }));
  337. return;
  338. }
  339. else
  340. {
  341. System.Threading.Thread.Sleep(500);
  342. }
  343. }
  344. }
  345. }
  346. public void RemoveSB(Object sb)
  347. {
  348. Storyboard sb2 = sb as Storyboard;
  349. System.Threading.Thread.Sleep(500);
  350. sb2.Remove();
  351. }
  352. private void AddUrlIcon(object sender, RoutedEventArgs e)
  353. {
  354. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog();
  355. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  356. }
  357. }
  358. }