-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActuallyChangingResolution.cs
180 lines (142 loc) · 5.91 KB
/
ActuallyChangingResolution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Runtime.InteropServices;
namespace Resolutioner
{
// based on https://www.codeproject.com/Articles/36664/Changing-Display-Settings-Programmatically
// Copyright Mohammad Elsheimy 2009, Mika Feiler 2022
// CPL 1.0 License http://www.opensource.org/licenses/cpl1.0.php
// The Common Public License Version 1.0 (CPL)
public class ActuallyChangingResolution
{
[StructLayout(LayoutKind.Sequential,
CharSet = CharSet.Ansi)]
struct DEVMODE
{
// You can define the following constant
// but OUTSIDE the structure because you know
// that size and layout of the structure
// is very important
// CCHDEVICENAME = 32 = 0x50
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
// In addition you can define the last character array
// as following:
//[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
//public Char[] dmDeviceName;
// After the 32-bytes array
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSpecVersion;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverVersion;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmSize;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmDriverExtra;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmFields;
[MarshalAs(UnmanagedType.I4)]
public int dmPositionX;
[MarshalAs(UnmanagedType.I4)]
public int dmPositionY;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayOrientation;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFixedOutput;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmColor;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmDuplex;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmYResolution;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmTTOption;
[MarshalAs(UnmanagedType.I2)]
public Int16 dmCollate;
// CCHDEVICENAME = 32 = 0x50
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
// Also can be defined as
//[MarshalAs(UnmanagedType.ByValArray,
// SizeConst = 32, ArraySubType = UnmanagedType.U1)]
//public Byte[] dmFormName;
[MarshalAs(UnmanagedType.U2)]
public UInt16 dmLogPixels;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmBitsPerPel;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsWidth;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPelsHeight;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFlags;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDisplayFrequency;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMMethod;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmICMIntent;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmMediaType;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmDitherType;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved1;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmReserved2;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningWidth;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dmPanningHeight;
}
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern Boolean EnumDisplaySettings(
[param: MarshalAs(UnmanagedType.LPTStr)]
string lpszDeviceName,
[param: MarshalAs(UnmanagedType.U4)]
iModeNumEnum iModeNum,
[In, Out]
ref DEVMODE lpDevMode);
enum iModeNumEnum
{
ENUM_CURRENT_SETTINGS = -1,
ENUM_REGISTRY_SETTINGS = -2
}
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.I4)]
static extern int ChangeDisplaySettings(
[In, Out]
ref DEVMODE lpDevMode,
[param: MarshalAs(UnmanagedType.U4)]
uint dwflags);
public enum dispChangeResult
{
SUCCESSFUL = 0, // Indicates that the function succeeded.
BADMODE = -2, // The graphics mode is not supported.
_FAILED = -1, // The display driver failed the specified graphics mode.
RESTART = 1 // The computer must be restarted for the graphics mode to work.
}
public static dispChangeResult ChangeDisplaySettings(int width, int height)
{
DEVMODE originalMode = new DEVMODE();
originalMode.dmSize =
(ushort)Marshal.SizeOf(originalMode);
// Retrieving current settings
// to edit them
#pragma warning disable CS8625 // Nie można przekonwertować literału o wartości null na nienullowalny typ referencyjny.
EnumDisplaySettings(null,
#pragma warning restore CS8625 // Nie można przekonwertować literału o wartości null na nienullowalny typ referencyjny.
iModeNumEnum.ENUM_CURRENT_SETTINGS,
ref originalMode);
// Making a copy of the current settings
// to allow reseting to the original mode
DEVMODE newMode = originalMode;
// Changing the settings
newMode.dmPelsWidth = (uint)width;
newMode.dmPelsHeight = (uint)height;
// Capturing the operation result
int result =
ChangeDisplaySettings(ref newMode, 0);
return (dispChangeResult) result;
}
}
}