RightCardControl.xaml.cs 19 KB

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