MainWindow.xaml.cs 19 KB

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