CommonCode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Other;
  3. using GeekDesk.ViewModel;
  4. using HandyControl.Data;
  5. using Microsoft.Win32;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.IO;
  11. using System.Runtime.InteropServices;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using System.Windows;
  14. using System.Windows.Media.Imaging;
  15. using static GeekDesk.Control.Other.GlobalMsgNotification;
  16. /// <summary>
  17. /// 提取一些代码
  18. /// </summary>
  19. namespace GeekDesk.Util
  20. {
  21. class CommonCode
  22. {
  23. /// <summary>
  24. /// 获取app 数据
  25. /// </summary>
  26. /// <returns></returns>
  27. public static AppData GetAppDataByFile()
  28. {
  29. AppData appData;
  30. if (!File.Exists(Constants.DATA_FILE_PATH))
  31. {
  32. using (FileStream fs = File.Create(Constants.DATA_FILE_PATH)) { }
  33. appData = new AppData();
  34. SaveAppData(appData, Constants.DATA_FILE_PATH);
  35. }
  36. else
  37. {
  38. try
  39. {
  40. using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Open))
  41. {
  42. BinaryFormatter bf = new BinaryFormatter();
  43. appData = bf.Deserialize(fs) as AppData;
  44. }
  45. }
  46. catch
  47. {
  48. if (File.Exists(Constants.DATA_FILE_BAK_PATH))
  49. {
  50. try
  51. {
  52. using (FileStream fs = new FileStream(Constants.DATA_FILE_BAK_PATH, FileMode.Open))
  53. {
  54. BinaryFormatter bf = new BinaryFormatter();
  55. appData = bf.Deserialize(fs) as AppData;
  56. }
  57. DialogMsg msg = new DialogMsg();
  58. msg.msg = "不幸的是, GeekDesk当前的数据文件已经损坏, " +
  59. "现在已经启用系统自动备份的数据\n\n" +
  60. "如果你有较新的备份, " +
  61. "请退出GeekDesk, " +
  62. "将备份文件重命名为:Data, " +
  63. "然后将Data覆盖到GeekDesk的根目录即可\n\n" +
  64. "系统上次备份时间: \n" + appData.AppConfig.SysBakTime +
  65. "\n\n如果当前数据就是你想要的数据, 那么请不用管它";
  66. GlobalMsgNotification gm = new GlobalMsgNotification(msg);
  67. HandyControl.Controls.Notification ntf = HandyControl.Controls.Notification.Show(gm, ShowAnimation.Fade, true);
  68. gm.ntf = ntf;
  69. } catch
  70. {
  71. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  72. Application.Current.Shutdown();
  73. return null;
  74. }
  75. } else
  76. {
  77. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  78. Application.Current.Shutdown();
  79. return null;
  80. }
  81. }
  82. }
  83. return appData;
  84. }
  85. /// <summary>
  86. /// 保存app 数据
  87. /// </summary>
  88. /// <param name="appData"></param>
  89. public static void SaveAppData(AppData appData, string filePath)
  90. {
  91. appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  92. if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
  93. {
  94. Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
  95. }
  96. using (FileStream fs = new FileStream(filePath, FileMode.Create))
  97. {
  98. BinaryFormatter bf = new BinaryFormatter();
  99. bf.Serialize(fs, appData);
  100. }
  101. }
  102. public static void BakAppData()
  103. {
  104. SaveFileDialog sfd = new SaveFileDialog
  105. {
  106. Title = "备份文件",
  107. Filter = "bak文件(*.bak)|*.bak",
  108. FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
  109. };
  110. if (sfd.ShowDialog() == true)
  111. {
  112. using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
  113. {
  114. BinaryFormatter bf = new BinaryFormatter();
  115. bf.Serialize(fs, MainWindow.appData);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
  121. /// </summary>
  122. /// <returns></returns>
  123. public static bool IsPrimaryFullScreen()
  124. {
  125. RECT rect = new RECT();
  126. GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
  127. int windowHeight = rect.bottom - rect.top;
  128. int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
  129. if (windowHeight >= screenHeight)
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// 根据路径获取文件图标等信息
  137. /// </summary>
  138. /// <param name="path"></param>
  139. /// <returns></returns>
  140. public static IconInfo GetIconInfoByPath(string path)
  141. {
  142. string tempPath = path;
  143. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  144. //string ext = "";
  145. //if (!ImageUtil.IsSystemItem(path))
  146. //{
  147. // ext = System.IO.Path.GetExtension(path).ToLower();
  148. //}
  149. string iconPath;
  150. //if (".lnk".Equals(ext))
  151. //{
  152. string targetPath = FileUtil.GetTargetPathByLnk(path);
  153. iconPath = FileUtil.GetIconPathByLnk(path);
  154. if (targetPath != null)
  155. {
  156. path = targetPath;
  157. }
  158. //}
  159. if (StringUtil.IsEmpty(iconPath))
  160. {
  161. iconPath = path;
  162. }
  163. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  164. IconInfo iconInfo = new IconInfo
  165. {
  166. Path_NoWrite = path,
  167. LnkPath_NoWrite = tempPath,
  168. BitmapImage_NoWrite = bi,
  169. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  170. };
  171. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  172. iconInfo.Name_NoWrite = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  173. if (StringUtil.IsEmpty(iconInfo.Name))
  174. {
  175. iconInfo.Name_NoWrite = path;
  176. }
  177. iconInfo.RelativePath_NoWrite = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
  178. return iconInfo;
  179. }
  180. public static IconInfo GetIconInfoByPath_NoWrite(string path)
  181. {
  182. string tempPath = path;
  183. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  184. string ext = "";
  185. if (!ImageUtil.IsSystemItem(path))
  186. {
  187. ext = System.IO.Path.GetExtension(path).ToLower();
  188. }
  189. string iconPath = null;
  190. if (".lnk".Equals(ext))
  191. {
  192. string targetPath = FileUtil.GetTargetPathByLnk(path);
  193. iconPath = FileUtil.GetIconPathByLnk(path);
  194. if (targetPath != null)
  195. {
  196. path = targetPath;
  197. }
  198. }
  199. if (StringUtil.IsEmpty(iconPath))
  200. {
  201. iconPath = path;
  202. }
  203. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  204. IconInfo iconInfo = new IconInfo
  205. {
  206. Path_NoWrite = path,
  207. LnkPath_NoWrite = tempPath,
  208. BitmapImage_NoWrite = bi,
  209. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  210. };
  211. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  212. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  213. if (StringUtil.IsEmpty(iconInfo.Name))
  214. {
  215. iconInfo.Name_NoWrite = path;
  216. }
  217. return iconInfo;
  218. }
  219. [StructLayout(LayoutKind.Sequential)]
  220. private struct RECT
  221. {
  222. public int left;
  223. public int top;
  224. public int right;
  225. public int bottom;
  226. }
  227. [DllImport("user32.dll")]
  228. private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
  229. [DllImport("user32.dll")]
  230. private static extern IntPtr GetForegroundWindow();
  231. /// <summary>
  232. /// 排序图标
  233. /// </summary>
  234. public static void SortIconList()
  235. {
  236. if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM)
  237. {
  238. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  239. //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
  240. List<IconInfo> list;
  241. foreach (MenuInfo menuInfo in menuList)
  242. {
  243. list = new List<IconInfo>(menuInfo.IconList);
  244. switch (MainWindow.appData.AppConfig.IconSortType)
  245. {
  246. case SortType.COUNT_UP:
  247. list.Sort((x, y) => x.Count.CompareTo(y.Count));
  248. break;
  249. case SortType.COUNT_LOW:
  250. list.Sort((x, y) => y.Count.CompareTo(x.Count));
  251. break;
  252. case SortType.NAME_UP:
  253. list.Sort((x, y) => x.Name.CompareTo(y.Name));
  254. break;
  255. case SortType.NAME_LOW:
  256. list.Sort((x, y) => y.Name.CompareTo(x.Name));
  257. break;
  258. }
  259. menuInfo.IconList = new ObservableCollection<IconInfo>(list);
  260. }
  261. MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
  262. }
  263. }
  264. /// <summary>
  265. /// 判断鼠标是否在窗口内
  266. /// </summary>
  267. /// <param name="window"></param>
  268. /// <returns></returns>
  269. public static bool MouseInWindow(Window window)
  270. {
  271. double windowHeight = window.Height;
  272. double windowWidth = window.Width;
  273. double windowTop = window.Top;
  274. double windowLeft = window.Left;
  275. //获取鼠标位置
  276. System.Windows.Point p = MouseUtil.GetMousePosition();
  277. double mouseX = p.X;
  278. double mouseY = p.Y;
  279. //鼠标不在窗口上
  280. if (mouseX < windowLeft || mouseX > windowLeft + windowWidth
  281. || mouseY < windowTop || mouseY > windowTop + windowHeight)
  282. {
  283. return false;
  284. }
  285. return true;
  286. }
  287. }
  288. }