RightCardControl.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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)((SimpleStackPanel)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. catch (Exception e)
  246. {
  247. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
  248. LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
  249. }
  250. }
  251. private void StartSystemApp(string startArg, IconStartType type)
  252. {
  253. if (type == IconStartType.SHOW_IN_EXPLORE)
  254. {
  255. Growl.WarningGlobal("系统项目不支持打开文件位置操作!");
  256. return;
  257. }
  258. switch (startArg)
  259. {
  260. case "Calculator":
  261. Process.Start("calc.exe");
  262. break;
  263. case "Computer":
  264. Process.Start("explorer.exe");
  265. break;
  266. case "GroupPolicy":
  267. Process.Start("gpedit.msc");
  268. break;
  269. case "Notepad":
  270. Process.Start("notepad");
  271. break;
  272. case "Network":
  273. Process.Start("ncpa.cpl");
  274. break;
  275. case "RecycleBin":
  276. Process.Start("shell:RecycleBinFolder");
  277. break;
  278. case "Registry":
  279. Process.Start("regedit.exe");
  280. break;
  281. case "Mstsc":
  282. if (type == IconStartType.ADMIN_STARTUP)
  283. {
  284. Process.Start("mstsc", "-admin");
  285. }
  286. else
  287. {
  288. Process.Start("mstsc");
  289. }
  290. break;
  291. case "Control":
  292. Process.Start("Control");
  293. break;
  294. case "CMD":
  295. if (type == IconStartType.ADMIN_STARTUP)
  296. {
  297. using (Process process = new Process())
  298. {
  299. process.StartInfo.FileName = "cmd.exe";
  300. process.StartInfo.Verb = "runas";
  301. process.Start();
  302. }
  303. }
  304. else
  305. {
  306. Process.Start("cmd");
  307. }
  308. break;
  309. case "Services":
  310. Process.Start("services.msc");
  311. break;
  312. }
  313. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  314. if (appData.AppConfig.MarginHide)
  315. {
  316. if (!MarginHide.IS_HIDE)
  317. {
  318. MainWindow.HideApp();
  319. }
  320. }
  321. else
  322. {
  323. MainWindow.HideApp();
  324. }
  325. }
  326. /// <summary>
  327. /// 拖动添加项目
  328. /// </summary>
  329. /// <param name="sender"></param>
  330. /// <param name="e"></param>
  331. private void Wrap_Drop(object sender, DragEventArgs e)
  332. {
  333. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  334. if (dropObject == null) return;
  335. foreach (object obj in dropObject)
  336. {
  337. string path = (string)obj;
  338. IconInfo iconInfo = CommonCode.GetIconInfoByPath(path);
  339. MainWindow.appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Add(iconInfo);
  340. }
  341. CommonCode.SortIconList();
  342. CommonCode.SaveAppData(MainWindow.appData);
  343. }
  344. /// <summary>
  345. /// 从列表删除图标
  346. /// </summary>
  347. /// <param name="sender"></param>
  348. /// <param name="e"></param>
  349. private void RemoveIcon(object sender, RoutedEventArgs e)
  350. {
  351. appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
  352. }
  353. private void SystemContextMenu(object sender, RoutedEventArgs e)
  354. {
  355. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  356. DirectoryInfo[] folders = new DirectoryInfo[1];
  357. folders[0] = new DirectoryInfo(icon.Path);
  358. ShellContextMenu scm = new ShellContextMenu();
  359. System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
  360. p.X -= 80;
  361. p.Y -= 80;
  362. scm.ShowContextMenu(folders, p);
  363. }
  364. /// <summary>
  365. /// 弹出Icon属性修改面板
  366. /// </summary>
  367. /// <param name="sender"></param>
  368. /// <param name="e"></param>
  369. private void PropertyConfig(object sender, RoutedEventArgs e)
  370. {
  371. IconInfo info = (IconInfo)((MenuItem)sender).Tag;
  372. switch (info.IconType)
  373. {
  374. case IconType.URL:
  375. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog(info);
  376. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  377. break;
  378. default:
  379. IconInfoDialog dialog = new IconInfoDialog(info);
  380. dialog.dialog = HandyControl.Controls.Dialog.Show(dialog, "IconInfoDialog");
  381. break;
  382. }
  383. }
  384. private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
  385. {
  386. double width = appData.AppConfig.ImageWidth;
  387. double height = appData.AppConfig.ImageHeight;
  388. width += width * 0.15;
  389. height += height * 0.15;
  390. ImgStoryBoard(sender, (int)width, (int)height, 1, true);
  391. }
  392. private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
  393. {
  394. ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 220);
  395. }
  396. private void ImgStoryBoard(object sender, int height, int width, int milliseconds, bool checkRmStoryboard = false)
  397. {
  398. if (appData.AppConfig.PMModel) return;
  399. Panel sp = sender as Panel;
  400. DependencyObject dos = sp.Parent;
  401. Image img = sp.Children[0] as Image;
  402. double afterHeight = img.Height;
  403. double afterWidth = img.Width;
  404. //动画定义
  405. Storyboard myStoryboard = new Storyboard();
  406. DoubleAnimation heightAnimation = new DoubleAnimation
  407. {
  408. From = afterHeight,
  409. To = height,
  410. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  411. };
  412. DoubleAnimation widthAnimation = new DoubleAnimation
  413. {
  414. From = afterWidth,
  415. To = width,
  416. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  417. };
  418. Timeline.SetDesiredFrameRate(heightAnimation, 60);
  419. Timeline.SetDesiredFrameRate(widthAnimation, 60);
  420. Storyboard.SetTarget(widthAnimation, img);
  421. Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));
  422. Storyboard.SetTarget(heightAnimation, img);
  423. Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("Height"));
  424. myStoryboard.Children.Add(heightAnimation);
  425. myStoryboard.Children.Add(widthAnimation);
  426. CheckRemoveStoryboard crs = new CheckRemoveStoryboard
  427. {
  428. sb = myStoryboard,
  429. sp = sp,
  430. heightAnimation = heightAnimation,
  431. widthAnimation = widthAnimation,
  432. img = img,
  433. isMouseOver = !checkRmStoryboard
  434. };
  435. heightAnimation.Completed += (s, e) =>
  436. {
  437. if (checkRmStoryboard)
  438. {
  439. ThreadStart ts = new ThreadStart(crs.Remove);
  440. System.Threading.Thread t = new System.Threading.Thread(ts);
  441. t.Start();
  442. }
  443. else
  444. {
  445. img.BeginAnimation(WidthProperty, null);
  446. img.BeginAnimation(HeightProperty, null);
  447. }
  448. };
  449. img.BeginAnimation(WidthProperty, widthAnimation);
  450. img.BeginAnimation(HeightProperty, heightAnimation);
  451. //myStoryboard.Completed += (s, e) =>
  452. //{
  453. // if (checkRmStoryboard || true)
  454. // {
  455. // ThreadStart ts = new ThreadStart(crs.Remove);
  456. // System.Threading.Thread t = new System.Threading.Thread(ts);
  457. // t.Start();
  458. // }
  459. // else
  460. // {
  461. // myStoryboard.Remove();
  462. // }
  463. //};
  464. //myStoryboard.Begin();
  465. }
  466. private class CheckRemoveStoryboard
  467. {
  468. public Storyboard sb;
  469. public Panel sp;
  470. public Image img;
  471. public DoubleAnimation heightAnimation;
  472. public DoubleAnimation widthAnimation;
  473. public bool isMouseOver;
  474. public void Remove()
  475. {
  476. while (true)
  477. {
  478. if (sp.IsMouseOver == isMouseOver)
  479. {
  480. App.Current.Dispatcher.Invoke((Action)(() =>
  481. {
  482. img.BeginAnimation(WidthProperty, null);
  483. img.BeginAnimation(HeightProperty, null);
  484. //heightAnimation.FillBehavior = FillBehavior.Stop;
  485. //widthAnimation.FillBehavior = FillBehavior.Stop;
  486. }));
  487. return;
  488. }
  489. else
  490. {
  491. System.Threading.Thread.Sleep(500);
  492. }
  493. }
  494. }
  495. }
  496. public void RemoveSB(Object sb)
  497. {
  498. Storyboard sb2 = sb as Storyboard;
  499. System.Threading.Thread.Sleep(500);
  500. sb2.Remove();
  501. }
  502. /// <summary>
  503. /// 添加URL项目
  504. /// </summary>
  505. /// <param name="sender"></param>
  506. /// <param name="e"></param>
  507. private void AddUrlIcon(object sender, RoutedEventArgs e)
  508. {
  509. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog();
  510. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  511. }
  512. /// <summary>
  513. /// 添加系统项目
  514. /// </summary>
  515. /// <param name="sender"></param>
  516. /// <param name="e"></param>
  517. private void AddSystemIcon(object sender, RoutedEventArgs e)
  518. {
  519. SystemItemWindow.Show();
  520. }
  521. public void VisibilitySearchCard(Visibility vb)
  522. {
  523. VerticalCard.Visibility = vb;
  524. if (vb == Visibility.Visible)
  525. {
  526. WrapCard.Visibility = Visibility.Collapsed;
  527. }
  528. else
  529. {
  530. WrapCard.Visibility = Visibility.Visible;
  531. }
  532. }
  533. }
  534. }