1 | using System;
|
2 | using System.Collections.Generic;
|
3 | using System.ComponentModel;
|
4 | using System.Data;
|
5 | using System.Drawing;
|
6 | using System.Linq;
|
7 | using System.Text;
|
8 | using System.Windows.Forms;
|
9 | using System.Runtime.InteropServices;
|
10 |
|
11 |
|
12 | namespace ChangeResolution
|
13 | {
|
14 | public partial class Form1 : Form
|
15 | {
|
16 |
|
17 | [StructLayout(LayoutKind.Sequential)]
|
18 | public struct DISPLAY_DEVICE
|
19 | {
|
20 | public int cb;
|
21 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
22 | public string DeviceName;
|
23 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
24 | public string DeviceString;
|
25 | public int StateFlags;
|
26 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
27 | public string DeviceID;
|
28 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
|
29 | public string DeviceKey;
|
30 |
|
31 | public DISPLAY_DEVICE(int flags)
|
32 | {
|
33 | cb = 0;
|
34 | StateFlags = flags;
|
35 | DeviceName = new string((char)32, 32);
|
36 | DeviceString = new string((char)32, 128);
|
37 | DeviceID = new string((char)32, 128);
|
38 | DeviceKey = new string((char)32, 128);
|
39 | cb = Marshal.SizeOf(this);
|
40 | }
|
41 | }
|
42 |
|
43 | [StructLayout(LayoutKind.Sequential)]
|
44 | public struct DEVMODE
|
45 | {
|
46 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
47 | public string dmDeviceName;
|
48 | public short dmSpecVersion;
|
49 | public short dmDriverVersion;
|
50 | public short dmSize;
|
51 | public short dmDriverExtra;
|
52 | public int dmFields;
|
53 | public short dmOrientation;
|
54 | public short dmPaperSize;
|
55 | public short dmPaperLength;
|
56 | public short dmPaperWidth;
|
57 | public short dmScale;
|
58 | public short dmCopies;
|
59 | public short dmDefaultSource;
|
60 | public short dmPrintQuality;
|
61 | public short dmColor;
|
62 | public short dmDuplex;
|
63 | public short dmYResolution;
|
64 | public short dmTTOption;
|
65 | public short dmCollate;
|
66 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
67 | public string dmFormName;
|
68 | public short dmUnusedPadding;
|
69 | public short dmBitsPerPel;
|
70 | public int dmPelsWidth;
|
71 | public int dmPelsHeight;
|
72 | public int dmDisplayFlags;
|
73 | public int dmDisplayFrequency;
|
74 | }
|
75 |
|
76 | public Form1()
|
77 | {
|
78 | InitializeComponent();
|
79 | EnumDevices();
|
80 | }
|
81 |
|
82 | private void listDevices_SelectedIndexChanged(
|
83 | object sender, EventArgs e)
|
84 | {
|
85 | int devNum = listDevices.SelectedIndex;
|
86 | bool isMain = MainDevice(devNum);
|
87 | btnSet.Enabled = isMain; // enable only for the main device
|
88 | EnumModes(devNum);
|
89 | }
|
90 |
|
91 | private void btnSet_Click(object sender, EventArgs e)
|
92 | { //set selected display mode
|
93 | int devNum = listDevices.SelectedIndex;
|
94 | int modeNum = listSettings.SelectedIndex;
|
95 | DEVMODE d = GetDevmode(devNum, modeNum);
|
96 | if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0
|
97 | & d.dmPelsHeight != 0)
|
98 | {
|
99 | ChangeDisplaySettings(ref d, 0);
|
100 | }
|
101 | }
|
102 |
|
103 | private void EnumModes(int devNum)
|
104 | {
|
105 | listSettings.Items.Clear();
|
106 |
|
107 | string devName = GetDeviceName(devNum);
|
108 | DEVMODE devMode = new DEVMODE();
|
109 | int modeNum = 0;
|
110 | bool result = true;
|
111 | do
|
112 | {
|
113 | result = EnumDisplaySettings(devName,
|
114 | modeNum, ref devMode);
|
115 |
|
116 | if (result)
|
117 | {
|
118 | string item = DevmodeToString(devMode);
|
119 | MessageBox.Show("hallo");
|
120 | listSettings.Items.Add(item);
|
121 | }
|
122 | modeNum++;
|
123 | } while (result);
|
124 |
|
125 | if (listSettings.Items.Count > 0)
|
126 | {
|
127 | DEVMODE current = GetDevmode(devNum, -1);
|
128 | int selected = listSettings.FindString(
|
129 | DevmodeToString(current));
|
130 | if (selected >= 0)
|
131 | listSettings.SetSelected(selected, true);
|
132 | }
|
133 | }
|
134 |
|
135 | private DEVMODE GetDevmode(int devNum, int modeNum)
|
136 | { //populates DEVMODE for the specified device and mode
|
137 | DEVMODE devMode = new DEVMODE();
|
138 | string devName = GetDeviceName(devNum);
|
139 | EnumDisplaySettings(devName, modeNum, ref devMode);
|
140 | return devMode;
|
141 | }
|
142 |
|
143 | private string DevmodeToString(DEVMODE devMode)
|
144 | {
|
145 | return devMode.dmPelsWidth.ToString() +
|
146 | " x " + devMode.dmPelsHeight.ToString() +
|
147 | ", " + devMode.dmBitsPerPel.ToString() +
|
148 | " bits, " +
|
149 | devMode.dmDisplayFrequency.ToString() + " Hz";
|
150 | }
|
151 |
|
152 | private void EnumDevices()
|
153 | { //populates Display Devices list
|
154 | this.listDevices.Items.Clear();
|
155 | DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
|
156 |
|
157 | int devNum = 0;
|
158 | bool result;
|
159 | do
|
160 | {
|
161 | result = EnumDisplayDevices(IntPtr.Zero,
|
162 | devNum, ref d, 0);
|
163 |
|
164 | if (result)
|
165 | {
|
166 | string item = devNum.ToString() +
|
167 | ". " + d.DeviceString.Trim();
|
168 | if ((d.StateFlags & 4) != 0) item += " - main";
|
169 | this.listDevices.Items.Add(item);
|
170 | }
|
171 | devNum++;
|
172 | } while (result);
|
173 | }
|
174 |
|
175 | private string GetDeviceName(int devNum)
|
176 | {
|
177 | DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
|
178 | bool result = EnumDisplayDevices(IntPtr.Zero,
|
179 | devNum, ref d, 0);
|
180 | return (result ? d.DeviceName.Trim() : "#error#");
|
181 | }
|
182 |
|
183 | private bool MainDevice(int devNum)
|
184 | { //whether the specified device is the main device
|
185 | DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
|
186 | if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
|
187 | {
|
188 | return ((d.StateFlags & 4) != 0);
|
189 | } return false;
|
190 | }
|
191 |
|
192 | [DllImport("User32.dll")]
|
193 | private static extern bool EnumDisplayDevices(
|
194 | IntPtr lpDevice, int iDevNum,
|
195 | ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
|
196 |
|
197 | [DllImport("User32.dll")]
|
198 | private static extern bool EnumDisplaySettings(
|
199 | string devName, int modeNum, ref DEVMODE devMode);
|
200 |
|
201 | [DllImport("user32.dll")]
|
202 | public static extern int ChangeDisplaySettings(
|
203 | ref DEVMODE devMode, int flags);
|
204 |
|
205 |
|
206 | }
|
207 | }
|