CommonCode.cs 12 KB

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