1
0

CommonCode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. private readonly static object _MyLock = new object();
  86. /// <summary>
  87. /// 保存app 数据
  88. /// </summary>
  89. /// <param name="appData"></param>
  90. public static void SaveAppData(AppData appData, string filePath)
  91. {
  92. lock (_MyLock)
  93. {
  94. if (filePath.Equals(Constants.DATA_FILE_BAK_PATH))
  95. {
  96. appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  97. }
  98. if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
  99. {
  100. Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
  101. }
  102. using (FileStream fs = new FileStream(filePath, FileMode.Create))
  103. {
  104. BinaryFormatter bf = new BinaryFormatter();
  105. bf.Serialize(fs, appData);
  106. }
  107. }
  108. }
  109. public static void SavePassword(string password)
  110. {
  111. using (StreamWriter sw = new StreamWriter(Constants.PW_FILE_BAK_PATH))
  112. {
  113. sw.Write(password);
  114. }
  115. }
  116. public static void BakAppData()
  117. {
  118. SaveFileDialog sfd = new SaveFileDialog
  119. {
  120. Title = "备份文件",
  121. Filter = "bak文件(*.bak)|*.bak",
  122. FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
  123. };
  124. if (sfd.ShowDialog() == true)
  125. {
  126. using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
  127. {
  128. BinaryFormatter bf = new BinaryFormatter();
  129. bf.Serialize(fs, MainWindow.appData);
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
  135. /// </summary>
  136. /// <returns></returns>
  137. public static bool IsPrimaryFullScreen()
  138. {
  139. RECT rect = new RECT();
  140. GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
  141. int windowHeight = rect.bottom - rect.top;
  142. int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
  143. if (windowHeight >= screenHeight)
  144. {
  145. return true;
  146. }
  147. return false;
  148. }
  149. /// <summary>
  150. /// 根据路径获取文件图标等信息
  151. /// </summary>
  152. /// <param name="path"></param>
  153. /// <returns></returns>
  154. public static IconInfo GetIconInfoByPath(string path)
  155. {
  156. string tempPath = path;
  157. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  158. //string ext = "";
  159. //if (!ImageUtil.IsSystemItem(path))
  160. //{
  161. // ext = System.IO.Path.GetExtension(path).ToLower();
  162. //}
  163. string iconPath;
  164. //if (".lnk".Equals(ext))
  165. //{
  166. string targetPath = FileUtil.GetTargetPathByLnk(path);
  167. iconPath = FileUtil.GetIconPathByLnk(path);
  168. if (targetPath != null)
  169. {
  170. path = targetPath;
  171. }
  172. //}
  173. if (StringUtil.IsEmpty(iconPath))
  174. {
  175. iconPath = path;
  176. }
  177. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  178. IconInfo iconInfo = new IconInfo
  179. {
  180. Path_NoWrite = path,
  181. LnkPath_NoWrite = tempPath,
  182. BitmapImage_NoWrite = bi,
  183. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  184. };
  185. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  186. iconInfo.Name_NoWrite = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  187. if (StringUtil.IsEmpty(iconInfo.Name))
  188. {
  189. iconInfo.Name_NoWrite = path;
  190. }
  191. string relativePath = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
  192. if (!string.IsNullOrEmpty(relativePath) && !relativePath.Equals(iconInfo.Path))
  193. {
  194. iconInfo.RelativePath_NoWrite = relativePath;
  195. }
  196. return iconInfo;
  197. }
  198. public static IconInfo GetIconInfoByPath_NoWrite(string path)
  199. {
  200. string tempPath = path;
  201. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  202. string ext = "";
  203. if (!ImageUtil.IsSystemItem(path))
  204. {
  205. ext = System.IO.Path.GetExtension(path).ToLower();
  206. }
  207. string iconPath = null;
  208. if (".lnk".Equals(ext))
  209. {
  210. string targetPath = FileUtil.GetTargetPathByLnk(path);
  211. iconPath = FileUtil.GetIconPathByLnk(path);
  212. if (targetPath != null)
  213. {
  214. path = targetPath;
  215. }
  216. }
  217. if (StringUtil.IsEmpty(iconPath))
  218. {
  219. iconPath = path;
  220. }
  221. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  222. IconInfo iconInfo = new IconInfo
  223. {
  224. Path_NoWrite = path,
  225. LnkPath_NoWrite = tempPath,
  226. BitmapImage_NoWrite = bi,
  227. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  228. };
  229. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  230. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  231. if (StringUtil.IsEmpty(iconInfo.Name))
  232. {
  233. iconInfo.Name_NoWrite = path;
  234. }
  235. return iconInfo;
  236. }
  237. [StructLayout(LayoutKind.Sequential)]
  238. private struct RECT
  239. {
  240. public int left;
  241. public int top;
  242. public int right;
  243. public int bottom;
  244. }
  245. [DllImport("user32.dll")]
  246. private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
  247. [DllImport("user32.dll")]
  248. private static extern IntPtr GetForegroundWindow();
  249. /// <summary>
  250. /// 排序图标
  251. /// </summary>
  252. public static void SortIconList()
  253. {
  254. if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM)
  255. {
  256. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  257. //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
  258. List<IconInfo> list;
  259. foreach (MenuInfo menuInfo in menuList)
  260. {
  261. list = new List<IconInfo>(menuInfo.IconList);
  262. switch (MainWindow.appData.AppConfig.IconSortType)
  263. {
  264. case SortType.COUNT_UP:
  265. list.Sort((x, y) => x.Count.CompareTo(y.Count));
  266. break;
  267. case SortType.COUNT_LOW:
  268. list.Sort((x, y) => y.Count.CompareTo(x.Count));
  269. break;
  270. case SortType.NAME_UP:
  271. list.Sort((x, y) => x.Name.CompareTo(y.Name));
  272. break;
  273. case SortType.NAME_LOW:
  274. list.Sort((x, y) => y.Name.CompareTo(x.Name));
  275. break;
  276. }
  277. menuInfo.IconList = new ObservableCollection<IconInfo>(list);
  278. }
  279. MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
  280. }
  281. }
  282. /// <summary>
  283. /// 判断鼠标是否在窗口内
  284. /// </summary>
  285. /// <param name="window"></param>
  286. /// <returns></returns>
  287. public static bool MouseInWindow(Window window)
  288. {
  289. double windowHeight = window.Height;
  290. double windowWidth = window.Width;
  291. double windowTop = window.Top;
  292. double windowLeft = window.Left;
  293. //获取鼠标位置
  294. System.Windows.Point p = MouseUtil.GetMousePosition();
  295. double mouseX = p.X;
  296. double mouseY = p.Y;
  297. //鼠标不在窗口上
  298. if (mouseX < windowLeft || mouseX > windowLeft + windowWidth
  299. || mouseY < windowTop || mouseY > windowTop + windowHeight)
  300. {
  301. return false;
  302. }
  303. return true;
  304. }
  305. }
  306. }