-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (72 loc) · 2.55 KB
/
Copy pathProgram.cs
File metadata and controls
81 lines (72 loc) · 2.55 KB
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
using System.Runtime.InteropServices;
namespace ToggleResolution
{
internal static class Program
{
[STAThread]
public static int Main()
{
try
{
return Run() ? 0 : 1;
}
catch (Exception exception)
{
ShowMessageBox($"Error: {exception.Message}");
return 2;
}
}
private static bool Run()
{
var currentDevMode = default(NativeMethods.DEVMODE);
currentDevMode.dmSize = (ushort) Marshal.SizeOf(currentDevMode);
NativeMethods.EnumDisplaySettings(null, NativeMethods.ENUM_CURRENT_SETTINGS, ref currentDevMode);
var availableDevMode = default(NativeMethods.DEVMODE);
availableDevMode.dmSize = (ushort) Marshal.SizeOf(availableDevMode);
var modeIndex = 0;
while (NativeMethods.EnumDisplaySettings(null, modeIndex, ref availableDevMode))
{
if (availableDevMode.dmPelsWidth == currentDevMode.dmPelsWidth * 2 &&
availableDevMode.dmPelsHeight == currentDevMode.dmPelsHeight * 2 &&
availableDevMode.dmBitsPerPel == currentDevMode.dmBitsPerPel &&
availableDevMode.dmDisplayFlags == currentDevMode.dmDisplayFlags &&
availableDevMode.dmDisplayFrequency == currentDevMode.dmDisplayFrequency)
{
ChangeDisplaySettings(ref availableDevMode);
return true;
}
modeIndex++;
}
modeIndex = 0;
while (NativeMethods.EnumDisplaySettings(null, modeIndex, ref availableDevMode))
{
if (availableDevMode.dmPelsWidth >= 800 &&
availableDevMode.dmPelsWidth == currentDevMode.dmPelsWidth / 2 &&
availableDevMode.dmPelsHeight == currentDevMode.dmPelsHeight / 2 &&
availableDevMode.dmBitsPerPel == currentDevMode.dmBitsPerPel &&
availableDevMode.dmDisplayFlags == currentDevMode.dmDisplayFlags &&
availableDevMode.dmDisplayFrequency == currentDevMode.dmDisplayFrequency)
{
ChangeDisplaySettings(ref availableDevMode);
return true;
}
modeIndex++;
}
ShowMessageBox($"Could not find double or half resolution of {currentDevMode.dmPelsWidth}x{currentDevMode.dmPelsHeight}.");
return false;
}
private static void ShowMessageBox(params string[] lines)
{
MessageBox.Show(
text: string.Join(Environment.NewLine, lines),
caption: c_appCaption);
}
private static void ChangeDisplaySettings(ref NativeMethods.DEVMODE devMode)
{
var result = NativeMethods.ChangeDisplaySettings(ref devMode, NativeMethods.CDS_UPDATEREGISTRY);
if (result != NativeMethods.DISP_CHANGE_SUCCESSFUL)
throw new InvalidOperationException($"ChangeDisplaySettings failed with result {result}.");
}
private const string c_appCaption = "ToggleResolution";
}
}