CommonCode.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using GeekDesk.Constant;
  2. using GeekDesk.ViewModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Windows;
  10. using System.Windows.Media.Imaging;
  11. /// <summary>
  12. /// 提取一些代码
  13. /// </summary>
  14. namespace GeekDesk.Util
  15. {
  16. class CommonCode
  17. {
  18. /// <summary>
  19. /// 获取app 数据
  20. /// </summary>
  21. /// <returns></returns>
  22. public static AppData GetAppDataByFile()
  23. {
  24. AppData appData;
  25. if (!File.Exists(Constants.DATA_FILE_PATH))
  26. {
  27. using (FileStream fs = File.Create(Constants.DATA_FILE_PATH)) { }
  28. appData = new AppData();
  29. SaveAppData(appData);
  30. }
  31. else
  32. {
  33. using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Open))
  34. {
  35. BinaryFormatter bf = new BinaryFormatter();
  36. appData = bf.Deserialize(fs) as AppData;
  37. }
  38. }
  39. return appData;
  40. }
  41. /// <summary>
  42. /// 保存app 数据
  43. /// </summary>
  44. /// <param name="appData"></param>
  45. public static void SaveAppData(AppData appData)
  46. {
  47. using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Create))
  48. {
  49. BinaryFormatter bf = new BinaryFormatter();
  50. bf.Serialize(fs, appData);
  51. }
  52. }
  53. /// <summary>
  54. /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
  55. /// </summary>
  56. /// <returns></returns>
  57. public static bool IsPrimaryFullScreen()
  58. {
  59. RECT rect = new RECT();
  60. GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
  61. int windowHeight = rect.bottom - rect.top;
  62. int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
  63. if (windowHeight >= screenHeight)
  64. {
  65. return true;
  66. }
  67. return false;
  68. }
  69. /// <summary>
  70. /// 根据路径获取文件图标等信息
  71. /// </summary>
  72. /// <param name="path"></param>
  73. /// <returns></returns>
  74. public static IconInfo GetIconInfoByPath(string path)
  75. {
  76. string tempPath = path;
  77. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  78. //string ext = "";
  79. //if (!ImageUtil.IsSystemItem(path))
  80. //{
  81. // ext = System.IO.Path.GetExtension(path).ToLower();
  82. //}
  83. string iconPath = null;
  84. //if (".lnk".Equals(ext))
  85. //{
  86. string targetPath = FileUtil.GetTargetPathByLnk(path);
  87. iconPath = FileUtil.GetIconPathByLnk(path);
  88. if (targetPath != null)
  89. {
  90. path = targetPath;
  91. }
  92. //}
  93. if (StringUtil.IsEmpty(iconPath))
  94. {
  95. iconPath = path;
  96. }
  97. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  98. IconInfo iconInfo = new IconInfo
  99. {
  100. Path = path,
  101. LnkPath = tempPath,
  102. BitmapImage = bi,
  103. StartArg = FileUtil.GetArgByLnk(tempPath)
  104. };
  105. iconInfo.DefaultImage = iconInfo.ImageByteArr;
  106. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  107. if (StringUtil.IsEmpty(iconInfo.Name))
  108. {
  109. iconInfo.Name = path;
  110. }
  111. return iconInfo;
  112. }
  113. public static IconInfo GetIconInfoByPath_NoWrite(string path)
  114. {
  115. string tempPath = path;
  116. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  117. string ext = "";
  118. if (!ImageUtil.IsSystemItem(path))
  119. {
  120. ext = System.IO.Path.GetExtension(path).ToLower();
  121. }
  122. string iconPath = null;
  123. if (".lnk".Equals(ext))
  124. {
  125. string targetPath = FileUtil.GetTargetPathByLnk(path);
  126. iconPath = FileUtil.GetIconPathByLnk(path);
  127. if (targetPath != null)
  128. {
  129. path = targetPath;
  130. }
  131. }
  132. if (StringUtil.IsEmpty(iconPath))
  133. {
  134. iconPath = path;
  135. }
  136. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  137. IconInfo iconInfo = new IconInfo
  138. {
  139. Path_NoWrite = path,
  140. LnkPath_NoWrite = tempPath,
  141. BitmapImage_NoWrite = bi,
  142. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  143. };
  144. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  145. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  146. if (StringUtil.IsEmpty(iconInfo.Name))
  147. {
  148. iconInfo.Name_NoWrite = path;
  149. }
  150. return iconInfo;
  151. }
  152. [StructLayout(LayoutKind.Sequential)]
  153. private struct RECT
  154. {
  155. public int left;
  156. public int top;
  157. public int right;
  158. public int bottom;
  159. }
  160. [DllImport("user32.dll")]
  161. private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
  162. [DllImport("user32.dll")]
  163. private static extern IntPtr GetForegroundWindow();
  164. /// <summary>
  165. /// 排序图标
  166. /// </summary>
  167. public static void SortIconList()
  168. {
  169. if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM)
  170. {
  171. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  172. //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
  173. List<IconInfo> list;
  174. foreach (MenuInfo menuInfo in menuList)
  175. {
  176. list = new List<IconInfo>(menuInfo.IconList);
  177. switch (MainWindow.appData.AppConfig.IconSortType)
  178. {
  179. case SortType.COUNT_UP:
  180. list.Sort((x, y) => x.Count.CompareTo(y.Count));
  181. break;
  182. case SortType.COUNT_LOW:
  183. list.Sort((x, y) => y.Count.CompareTo(x.Count));
  184. break;
  185. case SortType.NAME_UP:
  186. list.Sort((x, y) => x.Name.CompareTo(y.Name));
  187. break;
  188. case SortType.NAME_LOW:
  189. list.Sort((x, y) => y.Name.CompareTo(x.Name));
  190. break;
  191. }
  192. menuInfo.IconList = new ObservableCollection<IconInfo>(list);
  193. }
  194. MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
  195. }
  196. }
  197. }
  198. }