CommonCode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. }
  70. catch
  71. {
  72. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  73. Application.Current.Shutdown();
  74. return null;
  75. }
  76. }
  77. else
  78. {
  79. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  80. Application.Current.Shutdown();
  81. return null;
  82. }
  83. }
  84. }
  85. return appData;
  86. }
  87. private readonly static object _MyLock = new object();
  88. /// <summary>
  89. /// 保存app 数据
  90. /// </summary>
  91. /// <param name="appData"></param>
  92. public static void SaveAppData(AppData appData, string filePath)
  93. {
  94. lock (_MyLock)
  95. {
  96. if (filePath.Equals(Constants.DATA_FILE_BAK_PATH))
  97. {
  98. appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  99. }
  100. if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
  101. {
  102. Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
  103. }
  104. using (FileStream fs = new FileStream(filePath, FileMode.Create))
  105. {
  106. BinaryFormatter bf = new BinaryFormatter();
  107. bf.Serialize(fs, appData);
  108. }
  109. }
  110. }
  111. public static void SavePassword(string password)
  112. {
  113. using (StreamWriter sw = new StreamWriter(Constants.PW_FILE_BAK_PATH))
  114. {
  115. sw.Write(password);
  116. }
  117. }
  118. public static void BakAppData()
  119. {
  120. SaveFileDialog sfd = new SaveFileDialog
  121. {
  122. Title = "备份文件",
  123. Filter = "bak文件(*.bak)|*.bak",
  124. FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
  125. };
  126. if (sfd.ShowDialog() == true)
  127. {
  128. using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
  129. {
  130. BinaryFormatter bf = new BinaryFormatter();
  131. bf.Serialize(fs, MainWindow.appData);
  132. }
  133. }
  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. string relativePath = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
  178. if (!string.IsNullOrEmpty(relativePath) && !relativePath.Equals(iconInfo.Path))
  179. {
  180. iconInfo.RelativePath_NoWrite = relativePath;
  181. }
  182. return iconInfo;
  183. }
  184. public static IconInfo GetIconInfoByPath_NoWrite(string path)
  185. {
  186. string tempPath = path;
  187. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  188. string ext = "";
  189. if (!ImageUtil.IsSystemItem(path))
  190. {
  191. ext = System.IO.Path.GetExtension(path).ToLower();
  192. }
  193. string iconPath = null;
  194. if (".lnk".Equals(ext))
  195. {
  196. string targetPath = FileUtil.GetTargetPathByLnk(path);
  197. iconPath = FileUtil.GetIconPathByLnk(path);
  198. if (targetPath != null)
  199. {
  200. path = targetPath;
  201. }
  202. }
  203. if (StringUtil.IsEmpty(iconPath))
  204. {
  205. iconPath = path;
  206. }
  207. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  208. IconInfo iconInfo = new IconInfo
  209. {
  210. Path_NoWrite = path,
  211. LnkPath_NoWrite = tempPath,
  212. BitmapImage_NoWrite = bi,
  213. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  214. };
  215. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  216. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  217. if (StringUtil.IsEmpty(iconInfo.Name))
  218. {
  219. iconInfo.Name_NoWrite = path;
  220. }
  221. return iconInfo;
  222. }
  223. /// <summary>
  224. /// 排序图标
  225. /// </summary>
  226. public static void SortIconList()
  227. {
  228. if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM)
  229. {
  230. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  231. //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
  232. List<IconInfo> list;
  233. foreach (MenuInfo menuInfo in menuList)
  234. {
  235. list = new List<IconInfo>(menuInfo.IconList);
  236. switch (MainWindow.appData.AppConfig.IconSortType)
  237. {
  238. case SortType.COUNT_UP:
  239. list.Sort((x, y) => x.Count.CompareTo(y.Count));
  240. break;
  241. case SortType.COUNT_LOW:
  242. list.Sort((x, y) => y.Count.CompareTo(x.Count));
  243. break;
  244. case SortType.NAME_UP:
  245. list.Sort((x, y) => x.Name.CompareTo(y.Name));
  246. break;
  247. case SortType.NAME_LOW:
  248. list.Sort((x, y) => y.Name.CompareTo(x.Name));
  249. break;
  250. }
  251. menuInfo.IconList = new ObservableCollection<IconInfo>(list);
  252. }
  253. MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
  254. }
  255. }
  256. /// <summary>
  257. /// 判断鼠标是否在窗口内
  258. /// </summary>
  259. /// <param name="window"></param>
  260. /// <returns></returns>
  261. public static bool MouseInWindow(Window window)
  262. {
  263. double windowHeight = window.Height;
  264. double windowWidth = window.Width;
  265. double windowTop = window.Top;
  266. double windowLeft = window.Left;
  267. //获取鼠标位置
  268. System.Windows.Point p = MouseUtil.GetMousePosition();
  269. double mouseX = p.X;
  270. double mouseY = p.Y;
  271. //鼠标不在窗口上
  272. if (mouseX < windowLeft || mouseX > windowLeft + windowWidth
  273. || mouseY < windowTop || mouseY > windowTop + windowHeight)
  274. {
  275. return false;
  276. }
  277. return true;
  278. }
  279. }
  280. }