在孟子E章的"孟宪会之精彩世界"里找到一个文章,地址如下:
http://dotnet.aspx.cc/article/2l9uxy9l-mdvh-4zi3-hpn1-rpb9kv5z63ym/read.aspx
老大就是历害,在这里给出了在C#中对ChangeDisplaySettings这个API的使用,不过不知道是什么原因,我按着孟子老大的代码执行后好像屏只是闪了一下,返回值为-2说是不正确的模式。我参照了用法,添加了EnumDisplaySettings这个API,目的是获取显示器的可用设置列表,我想从这个列表里找出任何一个来进行ChangeDisplaySettings一定不会错了,按着这个思路,我写了如下的代码,编译并测试通过了。一时高兴拿来和大家分享一下,代码如下:
测试窗体的代码:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
using
System.Security.Principal;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
ChangeDisplaySettingApp
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public partial class Form1 : Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public enum DMDO
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DEFAULT = 0,
D90 = 1,
D180 = 2,
D270 = 3
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct DEVMODE
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
public const int DM_DISPLAYFREQUENCY = 0x400000;
public const int DM_PELSWIDTH = 0x80000;
public const int DM_PELSHEIGHT = 0x100000;
private const int CCHDEVICENAME = 32;
private const int CCHFORMNAME = 32;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int dmPositionX;
public int dmPositionY;
public DMDO dmDisplayOrientation;
public int dmDisplayFixedOutput;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
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 override string ToString()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return string.Format("分辨率:{0}*{1};颜色质量:{2}位;刷新率:{3};", this.dmPelsWidth, this.dmPelsHeight, this.dmBitsPerPel, this.dmDisplayFrequency);
}
}
enum ChangeReturnValue
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DISP_CHANGE_SUCCESSFUL = 0,
DISP_CHANGE_RESTART = 1,
DISP_CHANGE_FAILED = -1,
DISP_CHANGE_BADMODE = -2,
DISP_CHANGE_NOTUPDATED = -3,
DISP_CHANGE_BADFLAGS = -4,
DISP_CHANGE_BADPARAM = -5,
DISP_CHANGE_BADDUALVIEW = -6
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern ChangeReturnValue ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Form1()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InitializeComponent();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void button1_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DEVMODE lpDevMode = new DEVMODE();
lpDevMode.dmDriverExtra = 0;
lpDevMode.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
lpDevMode.dmDriverExtra = 0;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int index = 0;
while (EnumDisplaySettings(null, index, ref lpDevMode))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.lstSettings.Items.Add(lpDevMode);
index++;
}
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void btnSetDiaplay_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DEVMODE curDevMode = new DEVMODE();
curDevMode.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool success = EnumDisplaySettings(null, -1, ref curDevMode);
if (success)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (this.lstSettings.SelectedIndex != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DEVMODE selectedMode = (DEVMODE)this.lstSettings.SelectedItem;
ChangeReturnValue retVal = ChangeDisplaySettings(ref selectedMode, 0);
if (MessageBox.Show(this, "确认这个设置吗?", "显示属性", MessageBoxButtons.OKCancel) != DialogResult.OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ChangeReturnValue resetVal = ChangeDisplaySettings(ref curDevMode, 0);
}
}
}
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{ }
}
}
}
设计器生成的代码:
namespace
ChangeDisplaySettingApp
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
partial class Form1
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (disposing && (components != null))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
components.Dispose();
}
base.Dispose(disposing);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.btnGetDisplaySettings = new System.Windows.Forms.Button();
this.lstSettings = new System.Windows.Forms.ListBox();
this.btnSetDiaplay = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnGetDisplaySettings
//
this.btnGetDisplaySettings.Location = new System.Drawing.Point(12, 12);
this.btnGetDisplaySettings.Name = "btnGetDisplaySettings";
this.btnGetDisplaySettings.Size = new System.Drawing.Size(129, 23);
this.btnGetDisplaySettings.TabIndex = 0;
this.btnGetDisplaySettings.Text = "获取显示器设置列表";
this.btnGetDisplaySettings.UseVisualStyleBackColor = true;
this.btnGetDisplaySettings.Click += new System.EventHandler(this.button1_Click);
//
// lstSettings
//
this.lstSettings.FormattingEnabled = true;
this.lstSettings.ItemHeight = 12;
this.lstSettings.Location = new System.Drawing.Point(12, 41);
this.lstSettings.Name = "lstSettings";
this.lstSettings.Size = new System.Drawing.Size(341, 268);
this.lstSettings.TabIndex = 1;
//
// btnSetDiaplay
//
this.btnSetDiaplay.Location = new System.Drawing.Point(156, 12);
this.btnSetDiaplay.Name = "btnSetDiaplay";
this.btnSetDiaplay.Size = new System.Drawing.Size(139, 23);
this.btnSetDiaplay.TabIndex = 2;
this.btnSetDiaplay.Text = "设置显示器显示属性";
this.btnSetDiaplay.UseVisualStyleBackColor = true;
this.btnSetDiaplay.Click += new System.EventHandler(this.btnSetDiaplay_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(374, 318);
this.Controls.Add(this.btnSetDiaplay);
this.Controls.Add(this.lstSettings);
this.Controls.Add(this.btnGetDisplaySettings);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private System.Windows.Forms.Button btnGetDisplaySettings;
private System.Windows.Forms.ListBox lstSettings;
private System.Windows.Forms.Button btnSetDiaplay;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
程序运行时图:
![]()