RightCardControl.xaml.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control.Other;
  4. using GeekDesk.Control.Windows;
  5. using GeekDesk.Util;
  6. using GeekDesk.ViewModel;
  7. using HandyControl.Controls;
  8. using System;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.IO;
  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.Threading;
  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. private volatile static bool DROP_ICON = false;
  27. private Thread dropCheckThread = null;
  28. public RightCardControl()
  29. {
  30. InitializeComponent();
  31. }
  32. #region 图标拖动
  33. DelegateCommand<int[]> _swap;
  34. public DelegateCommand<int[]> SwapCommand
  35. {
  36. get
  37. {
  38. if (_swap == null)
  39. _swap = new DelegateCommand<int[]>(
  40. (indexes) =>
  41. {
  42. DROP_ICON = true;
  43. if (appData.AppConfig.IconSortType != SortType.CUSTOM
  44. && (dropCheckThread == null || !dropCheckThread.IsAlive))
  45. {
  46. dropCheckThread = new Thread(() =>
  47. {
  48. do
  49. {
  50. DROP_ICON = false;
  51. Thread.Sleep(1000);
  52. } while (DROP_ICON);
  53. MainWindow.appData.AppConfig.IconSortType = SortType.CUSTOM;
  54. App.Current.Dispatcher.Invoke(() =>
  55. {
  56. if (MainWindow.mainWindow.Visibility == Visibility.Collapsed
  57. || MainWindow.mainWindow.Opacity != 1)
  58. {
  59. Growl.WarningGlobal("已将图标排序规则重置为自定义!");
  60. }
  61. else
  62. {
  63. Growl.Warning("已将图标排序规则重置为自定义!", "MainWindowGrowl");
  64. }
  65. });
  66. });
  67. dropCheckThread.Start();
  68. }
  69. int fromS = indexes[0];
  70. int to = indexes[1];
  71. ObservableCollection<IconInfo> iconList = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  72. var elementSource = iconList[to];
  73. var dragged = iconList[fromS];
  74. iconList.Remove(dragged);
  75. iconList.Insert(to, dragged);
  76. }
  77. );
  78. return _swap;
  79. }
  80. }
  81. #endregion 图标拖动
  82. private void Icon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  83. {
  84. if (appData.AppConfig.DoubleOpen)
  85. {
  86. IconClick(sender, e);
  87. }
  88. }
  89. private void Icon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  90. {
  91. if (!appData.AppConfig.DoubleOpen)
  92. {
  93. IconClick(sender, e);
  94. }
  95. }
  96. /// <summary>
  97. /// 图标点击事件
  98. /// </summary>
  99. /// <param name="sender"></param>
  100. /// <param name="e"></param>
  101. private void IconClick(object sender, MouseButtonEventArgs e)
  102. {
  103. if (appData.AppConfig.DoubleOpen && e.ClickCount >= 2)
  104. {
  105. IconInfo icon = (IconInfo)((Panel)sender).Tag;
  106. if (icon.AdminStartUp)
  107. {
  108. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  109. }
  110. else
  111. {
  112. StartIconApp(icon, IconStartType.DEFAULT_STARTUP);
  113. }
  114. }
  115. else if (!appData.AppConfig.DoubleOpen && e.ClickCount == 1)
  116. {
  117. IconInfo icon = (IconInfo)((Panel)sender).Tag;
  118. if (icon.AdminStartUp)
  119. {
  120. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  121. }
  122. else
  123. {
  124. StartIconApp(icon, IconStartType.DEFAULT_STARTUP);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 管理员方式启动
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void IconAdminStart(object sender, RoutedEventArgs e)
  134. {
  135. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  136. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  137. }
  138. /// <summary>
  139. /// 打开文件所在位置
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void ShowInExplore(object sender, RoutedEventArgs e)
  144. {
  145. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  146. StartIconApp(icon, IconStartType.SHOW_IN_EXPLORE);
  147. }
  148. private void StartIconApp(IconInfo icon, IconStartType type)
  149. {
  150. try
  151. {
  152. using (Process p = new Process())
  153. {
  154. string startArg = icon.StartArg;
  155. if (startArg != null && Constants.SYSTEM_ICONS.ContainsKey(startArg))
  156. {
  157. StartSystemApp(startArg, type);
  158. }
  159. else
  160. {
  161. p.StartInfo.FileName = icon.Path;
  162. if (!StringUtil.IsEmpty(startArg))
  163. {
  164. p.StartInfo.Arguments = startArg;
  165. }
  166. if (icon.IconType == IconType.OTHER)
  167. {
  168. if (!File.Exists(icon.Path) && !Directory.Exists(icon.Path))
  169. {
  170. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
  171. return;
  172. }
  173. p.StartInfo.WorkingDirectory = icon.Path.Substring(0, icon.Path.LastIndexOf("\\"));
  174. switch (type)
  175. {
  176. case IconStartType.ADMIN_STARTUP:
  177. //p.StartInfo.Arguments = "1";//启动参数
  178. p.StartInfo.Verb = "runas";
  179. //p.StartInfo.CreateNoWindow = false; //设置显示窗口
  180. p.StartInfo.UseShellExecute = true;//不使用操作系统外壳程序启动进程
  181. //p.StartInfo.ErrorDialog = false;
  182. if (appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  183. {
  184. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  185. if (appData.AppConfig.MarginHide)
  186. {
  187. if (!MarginHide.IsMargin())
  188. {
  189. MainWindow.HideApp();
  190. }
  191. }
  192. else
  193. {
  194. MainWindow.HideApp();
  195. }
  196. }
  197. break;// c#好像不能case穿透
  198. case IconStartType.DEFAULT_STARTUP:
  199. if (appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  200. {
  201. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  202. if (appData.AppConfig.MarginHide)
  203. {
  204. if (!MarginHide.IsMargin())
  205. {
  206. MainWindow.HideApp();
  207. }
  208. }
  209. else
  210. {
  211. MainWindow.HideApp();
  212. }
  213. }
  214. break;
  215. case IconStartType.SHOW_IN_EXPLORE:
  216. p.StartInfo.FileName = "Explorer.exe";
  217. p.StartInfo.Arguments = "/e,/select," + icon.Path;
  218. break;
  219. }
  220. }
  221. else
  222. {
  223. if (appData.AppConfig.AppHideType == AppHideType.START_EXE && !RunTimeStatus.LOCK_APP_PANEL)
  224. {
  225. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  226. if (appData.AppConfig.MarginHide)
  227. {
  228. if (!MarginHide.IS_HIDE)
  229. {
  230. MainWindow.HideApp();
  231. }
  232. }
  233. else
  234. {
  235. MainWindow.HideApp();
  236. }
  237. }
  238. }
  239. p.Start();
  240. }
  241. }
  242. icon.Count++;
  243. //隐藏搜索框
  244. if (RunTimeStatus.SEARCH_BOX_SHOW)
  245. {
  246. MainWindow.mainWindow.HidedSearchBox();
  247. }
  248. }
  249. catch (Exception e)
  250. {
  251. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
  252. LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
  253. }
  254. }
  255. private void StartSystemApp(string startArg, IconStartType type)
  256. {
  257. if (type == IconStartType.SHOW_IN_EXPLORE)
  258. {
  259. Growl.WarningGlobal("系统项目不支持打开文件位置操作!");
  260. return;
  261. }
  262. switch (startArg)
  263. {
  264. case "Calculator":
  265. Process.Start("calc.exe");
  266. break;
  267. case "Computer":
  268. Process.Start("explorer.exe");
  269. break;
  270. case "GroupPolicy":
  271. Process.Start("gpedit.msc");
  272. break;
  273. case "Notepad":
  274. Process.Start("notepad");
  275. break;
  276. case "Network":
  277. Process.Start("ncpa.cpl");
  278. break;
  279. case "RecycleBin":
  280. Process.Start("shell:RecycleBinFolder");
  281. break;
  282. case "Registry":
  283. Process.Start("regedit.exe");
  284. break;
  285. case "Mstsc":
  286. if (type == IconStartType.ADMIN_STARTUP)
  287. {
  288. Process.Start("mstsc", "-admin");
  289. }
  290. else
  291. {
  292. Process.Start("mstsc");
  293. }
  294. break;
  295. case "Control":
  296. Process.Start("Control");
  297. break;
  298. case "CMD":
  299. if (type == IconStartType.ADMIN_STARTUP)
  300. {
  301. using (Process process = new Process())
  302. {
  303. process.StartInfo.FileName = "cmd.exe";
  304. process.StartInfo.Verb = "runas";
  305. process.Start();
  306. }
  307. }
  308. else
  309. {
  310. Process.Start("cmd");
  311. }
  312. break;
  313. case "Services":
  314. Process.Start("services.msc");
  315. break;
  316. }
  317. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  318. if (appData.AppConfig.MarginHide)
  319. {
  320. if (!MarginHide.IS_HIDE)
  321. {
  322. MainWindow.HideApp();
  323. }
  324. }
  325. else
  326. {
  327. MainWindow.HideApp();
  328. }
  329. }
  330. /// <summary>
  331. /// 拖动添加项目
  332. /// </summary>
  333. /// <param name="sender"></param>
  334. /// <param name="e"></param>
  335. private void Wrap_Drop(object sender, DragEventArgs e)
  336. {
  337. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  338. if (dropObject == null) return;
  339. foreach (object obj in dropObject)
  340. {
  341. string path = (string)obj;
  342. IconInfo iconInfo = CommonCode.GetIconInfoByPath(path);
  343. MainWindow.appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Add(iconInfo);
  344. }
  345. CommonCode.SortIconList();
  346. CommonCode.SaveAppData(MainWindow.appData);
  347. }
  348. /// <summary>
  349. /// 从列表删除图标
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void RemoveIcon(object sender, RoutedEventArgs e)
  354. {
  355. appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
  356. }
  357. private void SystemContextMenu(object sender, RoutedEventArgs e)
  358. {
  359. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  360. DirectoryInfo[] folders = new DirectoryInfo[1];
  361. folders[0] = new DirectoryInfo(icon.Path);
  362. ShellContextMenu scm = new ShellContextMenu();
  363. System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
  364. p.X -= 80;
  365. p.Y -= 80;
  366. scm.ShowContextMenu(folders, p);
  367. }
  368. /// <summary>
  369. /// 弹出Icon属性修改面板
  370. /// </summary>
  371. /// <param name="sender"></param>
  372. /// <param name="e"></param>
  373. private void PropertyConfig(object sender, RoutedEventArgs e)
  374. {
  375. IconInfo info = (IconInfo)((MenuItem)sender).Tag;
  376. switch (info.IconType)
  377. {
  378. case IconType.URL:
  379. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog(info);
  380. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  381. break;
  382. default:
  383. IconInfoDialog dialog = new IconInfoDialog(info);
  384. dialog.dialog = HandyControl.Controls.Dialog.Show(dialog, "IconInfoDialog");
  385. break;
  386. }
  387. }
  388. private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
  389. {
  390. double width = appData.AppConfig.ImageWidth;
  391. double height = appData.AppConfig.ImageHeight;
  392. width += width * 0.15;
  393. height += height * 0.15;
  394. Thread t = new Thread(() =>
  395. {
  396. this.Dispatcher.BeginInvoke(new Action(() =>
  397. {
  398. ImgStoryBoard(sender, (int)width, (int)height, 1, true);
  399. }));
  400. });
  401. t.IsBackground = true;
  402. t.Start();
  403. }
  404. private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
  405. {
  406. Thread t = new Thread(() =>
  407. {
  408. this.Dispatcher.BeginInvoke(new Action(() =>
  409. {
  410. ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 260);
  411. }));
  412. });
  413. t.IsBackground = true;
  414. t.Start();
  415. }
  416. private void ImgStoryBoard(object sender, int height, int width, int milliseconds, bool checkRmStoryboard = false)
  417. {
  418. if (appData.AppConfig.PMModel) return;
  419. //int count = 0;
  420. //Panel sp = sender as Panel;
  421. //Image img = sp.Children[0] as Image;
  422. //int nowH = (int)img.Height;
  423. //bool isSmall = nowH > height;
  424. //if (!isSmall)
  425. //{
  426. // img.Height = height;
  427. // img.Width = width;
  428. // return;
  429. //}
  430. //double subLen = (double)Math.Abs(nowH - height) / (double)milliseconds;
  431. //new Thread(() =>
  432. //{
  433. // this.Dispatcher.Invoke(() =>
  434. // {
  435. // while (count < milliseconds)
  436. // {
  437. // if (!isSmall)
  438. // {
  439. // img.Height += subLen;
  440. // img.Width += subLen;
  441. // } else
  442. // {
  443. // //if (img.Height > 1)
  444. // //{
  445. // // img.Height -= 1;
  446. // // img.Width -= 1;
  447. // //}
  448. // Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
  449. // new Action(delegate {
  450. // img.Height -= subLen;
  451. // img.Width -= subLen;
  452. // }));
  453. // //img.Height -= subLen;
  454. // //img.Width -= subLen;
  455. // }
  456. // count++;
  457. // Thread.Sleep(1);
  458. // }
  459. // img.Height = height;
  460. // img.Width = width;
  461. // });
  462. //}).Start();
  463. Panel sp = sender as Panel;
  464. DependencyObject dos = sp.Parent;
  465. Image img = sp.Children[0] as Image;
  466. double afterHeight = img.Height;
  467. double afterWidth = img.Width;
  468. //动画定义
  469. Storyboard myStoryboard = new Storyboard();
  470. DoubleAnimation heightAnimation = new DoubleAnimation
  471. {
  472. From = afterHeight,
  473. To = height,
  474. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  475. };
  476. DoubleAnimation widthAnimation = new DoubleAnimation
  477. {
  478. From = afterWidth,
  479. To = width,
  480. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  481. };
  482. Timeline.SetDesiredFrameRate(heightAnimation, 60);
  483. Timeline.SetDesiredFrameRate(widthAnimation, 60);
  484. Storyboard.SetTarget(widthAnimation, img);
  485. Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));
  486. Storyboard.SetTarget(heightAnimation, img);
  487. Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("Height"));
  488. myStoryboard.Children.Add(heightAnimation);
  489. myStoryboard.Children.Add(widthAnimation);
  490. CheckRemoveStoryboard crs = new CheckRemoveStoryboard
  491. {
  492. sb = myStoryboard,
  493. sp = sp,
  494. heightAnimation = heightAnimation,
  495. widthAnimation = widthAnimation,
  496. img = img,
  497. isMouseOver = !checkRmStoryboard
  498. };
  499. heightAnimation.Completed += (s, e) =>
  500. {
  501. if (checkRmStoryboard)
  502. {
  503. ThreadStart ts = new ThreadStart(crs.Remove);
  504. System.Threading.Thread t = new System.Threading.Thread(ts);
  505. t.IsBackground = true;
  506. t.Start();
  507. }
  508. else
  509. {
  510. img.BeginAnimation(WidthProperty, null);
  511. img.BeginAnimation(HeightProperty, null);
  512. }
  513. };
  514. img.BeginAnimation(WidthProperty, widthAnimation);
  515. img.BeginAnimation(HeightProperty, heightAnimation);
  516. //###################################################################
  517. //myStoryboard.Completed += (s, e) =>
  518. //{
  519. // if (checkRmStoryboard)
  520. // {
  521. // ThreadStart ts = new ThreadStart(crs.Remove);
  522. // System.Threading.Thread t = new System.Threading.Thread(ts);
  523. // t.Start();
  524. // }
  525. // else
  526. // {
  527. // myStoryboard.Remove();
  528. // }
  529. //};
  530. //myStoryboard.Begin();
  531. }
  532. private class CheckRemoveStoryboard
  533. {
  534. public Storyboard sb;
  535. public Panel sp;
  536. public Image img;
  537. public DoubleAnimation heightAnimation;
  538. public DoubleAnimation widthAnimation;
  539. public bool isMouseOver;
  540. public void Remove()
  541. {
  542. while (true)
  543. {
  544. if (sp.IsMouseOver == isMouseOver)
  545. {
  546. App.Current.Dispatcher.Invoke((Action)(() =>
  547. {
  548. img.BeginAnimation(WidthProperty, null);
  549. img.BeginAnimation(HeightProperty, null);
  550. //heightAnimation.FillBehavior = FillBehavior.Stop;
  551. //widthAnimation.FillBehavior = FillBehavior.Stop;
  552. }));
  553. return;
  554. }
  555. else
  556. {
  557. System.Threading.Thread.Sleep(500);
  558. }
  559. }
  560. }
  561. }
  562. public void RemoveSB(Object sb)
  563. {
  564. Storyboard sb2 = sb as Storyboard;
  565. System.Threading.Thread.Sleep(500);
  566. sb2.Remove();
  567. }
  568. /// <summary>
  569. /// 添加URL项目
  570. /// </summary>
  571. /// <param name="sender"></param>
  572. /// <param name="e"></param>
  573. private void AddUrlIcon(object sender, RoutedEventArgs e)
  574. {
  575. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog();
  576. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  577. }
  578. /// <summary>
  579. /// 添加系统项目
  580. /// </summary>
  581. /// <param name="sender"></param>
  582. /// <param name="e"></param>
  583. private void AddSystemIcon(object sender, RoutedEventArgs e)
  584. {
  585. SystemItemWindow.Show();
  586. }
  587. public void VisibilitySearchCard(Visibility vb)
  588. {
  589. VerticalCard.Visibility = vb;
  590. if (vb == Visibility.Visible)
  591. {
  592. WrapCard.Visibility = Visibility.Collapsed;
  593. }
  594. else
  595. {
  596. WrapCard.Visibility = Visibility.Visible;
  597. }
  598. }
  599. /// <summary>
  600. /// 搜索Card点击事件
  601. /// </summary>
  602. /// <param name="sender"></param>
  603. /// <param name="e"></param>
  604. private void VerticalCard_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  605. {
  606. //隐藏搜索框
  607. if (RunTimeStatus.SEARCH_BOX_SHOW)
  608. {
  609. MainWindow.mainWindow.HidedSearchBox();
  610. }
  611. }
  612. /// <summary>
  613. /// 设置光标
  614. /// </summary>
  615. /// <param name="sender"></param>
  616. /// <param name="e"></param>
  617. private void CursorPanel_MouseEnter(object sender, MouseEventArgs e)
  618. {
  619. this.Cursor = Cursors.Hand;
  620. }
  621. /// <summary>
  622. /// 设置光标
  623. /// </summary>
  624. /// <param name="sender"></param>
  625. /// <param name="e"></param>
  626. private void CursorPanel_MouseLeave(object sender, MouseEventArgs e)
  627. {
  628. this.Cursor = Cursors.Arrow;
  629. }
  630. /// <summary>
  631. /// 锁定/解锁主面板
  632. /// </summary>
  633. /// <param name="sender"></param>
  634. /// <param name="e"></param>
  635. private void LockAppPanel(object sender, RoutedEventArgs e)
  636. {
  637. RunTimeStatus.LOCK_APP_PANEL = !RunTimeStatus.LOCK_APP_PANEL;
  638. }
  639. private void WrapCard_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  640. {
  641. if (RunTimeStatus.LOCK_APP_PANEL)
  642. {
  643. CardLockCM.Header = "解锁主面板";
  644. } else
  645. {
  646. CardLockCM.Header = "锁定主面板";
  647. }
  648. }
  649. }
  650. }