用C#程序设置屏幕分辨率及刷新频率,程序中必须检查,不允许设置到显示器不支持的模式上,以免有可能损坏显示器(我所看到的前人的程序都未作检查)。解决方案: 用API枚举出当前显示器所支持的分辨率及刷新频率,填入列表框,然后再从中选择进行设置。
1
//
ScreenMode.cs - 使用API枚举并设置显示模式
2
//
Thu 2005.08.25
3
4
namespace
Skyiv
5![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
using System;
7
using System.Windows.Forms;
8
using System.Runtime.InteropServices;
9![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
class ScreenMode : Form
11![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
enum DMDO
{ DEFAULT = 0, D90 = 1, D180 = 2, D270 = 3 }
13![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
15
struct DEVMODE
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
public int dmPositionX;
33
public int dmPositionY;
34
public DMDO dmDisplayOrientation;
35
public int dmDisplayFixedOutput;
36![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
37
public short dmColor;
38
public short dmDuplex;
39
public short dmYResolution;
40
public short dmTTOption;
41
public short dmCollate;
42![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
public override string ToString()
61![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
74
[DllImport("user32.dll", CharSet = CharSet.Auto)]
75
static extern bool EnumDisplaySettings(int lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
76![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
77
[DllImport("user32.dll", CharSet = CharSet.Auto)]
78
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
79![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
80
ListBox lbxPels;
81![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
82
// 构造函数
83
public ScreenMode()
84![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
Text = "显示属性";
86![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
lbxPels.Items.Add(DevMode);
95
}
96
lbxPels.EndUpdate();
97![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
98
Button btnSet = new Button();
99
btnSet.Parent = this;
100
btnSet.Text = "设置显示模式";
101
btnSet.Dock = DockStyle.Top;
102
btnSet.Click += new EventHandler(SetScrnMode);
103![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
111
// 设置显示模式
112
void SetScrnMode(object sender, EventArgs e)
113![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
if (lbxPels.SelectedItem != null)
115![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
116
DEVMODE DevMode = (DEVMODE)lbxPels.SelectedItem;
117
ChangeDisplaySettings(ref DevMode, 0);
118
}
119
}
120![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
121
// 当前显示属性
122
void GetScrnInfo(object sender, EventArgs e)
123![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
141
// 程序入口
142
static void Main()
143![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
Application.Run(new ScreenMode());
145
}
146
}
147
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
12
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
48
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
57
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
58
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
59
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
61
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
62
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
63
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
64
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
65
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
67
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
68
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
69
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
74
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
76
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
77
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
78
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
79
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
80
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
81
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
82
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
83
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
84
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
85
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
86
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
87
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
88
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
89
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
90
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
91
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
92
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
93
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
94
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
95
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
96
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
97
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
98
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
99
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
100
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
101
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
102
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
103
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
104
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
105
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
106
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
107
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
108
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
109
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
110
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
111
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
112
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
113
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
114
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
115
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
116
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
117
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
118
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
119
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
121
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
122
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
123
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
124
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
125
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
126
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
127
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
128
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
129
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
130
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
131
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
132
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
133
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
134
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
135
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
136
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
137
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
138
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
139
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
140
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
141
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
142
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
143
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
144
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
145
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
146
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
147
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)