Files
GeekDesk/Util/CommonCode.cs

100 lines
2.5 KiB
C#
Raw Normal View History

2021-04-13 15:26:19 +08:00
using GeekDesk.Constant;
using GeekDesk.ViewModel;
2021-07-29 17:04:36 +08:00
using System;
2021-04-12 13:46:05 +08:00
using System.IO;
using System.Runtime.InteropServices;
2021-04-12 13:46:05 +08:00
using System.Runtime.Serialization.Formatters.Binary;
2021-07-29 17:04:36 +08:00
using System.Windows;
2021-04-12 13:46:05 +08:00
/// <summary>
/// 提取一些代码
/// </summary>
namespace GeekDesk.Util
{
class CommonCode
{
2021-04-13 15:26:19 +08:00
2021-04-12 13:46:05 +08:00
/// <summary>
2021-04-13 15:26:19 +08:00
/// 获取app 数据
2021-04-12 13:46:05 +08:00
/// </summary>
/// <returns></returns>
2021-05-08 17:27:41 +08:00
public static AppData GetAppDataByFile()
2021-04-12 13:46:05 +08:00
{
2021-04-13 15:26:19 +08:00
AppData appData;
2021-05-26 17:19:04 +08:00
if (!File.Exists(Constants.DATA_FILE_PATH))
2021-04-12 13:46:05 +08:00
{
2021-05-26 17:19:04 +08:00
using (FileStream fs = File.Create(Constants.DATA_FILE_PATH)) { }
2021-04-13 15:26:19 +08:00
appData = new AppData();
SaveAppData(appData);
2021-04-12 13:46:05 +08:00
}
else
{
2021-05-26 17:19:04 +08:00
using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Open))
2021-04-12 13:46:05 +08:00
{
BinaryFormatter bf = new BinaryFormatter();
2021-04-13 15:26:19 +08:00
appData = bf.Deserialize(fs) as AppData;
2021-04-12 13:46:05 +08:00
}
}
2021-04-13 15:26:19 +08:00
return appData;
2021-04-12 13:46:05 +08:00
}
/// <summary>
2021-04-13 15:26:19 +08:00
/// 保存app 数据
2021-04-12 13:46:05 +08:00
/// </summary>
2021-04-13 15:26:19 +08:00
/// <param name="appData"></param>
public static void SaveAppData(AppData appData)
2021-04-12 13:46:05 +08:00
{
2021-04-13 15:26:19 +08:00
2021-05-26 17:19:04 +08:00
using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Create))
2021-04-12 13:46:05 +08:00
{
BinaryFormatter bf = new BinaryFormatter();
2021-04-13 15:26:19 +08:00
bf.Serialize(fs, appData);
2021-04-12 13:46:05 +08:00
}
}
2021-04-13 15:26:19 +08:00
/// <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;
}
[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();
2021-04-13 15:26:19 +08:00
2021-04-12 13:46:05 +08:00
}
}