RightCardControl.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. }
  267. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  268. if (appData.AppConfig.MarginHide)
  269. {
  270. if (!MarginHide.IS_HIDE)
  271. {
  272. MainWindow.HideApp();
  273. }
  274. }
  275. else
  276. {
  277. MainWindow.HideApp();
  278. }
  279. }
  280. /// <summary>
  281. /// 拖动添加项目
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private void Wrap_Drop(object sender, DragEventArgs e)
  286. {
  287. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  288. if (dropObject == null) return;
  289. foreach (object obj in dropObject)
  290. {
  291. string path = (string)obj;
  292. IconInfo iconInfo = CommonCode.GetIconInfoByPath(path);
  293. MainWindow.appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Add(iconInfo);
  294. CommonCode.SaveAppData(MainWindow.appData);
  295. }
  296. }
  297. /// <summary>
  298. /// 从列表删除图标
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void RemoveIcon(object sender, RoutedEventArgs e)
  303. {
  304. appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
  305. }
  306. private void SystemContextMenu(object sender, RoutedEventArgs e)
  307. {
  308. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  309. DirectoryInfo[] folders = new DirectoryInfo[1];
  310. folders[0] = new DirectoryInfo(icon.Path);
  311. ShellContextMenu scm = new ShellContextMenu();
  312. System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
  313. p.X -= 80;
  314. p.Y -= 80;
  315. scm.ShowContextMenu(folders, p);
  316. }
  317. /// <summary>
  318. /// 弹出Icon属性修改面板
  319. /// </summary>
  320. /// <param name="sender"></param>
  321. /// <param name="e"></param>
  322. private void PropertyConfig(object sender, RoutedEventArgs e)
  323. {
  324. IconInfo info = (IconInfo)((MenuItem)sender).Tag;
  325. switch (info.IconType)
  326. {
  327. case IconType.URL:
  328. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog(info);
  329. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  330. break;
  331. default:
  332. IconInfoDialog dialog = new IconInfoDialog(info);
  333. dialog.dialog = HandyControl.Controls.Dialog.Show(dialog, "IconInfoDialog");
  334. break;
  335. }
  336. }
  337. private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
  338. {
  339. double width = appData.AppConfig.ImageWidth;
  340. double height = appData.AppConfig.ImageHeight;
  341. width += width * 0.15;
  342. height += height * 0.15;
  343. ImgStoryBoard(sender, (int)width, (int)height, 1, true);
  344. }
  345. private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
  346. {
  347. ImgStoryBoard(sender, appData.AppConfig.ImageWidth, appData.AppConfig.ImageHeight, 220);
  348. }
  349. private void ImgStoryBoard(object sender, int height, int width, int milliseconds, bool checkRmStoryboard = false)
  350. {
  351. if (appData.AppConfig.PMModel) return;
  352. Panel sp = sender as Panel;
  353. DependencyObject dos = sp.Parent;
  354. Image img = sp.Children[0] as Image;
  355. double afterHeight = img.Height;
  356. double afterWidth = img.Width;
  357. //动画定义
  358. Storyboard myStoryboard = new Storyboard();
  359. DoubleAnimation heightAnimation = new DoubleAnimation
  360. {
  361. From = afterHeight,
  362. To = height,
  363. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  364. };
  365. DoubleAnimation widthAnimation = new DoubleAnimation
  366. {
  367. From = afterWidth,
  368. To = width,
  369. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  370. };
  371. Timeline.SetDesiredFrameRate(heightAnimation, 60);
  372. Timeline.SetDesiredFrameRate(widthAnimation, 60);
  373. Storyboard.SetTarget(widthAnimation, img);
  374. Storyboard.SetTargetProperty(widthAnimation, new PropertyPath("Width"));
  375. Storyboard.SetTarget(heightAnimation, img);
  376. Storyboard.SetTargetProperty(heightAnimation, new PropertyPath("Height"));
  377. myStoryboard.Children.Add(heightAnimation);
  378. myStoryboard.Children.Add(widthAnimation);
  379. CheckRemoveStoryboard crs = new CheckRemoveStoryboard
  380. {
  381. sb = myStoryboard,
  382. sp = sp,
  383. heightAnimation = heightAnimation,
  384. widthAnimation = widthAnimation,
  385. img = img,
  386. isMouseOver = !checkRmStoryboard
  387. };
  388. heightAnimation.Completed += (s, e) =>
  389. {
  390. if (checkRmStoryboard)
  391. {
  392. ThreadStart ts = new ThreadStart(crs.Remove);
  393. System.Threading.Thread t = new System.Threading.Thread(ts);
  394. t.Start();
  395. }
  396. else
  397. {
  398. img.BeginAnimation(WidthProperty, null);
  399. img.BeginAnimation(HeightProperty, null);
  400. }
  401. };
  402. img.BeginAnimation(WidthProperty, widthAnimation);
  403. img.BeginAnimation(HeightProperty, heightAnimation);
  404. //myStoryboard.Completed += (s, e) =>
  405. //{
  406. // if (checkRmStoryboard || true)
  407. // {
  408. // ThreadStart ts = new ThreadStart(crs.Remove);
  409. // System.Threading.Thread t = new System.Threading.Thread(ts);
  410. // t.Start();
  411. // }
  412. // else
  413. // {
  414. // myStoryboard.Remove();
  415. // }
  416. //};
  417. //myStoryboard.Begin();
  418. }
  419. private class CheckRemoveStoryboard
  420. {
  421. public Storyboard sb;
  422. public Panel sp;
  423. public Image img;
  424. public DoubleAnimation heightAnimation;
  425. public DoubleAnimation widthAnimation;
  426. public bool isMouseOver;
  427. public void Remove()
  428. {
  429. while (true)
  430. {
  431. if (sp.IsMouseOver == isMouseOver)
  432. {
  433. App.Current.Dispatcher.Invoke((Action)(() =>
  434. {
  435. img.BeginAnimation(WidthProperty, null);
  436. img.BeginAnimation(HeightProperty, null);
  437. //heightAnimation.FillBehavior = FillBehavior.Stop;
  438. //widthAnimation.FillBehavior = FillBehavior.Stop;
  439. }));
  440. return;
  441. }
  442. else
  443. {
  444. System.Threading.Thread.Sleep(500);
  445. }
  446. }
  447. }
  448. }
  449. public void RemoveSB(Object sb)
  450. {
  451. Storyboard sb2 = sb as Storyboard;
  452. System.Threading.Thread.Sleep(500);
  453. sb2.Remove();
  454. }
  455. /// <summary>
  456. /// 添加URL项目
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void AddUrlIcon(object sender, RoutedEventArgs e)
  461. {
  462. IconInfoUrlDialog urlDialog = new IconInfoUrlDialog();
  463. urlDialog.dialog = HandyControl.Controls.Dialog.Show(urlDialog, "IconInfoDialog");
  464. }
  465. /// <summary>
  466. /// 添加系统项目
  467. /// </summary>
  468. /// <param name="sender"></param>
  469. /// <param name="e"></param>
  470. private void AddSystemIcon(object sender, RoutedEventArgs e)
  471. {
  472. SystemItemWindow.Show();
  473. }
  474. }
  475. }