123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using GeekDesk.Constant;
- using GeekDesk.ViewModel;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Windows;
- using System.Windows.Media.Imaging;
- /// <summary>
- /// 提取一些代码
- /// </summary>
- namespace GeekDesk.Util
- {
- class CommonCode
- {
- /// <summary>
- /// 获取app 数据
- /// </summary>
- /// <returns></returns>
- public static AppData GetAppDataByFile()
- {
- AppData appData;
- if (!File.Exists(Constants.DATA_FILE_PATH))
- {
- using (FileStream fs = File.Create(Constants.DATA_FILE_PATH)) { }
- appData = new AppData();
- SaveAppData(appData);
- }
- else
- {
- using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Open))
- {
- BinaryFormatter bf = new BinaryFormatter();
- appData = bf.Deserialize(fs) as AppData;
- }
- }
- return appData;
- }
- /// <summary>
- /// 保存app 数据
- /// </summary>
- /// <param name="appData"></param>
- public static void SaveAppData(AppData appData)
- {
- using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Create))
- {
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(fs, appData);
- }
- }
- /// <summary>
- /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
- /// </summary>
- /// <returns></returns>
- public static bool IsPrimaryFullScreen()
- {
- RECT rect = new RECT();
- GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
- int windowHeight = rect.bottom - rect.top;
- int screenHeight = (int)SystemParameters.PrimaryScreenHeight;
- if (windowHeight >= screenHeight)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 根据路径获取文件图标等信息
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static IconInfo GetIconInfoByPath(string path)
- {
- string tempPath = path;
- //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
- //string ext = "";
- //if (!ImageUtil.IsSystemItem(path))
- //{
- // ext = System.IO.Path.GetExtension(path).ToLower();
- //}
- string iconPath = null;
- //if (".lnk".Equals(ext))
- //{
- string targetPath = FileUtil.GetTargetPathByLnk(path);
- iconPath = FileUtil.GetIconPathByLnk(path);
- if (targetPath != null)
- {
- path = targetPath;
- }
- //}
- if (StringUtil.IsEmpty(iconPath))
- {
- iconPath = path;
- }
- BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
- IconInfo iconInfo = new IconInfo
- {
- Path = path,
- LnkPath = tempPath,
- BitmapImage = bi,
- StartArg = FileUtil.GetArgByLnk(tempPath)
- };
- iconInfo.DefaultImage = iconInfo.ImageByteArr;
- iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
- if (StringUtil.IsEmpty(iconInfo.Name))
- {
- iconInfo.Name = path;
- }
- return iconInfo;
- }
- public static IconInfo GetIconInfoByPath_NoWrite(string path)
- {
- string tempPath = path;
- //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
- string ext = "";
- if (!ImageUtil.IsSystemItem(path))
- {
- ext = System.IO.Path.GetExtension(path).ToLower();
- }
- string iconPath = null;
- if (".lnk".Equals(ext))
- {
- string targetPath = FileUtil.GetTargetPathByLnk(path);
- iconPath = FileUtil.GetIconPathByLnk(path);
- if (targetPath != null)
- {
- path = targetPath;
- }
- }
- if (StringUtil.IsEmpty(iconPath))
- {
- iconPath = path;
- }
- BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
- IconInfo iconInfo = new IconInfo
- {
- Path_NoWrite = path,
- LnkPath_NoWrite = tempPath,
- BitmapImage_NoWrite = bi,
- StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
- };
- iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
- iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
- if (StringUtil.IsEmpty(iconInfo.Name))
- {
- iconInfo.Name_NoWrite = path;
- }
- return iconInfo;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- }
- [DllImport("user32.dll")]
- private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
- [DllImport("user32.dll")]
- private static extern IntPtr GetForegroundWindow();
- /// <summary>
- /// 排序图标
- /// </summary>
- public static void SortIconList()
- {
- if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM)
- {
- ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
- //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
- List<IconInfo> list;
- foreach (MenuInfo menuInfo in menuList)
- {
- list = new List<IconInfo>(menuInfo.IconList);
- switch (MainWindow.appData.AppConfig.IconSortType)
- {
- case SortType.COUNT_UP:
- list.Sort((x, y) => x.Count.CompareTo(y.Count));
- break;
- case SortType.COUNT_LOW:
- list.Sort((x, y) => y.Count.CompareTo(x.Count));
- break;
- case SortType.NAME_UP:
- list.Sort((x, y) => x.Name.CompareTo(y.Name));
- break;
- case SortType.NAME_LOW:
- list.Sort((x, y) => y.Name.CompareTo(x.Name));
- break;
- }
- menuInfo.IconList = new ObservableCollection<IconInfo>(list);
- }
- MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
- }
- }
- }
- }
|