C#使用API枚举并设置显示模式
用C#程序设置屏幕分辨率及刷新频率,程序中必须检查,不允许设置到显示器不支持的模式上,以免有可能损坏显示器(我所看到的前人的程序都未作检查)。解决方案: 用API枚举出当前显示器所支持的分辨率及刷新频率,填入列表框,然后再从中选择进行设置。
1
// ScreenMode.cs - 使用API枚举并设置显示模式
2
// Thu 2005.08.25
3
4
namespace Skyiv
5
{
6
using System;
7
using System.Windows.Forms;
8
using System.Runtime.InteropServices;
9
10
class ScreenMode : Form
11
{
12
enum DMDO { DEFAULT = 0, D90 = 1, D180 = 2, D270 = 3 }
13
14
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
15
struct DEVMODE
16
{
17
public const int DM_BITSPERPEL = 0x040000;
18
public const int DM_PELSWIDTH = 0x080000;
19
public const int DM_PELSHEIGHT = 0x100000;
20
public const int DM_DISPLAYFREQUENCY = 0x400000;
21
private const int CCHDEVICENAME = 32;
22
private const int CCHFORMNAME = 32;
23
24
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
25
public string dmDeviceName;
26
public short dmSpecVersion;
27
public short dmDriverVersion;
28
public short dmSize;
29
public short dmDriverExtra;
30
public int dmFields;
31
32
public int dmPositionX;
33
public int dmPositionY;
34
public DMDO dmDisplayOrientation;
35
public int dmDisplayFixedOutput;
36
37
public short dmColor;
38
public short dmDuplex;
39
public short dmYResolution;
40
public short dmTTOption;
41
public short dmCollate;
42
43
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
44
public string dmFormName;
45
public short dmLogPixels;
46
public int dmBitsPerPel;
47
public int dmPelsWidth;
48
public int dmPelsHeight;
49
public int dmDisplayFlags;
50
public int dmDisplayFrequency;
51
public int dmICMMethod;
52
public int dmICMIntent;
53
public int dmMediaType;
54
public int dmDitherType;
55
public int dmReserved1;
56
public int dmReserved2;
57
public int dmPanningWidth;
58
public int dmPanningHeight;
59
60
public override string ToString()
61
{
62
return string.Format
63
(
64
"{0,4}×{1,-4} {2,2}Bits {3,3}Hz {4}",
65
dmPelsWidth,
66
dmPelsHeight,
67
dmBitsPerPel,
68
dmDisplayFrequency,
69
dmDeviceName
70
);
71
}
72
}
73
74
[DllImport("user32.dll", CharSet = CharSet.Auto)]
75
static extern bool EnumDisplaySettings(int lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
76
77
[DllImport("user32.dll", CharSet = CharSet.Auto)]
78
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
79
80
ListBox lbxPels;
81
82
// 构造函数
83
public ScreenMode()
84
{
85
Text = "显示属性";
86
87
lbxPels = new ListBox();
88
lbxPels.Parent = this;
89
lbxPels.Dock = DockStyle.Fill;
90
lbxPels.BeginUpdate();
91
DEVMODE DevMode = new DEVMODE();
92
for (int i = 0; EnumDisplaySettings(0, i, ref DevMode); i++)
93
{
94
lbxPels.Items.Add(DevMode);
95
}
96
lbxPels.EndUpdate();
97
98
Button btnSet = new Button();
99
btnSet.Parent = this;
100
btnSet.Text = "设置显示模式";
101
btnSet.Dock = DockStyle.Top;
102
btnSet.Click += new EventHandler(SetScrnMode);
103
104
Button btnGet = new Button();
105
btnGet.Parent = this;
106
btnGet.Text = "当前显示属性";
107
btnGet.Dock = DockStyle.Top;
108
btnGet.Click += new EventHandler(GetScrnInfo);
109
}
110
111
// 设置显示模式
112
void SetScrnMode(object sender, EventArgs e)
113
{
114
if (lbxPels.SelectedItem != null)
115
{
116
DEVMODE DevMode = (DEVMODE)lbxPels.SelectedItem;
117
ChangeDisplaySettings(ref DevMode, 0);
118
}
119
}
120
121
// 当前显示属性
122
void GetScrnInfo(object sender, EventArgs e)
123
{
124
Screen scrn = Screen.PrimaryScreen;
125
MessageBox.Show(
126
string.Format
127
(
128
"主设备: {1}{0}设备名: {2}{0}边 界: {3}{0}工作区: {4}",
129
Environment.NewLine,
130
scrn.Primary,
131
scrn.DeviceName,
132
scrn.Bounds,
133
scrn.WorkingArea
134
),
135
"当前显示属性",
136
MessageBoxButtons.OK,
137
MessageBoxIcon.Information
138
);
139
}
140
141
// 程序入口
142
static void Main()
143
{
144
Application.Run(new ScreenMode());
145
}
146
}
147
}
// ScreenMode.cs - 使用API枚举并设置显示模式2
// Thu 2005.08.253

4
namespace Skyiv5
{6
using System;7
using System.Windows.Forms;8
using System.Runtime.InteropServices;9

10
class ScreenMode : Form11
{12
enum DMDO { DEFAULT = 0, D90 = 1, D180 = 2, D270 = 3 }13

14
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]15
struct DEVMODE16
{17
public const int DM_BITSPERPEL = 0x040000;18
public const int DM_PELSWIDTH = 0x080000;19
public const int DM_PELSHEIGHT = 0x100000;20
public const int DM_DISPLAYFREQUENCY = 0x400000;21
private const int CCHDEVICENAME = 32;22
private const int CCHFORMNAME = 32;23

24
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]25
public string dmDeviceName;26
public short dmSpecVersion;27
public short dmDriverVersion;28
public short dmSize;29
public short dmDriverExtra;30
public int dmFields;31

32
public int dmPositionX;33
public int dmPositionY;34
public DMDO dmDisplayOrientation;35
public int dmDisplayFixedOutput;36

37
public short dmColor;38
public short dmDuplex;39
public short dmYResolution;40
public short dmTTOption;41
public short dmCollate;42

43
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]44
public string dmFormName;45
public short dmLogPixels;46
public int dmBitsPerPel;47
public int dmPelsWidth;48
public int dmPelsHeight;49
public int dmDisplayFlags;50
public int dmDisplayFrequency;51
public int dmICMMethod;52
public int dmICMIntent;53
public int dmMediaType;54
public int dmDitherType;55
public int dmReserved1;56
public int dmReserved2;57
public int dmPanningWidth;58
public int dmPanningHeight;59

60
public override string ToString()61
{62
return string.Format63
(64
"{0,4}×{1,-4} {2,2}Bits {3,3}Hz {4}",65
dmPelsWidth,66
dmPelsHeight,67
dmBitsPerPel,68
dmDisplayFrequency,69
dmDeviceName70
);71
}72
}73

74
[DllImport("user32.dll", CharSet = CharSet.Auto)]75
static extern bool EnumDisplaySettings(int lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);76

77
[DllImport("user32.dll", CharSet = CharSet.Auto)]78
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);79

80
ListBox lbxPels;81

82
// 构造函数83
public ScreenMode()84
{85
Text = "显示属性";86

87
lbxPels = new ListBox();88
lbxPels.Parent = this;89
lbxPels.Dock = DockStyle.Fill;90
lbxPels.BeginUpdate();91
DEVMODE DevMode = new DEVMODE();92
for (int i = 0; EnumDisplaySettings(0, i, ref DevMode); i++)93
{94
lbxPels.Items.Add(DevMode);95
}96
lbxPels.EndUpdate();97

98
Button btnSet = new Button();99
btnSet.Parent = this;100
btnSet.Text = "设置显示模式";101
btnSet.Dock = DockStyle.Top;102
btnSet.Click += new EventHandler(SetScrnMode);103

104
Button btnGet = new Button();105
btnGet.Parent = this;106
btnGet.Text = "当前显示属性";107
btnGet.Dock = DockStyle.Top;108
btnGet.Click += new EventHandler(GetScrnInfo);109
}110

111
// 设置显示模式112
void SetScrnMode(object sender, EventArgs e)113
{114
if (lbxPels.SelectedItem != null)115
{116
DEVMODE DevMode = (DEVMODE)lbxPels.SelectedItem;117
ChangeDisplaySettings(ref DevMode, 0);118
}119
}120

121
// 当前显示属性122
void GetScrnInfo(object sender, EventArgs e)123
{124
Screen scrn = Screen.PrimaryScreen;125
MessageBox.Show(126
string.Format127
(128
"主设备: {1}{0}设备名: {2}{0}边 界: {3}{0}工作区: {4}",129
Environment.NewLine,130
scrn.Primary,131
scrn.DeviceName,132
scrn.Bounds,133
scrn.WorkingArea134
),135
"当前显示属性",136
MessageBoxButtons.OK,137
MessageBoxIcon.Information138
);139
}140

141
// 程序入口142
static void Main()143
{144
Application.Run(new ScreenMode());145
}146
}147
}


浙公网安备 33010602011771号