RightCardControl.xaml.cs 23 KB

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