MainWindow.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control;
  4. using GeekDesk.Util;
  5. using GeekDesk.ViewModel;
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. namespace GeekDesk
  14. {
  15. /// <summary>
  16. /// MainWindow.xaml 的交互逻辑
  17. /// </summary>
  18. ///
  19. public partial class MainWindow : Window
  20. {
  21. public static AppData appData = CommonCode.GetAppDataByFile();
  22. private int menuSelectIndexTemp = -1;
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. loadData();
  27. this.Loaded += Window_Loaded;
  28. this.SizeChanged += MainWindow_Resize;
  29. this.Topmost = true;
  30. }
  31. private void loadData()
  32. {
  33. this.DataContext = appData;
  34. if (appData.MenuList.Count == 0)
  35. {
  36. appData.MenuList.Add(new MenuInfo() { MenuName = "NewMenu", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = Visibility.Collapsed});
  37. }
  38. this.Visibility = appData.AppConfig.StartedShowPanel;
  39. //窗体大小
  40. LeftColumn.Width = new GridLength(appData.AppConfig.MenuCardWidth);
  41. this.Width = appData.AppConfig.WindowWidth;
  42. this.Height = appData.AppConfig.WindowHeight;
  43. //选中 菜单
  44. menus.SelectedIndex = appData.AppConfig.SelectedMenuIndex;
  45. //图标数据
  46. icons.ItemsSource = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  47. }
  48. #region 图标拖动
  49. DelegateCommand<int[]> _swap;
  50. public DelegateCommand<int[]> SwapCommand
  51. {
  52. get
  53. {
  54. if (_swap == null)
  55. _swap = new DelegateCommand<int[]>(
  56. (indexes) =>
  57. {
  58. int fromS = indexes[0];
  59. int to = indexes[1];
  60. ObservableCollection<IconInfo> iconList = appData.MenuList[menus.SelectedIndex].IconList;
  61. var elementSource = iconList[to];
  62. var dragged = iconList[fromS];
  63. iconList.Remove(dragged);
  64. iconList.Insert(to, dragged);
  65. CommonCode.SaveAppData(appData);
  66. }
  67. );
  68. return _swap;
  69. }
  70. }
  71. DelegateCommand<int[]> _swap2;
  72. public DelegateCommand<int[]> SwapCommand2
  73. {
  74. get
  75. {
  76. if (_swap2 == null)
  77. _swap2 = new DelegateCommand<int[]>(
  78. (indexes) =>
  79. {
  80. int fromS = indexes[0];
  81. int to = indexes[1];
  82. ObservableCollection<MenuInfo> menuList = appData.MenuList;
  83. var elementSource = menuList[to];
  84. var dragged = menuList[fromS];
  85. menuList.Remove(dragged);
  86. menuList.Insert(to, dragged);
  87. menus.SelectedIndex = to;
  88. appData.MenuList = menuList;
  89. CommonCode.SaveAppData(appData);
  90. }
  91. );
  92. return _swap2;
  93. }
  94. }
  95. #endregion 图标拖动
  96. private void Wrap_Drop(object sender, DragEventArgs e)
  97. {
  98. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  99. if (dropObject == null) return;
  100. foreach (object obj in dropObject)
  101. {
  102. string path = (string)obj;
  103. IconInfo iconInfo = new IconInfo
  104. {
  105. Path = path,
  106. BitmapImage = ImageUtil.GetBitmapIconByPath(path)
  107. };
  108. iconInfo.DefaultImage = iconInfo.ImageByteArr;
  109. iconInfo.Name = Path.GetFileNameWithoutExtension(path);
  110. appData.MenuList[menus.SelectedIndex].IconList.Add(iconInfo);
  111. }
  112. CommonCode.SaveAppData(appData);
  113. }
  114. ////菜单点击事件
  115. private void MenuClick(object sender, MouseButtonEventArgs e)
  116. {
  117. //设置对应菜单的图标列表
  118. MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
  119. icons.ItemsSource = mi.IconList;
  120. appData.AppConfig.SelectedMenuIndex = menus.Items.IndexOf(mi);
  121. CommonCode.SaveAppData(appData);
  122. }
  123. /// <summary>
  124. /// 图标点击事件
  125. /// </summary>
  126. /// <param name="sender"></param>
  127. /// <param name="e"></param>
  128. private void IconClick(object sender, MouseButtonEventArgs e)
  129. {
  130. IconInfo icon = (IconInfo)((StackPanel)sender).Tag;
  131. if (icon.AdminStartUp)
  132. {
  133. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  134. }
  135. else
  136. {
  137. StartIconApp(icon, IconStartType.DEFAULT_STARTUP);
  138. }
  139. }
  140. /// <summary>
  141. /// 管理员方式启动
  142. /// </summary>
  143. /// <param name="sender"></param>
  144. /// <param name="e"></param>
  145. private void IconAdminStart(object sender, RoutedEventArgs e)
  146. {
  147. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  148. StartIconApp(icon, IconStartType.ADMIN_STARTUP);
  149. }
  150. /// <summary>
  151. /// 打开文件所在位置
  152. /// </summary>
  153. /// <param name="sender"></param>
  154. /// <param name="e"></param>
  155. private void ShowInExplore(object sender, RoutedEventArgs e)
  156. {
  157. IconInfo icon = (IconInfo)((MenuItem)sender).Tag;
  158. StartIconApp(icon, IconStartType.SHOW_IN_EXPLORE);
  159. }
  160. private void StartIconApp(IconInfo icon, IconStartType type)
  161. {
  162. try
  163. {
  164. if (!File.Exists(icon.Path) && !Directory.Exists(icon.Path))
  165. {
  166. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
  167. return;
  168. }
  169. Process p = new Process();
  170. p.StartInfo.FileName = icon.Path;
  171. switch (type) {
  172. case IconStartType.ADMIN_STARTUP:
  173. p.StartInfo.Arguments = "1";//启动参数
  174. p.StartInfo.Verb = "runas";
  175. p.StartInfo.CreateNoWindow = false; //设置显示窗口
  176. p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动进程
  177. p.StartInfo.ErrorDialog = false;
  178. this.Visibility = Visibility.Collapsed;
  179. break;// c#好像不能case穿透
  180. case IconStartType.DEFAULT_STARTUP:
  181. this.Visibility = Visibility.Collapsed;
  182. break;
  183. case IconStartType.SHOW_IN_EXPLORE:
  184. p.StartInfo.FileName = "Explorer.exe";
  185. p.StartInfo.Arguments = "/e,/select," + icon.Path;
  186. break;
  187. }
  188. p.Start();
  189. icon.Count++;
  190. CommonCode.SaveAppData(appData);
  191. } catch (Exception)
  192. {
  193. HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
  194. }
  195. }
  196. /// <summary>
  197. /// data选中事件 设置不可选中
  198. /// </summary>
  199. /// <param name="sender"></param>
  200. /// <param name="e"></param>
  201. private void IconSelectionChanged(object sender, SelectionChangedEventArgs e)
  202. {
  203. if (icons.SelectedIndex != -1) icons.SelectedIndex = -1;
  204. }
  205. void Window_Loaded(object sender, RoutedEventArgs e)
  206. {
  207. //加载完毕注册热键
  208. Hotkey.Regist(this, HotkeyModifiers.MOD_CONTROL, Key.Y, ()=>
  209. {
  210. if (this.Visibility == Visibility.Collapsed)
  211. {
  212. ShowAppAndFollowMouse();
  213. } else
  214. {
  215. this.Visibility = Visibility.Collapsed;
  216. }
  217. });
  218. }
  219. void MainWindow_Resize(object sender, System.EventArgs e)
  220. {
  221. if (this.DataContext != null)
  222. {
  223. AppData appData = this.DataContext as AppData;
  224. appData.AppConfig.WindowWidth = this.Width;
  225. appData.AppConfig.WindowHeight = this.Height;
  226. CommonCode.SaveAppData(appData);
  227. }
  228. }
  229. /// <summary>
  230. /// 删除菜单
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void DeleteMenu(object sender, RoutedEventArgs e)
  235. {
  236. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  237. appData.MenuList.Remove(menuInfo);
  238. CommonCode.SaveAppData(appData);
  239. }
  240. private void DragMove(object sender, MouseEventArgs e)
  241. {
  242. //if (e.LeftButton == MouseButtonState.Pressed)
  243. //{
  244. // this.DragMove();
  245. //}
  246. if (e.LeftButton == MouseButtonState.Pressed)
  247. {
  248. var windowMode = this.ResizeMode;
  249. if (this.ResizeMode != ResizeMode.NoResize)
  250. {
  251. this.ResizeMode = ResizeMode.NoResize;
  252. }
  253. this.UpdateLayout();
  254. /* 当点击拖拽区域的时候,让窗口跟着移动
  255. (When clicking the drag area, make the window follow) */
  256. DragMove();
  257. if (this.ResizeMode != windowMode)
  258. {
  259. this.ResizeMode = windowMode;
  260. }
  261. this.UpdateLayout();
  262. }
  263. }
  264. /// <summary>
  265. /// 重命名菜单 将textbox 设置为可见
  266. /// </summary>
  267. /// <param name="sender"></param>
  268. /// <param name="e"></param>
  269. private void RenameMenu(object sender, RoutedEventArgs e)
  270. {
  271. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  272. menuInfo.MenuEdit = (int)Visibility.Visible;
  273. }
  274. /// <summary>
  275. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  276. /// </summary>
  277. /// <param name="sender"></param>
  278. /// <param name="e"></param>
  279. private void LostFocusOrEnterDown(object sender, EventArgs e)
  280. {
  281. TextBox menuBox = null;
  282. if (e.GetType() == typeof(KeyEventArgs))
  283. {
  284. KeyEventArgs eKey = e as KeyEventArgs;
  285. if (eKey.Key == Key.Enter)
  286. {
  287. menuBox = ((TextBox)sender);
  288. }
  289. } else if(e.GetType() == typeof(RoutedEventArgs))
  290. {
  291. menuBox = ((TextBox)sender);
  292. }
  293. if (menuBox != null)
  294. {
  295. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  296. string text = menuBox.Text;
  297. menuInfo.MenuName = text;
  298. menuInfo.MenuEdit = Visibility.Collapsed;
  299. CommonCode.SaveAppData(appData);
  300. }
  301. }
  302. /// <summary>
  303. /// 当修改菜单元素可见时 设置全选并获得焦点
  304. /// </summary>
  305. /// <param name="sender"></param>
  306. /// <param name="e"></param>
  307. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  308. {
  309. TextBox box = sender as TextBox;
  310. if (box.Visibility == Visibility.Visible)
  311. {
  312. Keyboard.Focus(box);
  313. box.SelectAll();
  314. }
  315. }
  316. /// <summary>
  317. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  318. /// 修改菜单元素不可见时 原菜单可见 并 选中
  319. /// </summary>
  320. /// <param name="sender"></param>
  321. /// <param name="e"></param>
  322. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  323. {
  324. TextBlock tb = sender as TextBlock;
  325. if (tb.Visibility == Visibility.Collapsed)
  326. {
  327. if (menus.SelectedIndex != -1)
  328. {
  329. menuSelectIndexTemp = menus.SelectedIndex;
  330. menus.SelectedIndex = -1;
  331. } else
  332. {
  333. menus.SelectedIndex = menuSelectIndexTemp;
  334. }
  335. }
  336. }
  337. /// <summary>
  338. /// 新建菜单
  339. /// </summary>
  340. /// <param name="sender"></param>
  341. /// <param name="e"></param>
  342. private void CreateMenu(object sender, RoutedEventArgs e)
  343. {
  344. appData.MenuList.Add(new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" });
  345. menus.SelectedIndex = appData.MenuList.Count - 1;
  346. //appData.MenuList[appData.MenuList.Count - 1].MenuEdit = (int)Visibility.Visible;
  347. CommonCode.SaveAppData(appData);
  348. }
  349. /// <summary>
  350. /// 关闭按钮单击事件
  351. /// </summary>
  352. /// <param name="sender"></param>
  353. /// <param name="e"></param>
  354. private void CloseButtonClick(object sender, RoutedEventArgs e)
  355. {
  356. this.Visibility = Visibility.Collapsed;
  357. }
  358. /// <summary>
  359. /// 弹出Icon属性修改面板
  360. /// </summary>
  361. /// <param name="sender"></param>
  362. /// <param name="e"></param>
  363. private void PropertyConfig(object sender, RoutedEventArgs e)
  364. {
  365. HandyControl.Controls.Dialog.Show(new IconInfoDialog((IconInfo)((MenuItem)sender).Tag));
  366. }
  367. /// <summary>
  368. /// 从列表删除图标
  369. /// </summary>
  370. /// <param name="sender"></param>
  371. /// <param name="e"></param>
  372. private void RemoveIcon(object sender, RoutedEventArgs e)
  373. {
  374. appData.MenuList[menus.SelectedIndex].IconList.Remove((IconInfo)((MenuItem)sender).Tag);
  375. CommonCode.SaveAppData(appData);
  376. }
  377. /// <summary>
  378. /// 左侧栏宽度改变 持久化
  379. /// </summary>
  380. /// <param name="sender"></param>
  381. /// <param name="e"></param>
  382. private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  383. {
  384. appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
  385. CommonCode.SaveAppData(appData);
  386. }
  387. /// <summary>
  388. /// 随鼠标位置显示面板 (鼠标始终在中间)
  389. /// </summary>
  390. private void ShowAppAndFollowMouse()
  391. {
  392. //获取鼠标位置
  393. Point p = MouseUtil.GetMousePosition();
  394. double left = SystemParameters.VirtualScreenLeft;
  395. double top = SystemParameters.VirtualScreenTop;
  396. double width = SystemParameters.VirtualScreenWidth;
  397. double height = SystemParameters.VirtualScreenHeight;
  398. double right = width - Math.Abs(left);
  399. double bottom = height - Math.Abs(top);
  400. if (p.X - this.Width / 2 < left)
  401. {
  402. //判断是否在最左边缘
  403. this.Left = left;
  404. } else if (p.X + this.Width / 2 > right)
  405. {
  406. //判断是否在最右边缘
  407. this.Left = right - this.Width;
  408. } else
  409. {
  410. this.Left = p.X - this.Width / 2;
  411. }
  412. if (p.Y - this.Height / 2 < top)
  413. {
  414. //判断是否在最上边缘
  415. this.Top = top;
  416. } else if (p.Y + this.Height/2 > bottom)
  417. {
  418. //判断是否在最下边缘
  419. this.Top = bottom - this.Height;
  420. } else
  421. {
  422. this.Top = p.Y - this.Height / 2;
  423. }
  424. this.Visibility = Visibility.Visible;
  425. }
  426. /// <summary>
  427. /// 右键任务栏图标 显示主面板
  428. /// </summary>
  429. /// <param name="sender"></param>
  430. /// <param name="e"></param>
  431. private void ShowApp(object sender, RoutedEventArgs e)
  432. {
  433. ShowApp();
  434. }
  435. private void ShowApp()
  436. {
  437. this.Visibility = Visibility.Visible;
  438. ShowAppAndFollowMouse();
  439. }
  440. /// <summary>
  441. /// 图片图标单击事件
  442. /// </summary>
  443. /// <param name="sender"></param>
  444. /// <param name="e"></param>
  445. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  446. {
  447. if (this.Visibility == Visibility.Collapsed)
  448. {
  449. ShowApp();
  450. }
  451. else
  452. {
  453. this.Visibility = Visibility.Collapsed;
  454. }
  455. }
  456. /// <summary>
  457. /// 右键任务栏图标 设置
  458. /// </summary>
  459. /// <param name="sender"></param>
  460. /// <param name="e"></param>
  461. private void ConfigApp(object sender, RoutedEventArgs e)
  462. {
  463. //MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  464. //appData.MenuList.Remove(menuInfo);
  465. CommonCode.SaveAppData(appData);
  466. }
  467. /// <summary>
  468. /// 右键任务栏图标退出
  469. /// </summary>
  470. /// <param name="sender"></param>
  471. /// <param name="e"></param>
  472. private void ExitApp(object sender, RoutedEventArgs e)
  473. {
  474. Application.Current.Shutdown();
  475. }
  476. private void MenuItem_Click(object sender, RoutedEventArgs e)
  477. {
  478. }
  479. /// <summary>
  480. /// 设置按钮左键弹出菜单
  481. /// </summary>
  482. /// <param name="sender"></param>
  483. /// <param name="e"></param>
  484. private void ConfigButtonClick(object sender, RoutedEventArgs e)
  485. {
  486. SettingMenu.IsOpen = true;
  487. }
  488. /// <summary>
  489. /// 禁用设置按钮右键菜单
  490. /// </summary>
  491. /// <param name="sender"></param>
  492. /// <param name="e"></param>
  493. private void SettingButton_Initialized(object sender, EventArgs e)
  494. {
  495. SettingButton.ContextMenu = null;
  496. }
  497. }
  498. }