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