SystemItemWindow.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Other;
  3. using GeekDesk.Interface;
  4. using GeekDesk.Util;
  5. using GeekDesk.ViewModel;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Resources;
  13. using System.Threading;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Input;
  18. using System.Windows.Media.Imaging;
  19. using static GeekDesk.Util.ShowWindowFollowMouse;
  20. namespace GeekDesk.Control.Windows
  21. {
  22. /// <summary>
  23. /// SystemItemWindow.xaml 的交互逻辑
  24. /// 添加系统项目到对应菜单
  25. /// </summary>
  26. public partial class SystemItemWindow : Window, IWindowCommon
  27. {
  28. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  29. private static SystemItemViewModel vm;
  30. private List<IconInfo> systemIcons;
  31. private List<IconInfo> startMenuIcons;
  32. private List<IconInfo> storeIcons;
  33. private SystemItemWindow()
  34. {
  35. vm = new SystemItemViewModel();
  36. this.DataContext = vm;
  37. InitializeComponent();
  38. this.Topmost = true;
  39. }
  40. /// <summary>
  41. /// 移动窗口
  42. /// </summary>
  43. /// <param name="sender"></param>
  44. /// <param name="e"></param>
  45. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  46. {
  47. if (e.LeftButton == MouseButtonState.Pressed)
  48. {
  49. DragMove();
  50. }
  51. }
  52. private void Close_Click(object sender, RoutedEventArgs e)
  53. {
  54. this.DataContext = null;
  55. this.Close();
  56. }
  57. /// <summary>
  58. /// 切换选项卡
  59. /// </summary>
  60. /// <param name="sender"></param>
  61. /// <param name="e"></param>
  62. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  63. {
  64. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  65. List<IconInfo> systemInfos = vm.IconInfos;
  66. if (systemInfos == null)
  67. {
  68. systemInfos = new List<IconInfo>();
  69. }
  70. switch (ti.Tag.ToString())
  71. {
  72. case "StartMenu": //开始菜单
  73. if (startMenuIcons == null)
  74. {
  75. vm.IconInfos = null;
  76. System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(GetStartMenuInfos))
  77. {
  78. IsBackground = true
  79. };
  80. t.Start();
  81. } else
  82. {
  83. StartMenuLoading.Visibility = Visibility.Collapsed;
  84. vm.IconInfos = startMenuIcons;
  85. }
  86. break;
  87. case "Store": //应用商店
  88. if (storeIcons == null)
  89. {
  90. vm.IconInfos = null;
  91. storeIcons = new List<IconInfo>();
  92. vm.IconInfos = storeIcons;
  93. }
  94. else
  95. {
  96. vm.IconInfos = storeIcons;
  97. }
  98. break;
  99. default: //默认系统项
  100. if (systemIcons == null)
  101. {
  102. vm.IconInfos = null;
  103. systemIcons = GetSysteIconInfos();
  104. vm.IconInfos = systemIcons;
  105. } else
  106. {
  107. vm.IconInfos = systemIcons;
  108. }
  109. break;
  110. }
  111. }
  112. /// <summary>
  113. /// 获取开始菜单路径下项目
  114. /// </summary>
  115. /// <returns></returns>
  116. private void GetStartMenuInfos()
  117. {
  118. App.Current.Dispatcher.Invoke((Action)(() =>
  119. {
  120. StartMenuLoading.Visibility = Visibility.Visible;
  121. }));
  122. List<IconInfo> infos = new List<IconInfo>();
  123. //获取开始菜单路径
  124. string path = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs";
  125. //递归获取信息
  126. GetInfos(path, infos);
  127. App.Current.Dispatcher.Invoke((Action)(() =>
  128. {
  129. if (StartMenu.IsSelected)
  130. {
  131. startMenuIcons = infos;
  132. vm.IconInfos = startMenuIcons;
  133. }
  134. StartMenuLoading.Visibility = Visibility.Collapsed;
  135. }));
  136. }
  137. /// <summary>
  138. /// 递归获取文件信息
  139. /// </summary>
  140. /// <param name="path"></param>
  141. /// <param name="listInfos"></param>
  142. private void GetInfos(string filePath, List<IconInfo> listInfos)
  143. {
  144. DirectoryInfo di = new DirectoryInfo(filePath);
  145. string[] filePaths = Directory.GetFiles(filePath);
  146. string[] dirPaths = Directory.GetDirectories(filePath);
  147. string[] paths = new string[filePaths.Length + dirPaths.Length];
  148. filePaths.CopyTo(paths, 0);
  149. if (filePaths == null || filePaths.Length == 0)
  150. {
  151. dirPaths.CopyTo(paths, 0);
  152. } else
  153. {
  154. dirPaths.CopyTo(paths, filePaths.Length - 1);
  155. }
  156. foreach (string path in paths)
  157. {
  158. if (File.Exists(path))
  159. {
  160. string ext = Path.GetExtension(path).ToLower();
  161. if (".exe".Equals(ext) || ".lnk".Equals(ext))
  162. {
  163. try
  164. {
  165. IconInfo iconInfo = CommonCode.GetIconInfoByPath_NoWrite(path);
  166. if (iconInfo.Path_NoWrite != null)
  167. {
  168. iconInfo.Content_NoWrite = iconInfo.Path_NoWrite + "\n" + iconInfo.Name_NoWrite;
  169. listInfos.Add(iconInfo);
  170. }
  171. }
  172. catch (Exception) { }
  173. }
  174. }
  175. else if (Directory.Exists(path))
  176. {
  177. GetInfos(path, listInfos);
  178. }
  179. }
  180. //FileSystemInfo[] fileInfoArr = di.GetFileSystemInfos();
  181. //foreach(FileSystemInfo fi in fileInfoArr)
  182. //{
  183. // string path = fi.FullName;
  184. //}
  185. }
  186. /// <summary>
  187. /// 获取系统项目
  188. /// </summary>
  189. /// <returns></returns>
  190. private List<IconInfo> GetSysteIconInfos()
  191. {
  192. List<IconInfo> iconInfos = new List<IconInfo>();
  193. Hashtable systemIcons = Constants.SYSTEM_ICONS;
  194. IconInfo iconInfo;
  195. foreach (object key in systemIcons.Keys)
  196. {
  197. string keyStr = key.ToString();
  198. iconInfo = new IconInfo
  199. {
  200. Name_NoWrite = systemIcons[key].ToString()
  201. };
  202. iconInfo.BitmapImage_NoWrite = new BitmapImage(
  203. new Uri("pack://application:,,,/GeekDesk;component/Resource/Image/SystemIcon/" + keyStr + ".png"
  204. , UriKind.RelativeOrAbsolute));
  205. iconInfo.StartArg = keyStr;
  206. iconInfo.Content_NoWrite = iconInfo.Name_NoWrite;
  207. iconInfos.Add(iconInfo);
  208. }
  209. return iconInfos;
  210. }
  211. public class SystemItemViewModel : INotifyPropertyChanged
  212. {
  213. private List<IconInfo> iconInfos;
  214. private AppConfig appConfig;
  215. public SystemItemViewModel()
  216. {
  217. this.AppConfig = MainWindow.appData.AppConfig;
  218. }
  219. public AppConfig AppConfig
  220. {
  221. get
  222. {
  223. return appConfig;
  224. }
  225. set
  226. {
  227. appConfig = value;
  228. OnPropertyChanged("AppConfig");
  229. }
  230. }
  231. public List<IconInfo> IconInfos
  232. {
  233. get
  234. {
  235. return iconInfos;
  236. }
  237. set
  238. {
  239. iconInfos = value;
  240. OnPropertyChanged("IconInfos");
  241. }
  242. }
  243. public event PropertyChangedEventHandler PropertyChanged;
  244. private void OnPropertyChanged(string propertyName)
  245. {
  246. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  247. }
  248. }
  249. private static System.Windows.Window window = null;
  250. public static void Show()
  251. {
  252. if (window == null || !window.Activate())
  253. {
  254. window = new SystemItemWindow();
  255. }
  256. window.Show();
  257. Keyboard.Focus(window);
  258. ShowWindowFollowMouse.Show(window, MousePosition.LEFT_CENTER, 0, 0, false);
  259. }
  260. public void OnKeyDown(object sender, KeyEventArgs e)
  261. {
  262. if (e.Key == Key.Escape)
  263. {
  264. this.DataContext = null;
  265. this.Close();
  266. }
  267. }
  268. }
  269. }