From 1abef31437e0c5d533e941c31bc6c4378a87cc4f Mon Sep 17 00:00:00 2001 From: GHXX Date: Fri, 29 May 2026 10:48:30 +0200 Subject: [PATCH] make it work for just one screen --- McsrGraphImageDownloader.csproj | 2 +- Program.cs | 46 ++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/McsrGraphImageDownloader.csproj b/McsrGraphImageDownloader.csproj index 07adfbe..8f4f2a5 100644 --- a/McsrGraphImageDownloader.csproj +++ b/McsrGraphImageDownloader.csproj @@ -5,7 +5,7 @@ net9.0 enable enable - true + false true diff --git a/Program.cs b/Program.cs index 5df1ab7..0aade04 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,6 @@ -using Svg; +#define SHOW_ON_ONE_SCREEN + +using Svg; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; @@ -6,11 +8,32 @@ using System.Text.Json; namespace McsrGraphImageDownloader; internal class Program { +#if SHOW_ON_ONE_SCREEN + private const int screenToShowItOn = 0; // must be >= 0 and < amount_of_screens, will be clamped if necessary + + [ComImport] + [Guid("B92B56A9-8B55-4E14-9A89-0199BBB6F93B")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IDesktopWallpaper { + void SetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string monitorID, [MarshalAs(UnmanagedType.LPWStr)] string wallpaper); + + [return: MarshalAs(UnmanagedType.LPWStr)] + string GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string monitorID); + + void GetMonitorDevicePathAt(uint monitorIndex, [MarshalAs(UnmanagedType.LPWStr)] out string monitorID); + + uint GetMonitorDevicePathCount(); + + // Remaining methods omitted since not needed + } +#endif + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); - public const int SPI_SETDESKWALLPAPER = 20; + public const int SPI_SETDESKWALLPAPER = 0x14; public const int SPIF_UPDATEINIFILE = 1; public const int SPIF_SENDCHANGE = 2; + private const string outputFilename = "mcsrChart.png"; private static async Task Main(string[] args) { @@ -22,8 +45,25 @@ internal class Program { using (var bitmap = svg.Draw(1920, 1080)) { bitmap.Save(outputFilename); } - SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Path.GetFullPath(outputFilename), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE); +#if SHOW_ON_ONE_SCREEN + Type type = Type.GetTypeFromCLSID(new Guid("C2CF3110-460E-4FC1-B9D0-8A1C0C9CC4BD"))!; + + var wallpaper = (IDesktopWallpaper)Activator.CreateInstance(type)!; + + uint monitorCount = wallpaper.GetMonitorDevicePathCount(); + + Console.WriteLine($"Monitor count: {monitorCount}"); + var monitorIndex = Math.Clamp(screenToShowItOn, 0, monitorCount - 1); + + wallpaper.GetMonitorDevicePathAt(monitorIndex, out string? monitorId); + + wallpaper.SetWallpaper(monitorId, Path.GetFullPath(outputFilename)); + + Console.WriteLine($"Wallpaper set for monitor {monitorIndex} only."); +#else + SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Path.GetFullPath(outputFilename), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE); +#endif await Task.Delay(TimeSpan.FromMinutes(15)); } }