RightCardControl.xaml.cs 14 KB

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