Skip to content

Commit

Permalink
Add Wallpaper Engine Support and Fix Startup
Browse files Browse the repository at this point in the history
Add:
1. Wallpaper Engine Support
2. User information collection
3. Improve related pages
Fix:
1. Startup
2. System version control
  • Loading branch information
6get-xiaofan committed May 12, 2023
1 parent 1431a86 commit 7cb9792
Show file tree
Hide file tree
Showing 22 changed files with 442 additions and 253 deletions.
6 changes: 6 additions & 0 deletions DarkMode_2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@
<Compile Include="Models\MessageBox.cs" />
<Compile Include="Models\RedRawWindow.cs" />
<Compile Include="Models\ReplaceWallpaper.cs" />
<Compile Include="Models\StartupHelper.cs" />
<Compile Include="Models\StartUrl.cs" />
<Compile Include="Models\SwitchMode.cs" />
<Compile Include="Models\SystemHotKey.cs" />
<Compile Include="Models\TimeConverter.cs" />
<Compile Include="Models\Update.cs" />
<Compile Include="Models\VersionControl.cs" />
<Compile Include="Models\WallpaperChanger.cs" />
<Compile Include="Models\WindowsVersionHelper.cs" />
<Compile Include="Services\ApplicationHostService.cs" />
<Compile Include="Services\Contracts\ITestWindowService.cs" />
<Compile Include="Services\PageService.cs" />
Expand Down Expand Up @@ -283,6 +286,9 @@
<PackageReference Include="OpenHardwareMonitor">
<Version>0.9.6</Version>
</PackageReference>
<PackageReference Include="TaskScheduler">
<Version>2.10.1</Version>
</PackageReference>
<PackageReference Include="WPF-UI">
<Version>2.0.3</Version>
</PackageReference>
Expand Down
79 changes: 29 additions & 50 deletions Models/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,44 @@
using Microsoft.Win32;
using System;
using System;
using System.Diagnostics;
using System.IO;

namespace DarkMode_2.Models;

public class CommandLine
{
public static bool CallCommandLine(string hand, string command, string type)
private string commandOutput = "";

public string CommandOutput
{
get { return commandOutput; }
}

public int ExitCode { get; private set; }

public CommandLine(string command)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\DarkMode2", true);
if (getNowWallpaperPath(hand) == key.GetValue(type).ToString())
var processInfo = new ProcessStartInfo("cmd.exe", $"/c \"{command}\"")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
};

using (var process = new Process())
{
string zhilin = "\"" + hand + "\"" + " -control openWallpaper -file " + "\"" + command + "\"";
Process process = new Process()
{
StartInfo =
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
process.StartInfo = processInfo;
process.OutputDataReceived += Process_OutputDataReceived;
process.Start();
process.StandardInput.WriteLine(zhilin);
string res = process.StandardOutput.ReadToEnd();
process.StandardInput.WriteLine("exit");
process.StandardInput.AutoFlush = true;
process.BeginOutputReadLine();
process.WaitForExit();
return true;
ExitCode = process.ExitCode;
}
return false;
}


public static string getNowWallpaperPath(string hand)
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
//string zhilin = "\"" + hand + "\"" + " -control getWallpaper";
////执行cmd命令
//Process CmdProcess = new Process();
//CmdProcess.StartInfo.FileName = "cmd.exe";
//CmdProcess.StartInfo.CreateNoWindow = true; // 不创建新窗口
//CmdProcess.StartInfo.UseShellExecute = false; //不启用shell启动进程
//CmdProcess.StartInfo.RedirectStandardInput = true; // 重定向输入
//CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出
//CmdProcess.StartInfo.RedirectStandardError = true; // 重定向错误输出
//CmdProcess.StartInfo.Arguments = zhilin;//“/C”表示执行完命令后马上退出
//CmdProcess.StartInfo.RedirectStandardOutput = true;
//CmdProcess.Start();//执行

//CmdProcess.StandardOutput.ReadToEnd();//获取返回值
//StreamReader sr = CmdProcess.StandardOutput;//获取返回值
//CmdProcess.WaitForExit();//等待程序执行完退出进程

//CmdProcess.Close();//结束

//string line = sr.ReadLine();
//return line;
return "";
if (!string.IsNullOrWhiteSpace(e.Data))
{
commandOutput += e.Data + "\n";
}
}
}
}
52 changes: 33 additions & 19 deletions Models/GetGPULoad.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
using LibreHardwareMonitor.Hardware;
using System.Collections.Generic;
using System.Diagnostics;
using System;
using System.Management;
using System.Threading;
using System;
using System.Linq;
using System.Timers;
//using OpenHardwareMonitor.Hardware;

namespace DarkMode_2.Models;

public class GetGPULoad
namespace DarkMode_2.Models
{
/// <summary>
/// 获取利用率
/// </summary>
/// <param name="lstCounters">计数集合</param>
/// <returns>利用率</returns>
//public static float GetUsage()
//{
//PerformanceCounter counter = new PerformanceCounter("Video Card", "GPU Busy", string.Empty);
//float gpuUsage = counter.NextValue();
//return gpuUsage;
//}
class Program
{
static int threshold = 80; // 注册表中设置的阈值(占用率百分比)
static System.Timers.Timer timer = new System.Timers.Timer(1000); // 每1秒执行一次
//static void Main(string[] args)
//{
// timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// timer.Start();

// Console.WriteLine("Press any key to exit...");
// Console.ReadKey();
//}

static void OnTimedEvent(object source, ElapsedEventArgs e)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController");
foreach (var item in searcher.Get())
{
int usage = Convert.ToInt32(item["AdapterRAM"]) * 100 / Convert.ToInt32(item["AdapterCompatibility"]);
Console.WriteLine("GPU Usage: " + usage + "%");

// 如果达到阈值,执行一些代码
if (usage >= threshold)
{
Console.WriteLine("GPU is heavily utilized. Taking action...");
// TODO: 执行你需要的代码
}
}
}
}
}
13 changes: 11 additions & 2 deletions Models/MessageBox.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
namespace DarkMode_2.Models;
using System.Windows;

namespace DarkMode_2.Models;

public class MessageBox
{

private static string Content;
public static void OpenMessageBox(string title, string content)
{
Content = content;
var messageBox = new Wpf.Ui.Controls.MessageBox
{
ButtonRightName = "关闭弹窗",
ButtonRightName = "关闭",

ButtonLeftName = "确定"
};
Expand All @@ -15,12 +20,16 @@ public static void OpenMessageBox(string title, string content)

messageBox.ButtonLeftClick += MessageBox_LeftButtonClick;

messageBox.ButtonLeftName = "复制";
messageBox.ButtonRightName = "关闭";

messageBox.Show(title, content);
}

private static void MessageBox_RightButtonClick(object sender, System.Windows.RoutedEventArgs e)
{
(sender as Wpf.Ui.Controls.MessageBox)?.Close();
Clipboard.SetText(Content);
}
private static void MessageBox_LeftButtonClick(object sender, System.Windows.RoutedEventArgs e)
{
Expand Down
48 changes: 48 additions & 0 deletions Models/StartupHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Win32.TaskScheduler;
using System.Linq;

namespace DarkMode_2.Models
{
public static class StartupHelper
{
private const string TaskName = "DarkMode2";
private const string TaskDescription = "DarkMode2自启动任务计划";
private static readonly string TaskPath = $@"\{TaskName}";

public static void Enable()
{
using (var taskService = new TaskService())
{
var taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Description = TaskDescription;

var bootTrigger = new BootTrigger();
taskDefinition.Triggers.Add(bootTrigger);

var action = new ExecAction(System.Reflection.Assembly.GetEntryAssembly().Location);
taskDefinition.Actions.Add(action);

taskService.RootFolder.RegisterTaskDefinition(TaskPath, taskDefinition);
}
}

public static void Disable()
{
using (var taskService = new TaskService())
{
taskService.RootFolder.DeleteTask(TaskPath, false);
}
}

public static bool IsEnabled()
{
using (var taskService = new TaskService())
{
var task = taskService.RootFolder.GetTasks().FirstOrDefault(t => t.Path.EndsWith(TaskPath));
return task != null;
}
}
}
}


14 changes: 4 additions & 10 deletions Models/SwitchMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ public static void switchMode(string mode)
{
if (key.GetValue("WeInstallPath").ToString() != "")
{
CommandLine.CallCommandLine(key.GetValue("WeInstallPath").ToString(), key.GetValue("WeDark").ToString(), "WeDark");
WallpaperChanger wallpaperChanger = new(key.GetValue("WeInstallPath").ToString(), key.GetValue("WeDark").ToString());
wallpaperChanger.openWallpaper();

}
else
{
MessageBox.OpenMessageBox("错误", "未找到Wallpaper Engine路径。");
}
}
}
else if (mode == "dark" && DetermineSystemColorMode.GetSysState() == "light") //深色
Expand All @@ -56,11 +53,8 @@ public static void switchMode(string mode)
{
if (key.GetValue("WeInstallPath").ToString() != "")
{
CommandLine.CallCommandLine(key.GetValue("WeInstallPath").ToString(), key.GetValue("WeLight").ToString(), "WeLight");
}
else
{
MessageBox.OpenMessageBox("错误", "未找到Wallpaper Engine路径。");
WallpaperChanger wallpaperChanger = new(key.GetValue("WeInstallPath").ToString(), key.GetValue("WeLight").ToString());
wallpaperChanger.openWallpaper();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Models/VersionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public static string Channel()
}
public static string Version()
{
string version = "2.0.7";
string version = "2.0.8";
return version;
}
public static string InternalVersion()
{
string internalVersion = "20230510";
string internalVersion = "20230512";
return internalVersion;
}
}
32 changes: 32 additions & 0 deletions Models/WallpaperChanger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace DarkMode_2.Models
{
public class WallpaperChanger
{
private string _wallpaperEnginePath;
private string _wallpaperFilePath;

public WallpaperChanger(string wallpapernEginePath, string wallpaperFilePath)
{
_wallpaperEnginePath = wallpapernEginePath;
_wallpaperFilePath = wallpaperFilePath;
}
public void openWallpaper()
{
string command = $"{_wallpaperEnginePath} -control openWallpaper -file {_wallpaperFilePath}";
Console.WriteLine("小子看这里:"+command);
CommandLine run = new CommandLine(command);
}

// Wallpaper Engine官方对于这个命令存在bug
public string getNowWallpaper()
{
string command = $"{_wallpaperEnginePath} -control getWallpaper";
CommandLine run = new CommandLine(command);
return run.CommandOutput.Trim();
}


}
}
Loading

0 comments on commit 7cb9792

Please sign in to comment.