RightCardControl.xaml.cs 19 KB

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