-
Notifications
You must be signed in to change notification settings - Fork 0
/
setScreen.cs
197 lines (175 loc) · 6.17 KB
/
setScreen.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TaskTrayApplication
{
// Struct for DEVMODE
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
public enum DisplaySettingsFlags : int
{
CDS_UPDATEREGISTRY = 1,
CDS_TEST = 2,
CDS_FULLSCREEN = 4,
CDS_GLOBAL = 8,
CDS_SET_PRIMARY = 0x10,
CDS_RESET = 0x40000000,
CDS_NORESET = 0x10000000
}
public enum ScreenOrientation : int
{
DMDO_DEFAULT = 0,
DMDO_90 = 1,
DMDO_180 = 2,
DMDO_270 = 3
}
public struct screenState
{
public string name;
public int x;
public int y;
public int width;
public int height;
public int rotate;
public int hz;
public override string ToString() {
return $"{name};{x};{y};{width};{height};{rotate};{hz}";
}
public static screenState fromString(string s)
{
string[] arr = s.Split(';');
return new screenState { name = arr[0], x = int.Parse(arr[1]), y = int.Parse(arr[2]), width = int.Parse(arr[3]), height = int.Parse(arr[4]), rotate = int.Parse(arr[5]), hz = int.Parse(arr[6]) };
}
}
internal class SetScreen
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
public static extern int ChangeDisplaySettings(ref DEVMODE devMode, DisplaySettingsFlags flags);
[DllImport("user32.dll")]
public static extern int ChangeDisplaySettingsExA(
string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd,
DisplaySettingsFlags dwflags, IntPtr lParam);
public static string ScreenStateArrayToString(screenState[] states)
{
string s = "";
int i = 0;
foreach (screenState ss in states)
{
if (i++ > 0)
{
s += "|";
}
s += ss.ToString();
}
return s;
}
public static screenState[] ScreenStateArrayfromString( string s )
{
string[] arr = s.Split('|');
screenState[] result= new screenState[arr.Length];
int i = 0;
foreach (string ss in arr)
{
result[i]=screenState.fromString(ss);
i++;
}
return result;
}
public static screenState[] getScreenState()
{
screenState[] sss= new screenState[Screen.AllScreens.Count()];
int i = 0;
foreach (Screen screen in Screen.AllScreens)
{
string id = screen.DeviceName;
Point position = screen.Bounds.Location;
int width = screen.Bounds.Width;
int height = screen.Bounds.Height;
DEVMODE originalMode = new DEVMODE();
EnumDisplaySettings(id, -1, ref originalMode);
sss[i] = new screenState()
{
name = id,
x = originalMode.dmPositionX,
y = originalMode.dmPositionY,
width = originalMode.dmPelsWidth,
height = originalMode.dmPelsHeight,
rotate = (int)originalMode.dmDisplayOrientation,
hz = originalMode.dmDisplayFrequency
};
i++;
}
return sss;
}
public static int setScreenState(screenState[] states)
{
int Rets = 0;
foreach (Screen screen in Screen.AllScreens)
{
string id = screen.DeviceName;
foreach (screenState ss in states)
{
if (ss.name == id)
{
DEVMODE originalMode = new DEVMODE();
EnumDisplaySettings(id, -1, ref originalMode); // Get the current settings
DEVMODE newMode = originalMode;
newMode.dmPositionX = ss.x;
newMode.dmPositionY = ss.y;
newMode.dmPelsWidth = ss.width;
newMode.dmPelsHeight = ss.height;
newMode.dmDisplayOrientation = (ScreenOrientation)ss.rotate;
newMode.dmDisplayFrequency = ss.hz;
int iRet = ChangeDisplaySettingsExA(id, ref newMode, IntPtr.Zero, DisplaySettingsFlags.CDS_UPDATEREGISTRY, IntPtr.Zero);
if (iRet != 0 && Rets == 0)
{
Rets = iRet;
}
break;
}
}
}
return Rets;
}
}
}