Skip to content

Instantly share code, notes, and snippets.

@sensboston
Created February 7, 2024 04:33
Show Gist options
  • Save sensboston/359c7a35677b635d998213cf50250a31 to your computer and use it in GitHub Desktop.
Save sensboston/359c7a35677b635d998213cf50250a31 to your computer and use it in GitHub Desktop.
ChatGPT created :)
// ChatGPT: Here's a C# console application that sets the resolution, scaling, and orientation of each monitor:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
namespace MonitorSettings
{
class Program
{
// Define constants for monitor orientation
private const uint DMDO_DEFAULT = 0;
private const uint DMDO_90 = 1;
private const uint DMDO_180 = 2;
private const uint DMDO_270 = 3;
static void Main(string[] args)
{
// Get the WMI ManagementScope
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\WMI");
// Set the resolution and scaling for each monitor
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM WmiMonitorBasicDisplayParams"));
foreach (ManagementObject queryObj in searcher.Get())
{
uint screenWidth = 1920;
uint screenHeight = 1080;
queryObj["Active"] = true;
queryObj["ScreenWidth"] = screenWidth;
queryObj["ScreenHeight"] = screenHeight;
queryObj.Put();
ManagementBaseObject inParams = queryObj.GetMethodParameters("WmiSetBrightness");
inParams["Brightness"] = (uint)100;
inParams["Timeout"] = (uint)1;
queryObj.InvokeMethod("WmiSetBrightness", inParams, null);
}
// Set the orientation for each monitor
searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM WmiMonitorBasicDisplayParams"));
Dictionary<uint, string> orientationMap = new Dictionary<uint, string>()
{
{ DMDO_DEFAULT, "Default" },
{ DMDO_90, "90 degrees" },
{ DMDO_180, "180 degrees" },
{ DMDO_270, "270 degrees" }
};
int i = 1;
foreach (ManagementObject queryObj in searcher.Get())
{
uint orientation = (uint)DMDO_DEFAULT;
if (i == 1)
{
orientation = (uint)DMDO_DEFAULT; // First monitor is primary
}
else
{
Console.WriteLine($"Enter orientation for monitor {i} (1 = default, 2 = 90 degrees, 3 = 180 degrees, 4 = 270 degrees): ");
uint.TryParse(Console.ReadLine(), out orientation);
orientation--;
}
queryObj["DisplayOrientation"] = orientation;
queryObj.Put();
Console.WriteLine($"Monitor {i} orientation set to {orientationMap[orientation]}");
i++;
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment