RightCardControl.xaml.cs 22 KB

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