///主程序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resolution;
using System.Collections;
namespace Resolution
{
public class Display
{
//public DEVMODE getResolution();
//public List<DEVMODE> getAllResolution();
public Resolution.DEVMODE devMode = new Resolution.DEVMODE();
public Resolution.DMDO dmdo = new Resolution.DMDO();
public Resolution resolution = new Resolution();
Resolution.DEVMODE dm = new Resolution.DEVMODE();
public readonly int[] disPlay = new int[400];
int count = 0;
public Display()
{
List<Resolution.DEVMODE> allMode = new List<Resolution.DEVMODE>();
allMode = resolution.getAllResolution();
foreach (Resolution.DEVMODE dm in allMode)
{
disPlay[count] = dm.dmPelsWidth;
count++;
}
//找出里面支持最大分辨率
}
public int MaxWidth()
{
int max = 800;
foreach (var item in disPlay)
{
if (item > max)
{
max = item;
}
}
return max;
}
public int DisplayCurrent()
{
dm = resolution.getResolution();
int x = dm.dmPelsWidth;
return x;
}
public void SetRosulution(int width)
{
int height = 0;
switch (width)
{
case 1920:
height = 1080;
break;
case 1680:
height = 1050;
break;
case 1280:
height = 1024;
break;
}
resolution.setResolution(width, height, 60);
}
}
class Program
{
static void Main(string[] args)
{
Display dis = new Display();
int x = dis.MaxWidth();
int y = dis.DisplayCurrent();
if (x != y)
{
dis.SetRosulution(x);
}
}
}
}
==========================================
///resloution类.
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.Collections;
namespace Resolution
{
public class Resolution
{
public const int CDS_UPDATEREGISTRY = 0x01;
public const int CDS_TEST = 0x02;
public const int DISP_CHANGE_SUCCESSFUL = 0;
public const int DISP_CHANGE_RESTART = 1;
public const int DISP_CHANGE_FAILED = -1;
public enum DMDO
{
DEFAULT = 0,
D90 = 1,
D180 = 2,
D270 = 3
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
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;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public DMDO dmDisplayOrientation;
public int dmDisplayFixedOutput;
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;//颜色质量,如32位,24位
public int dmPelsWidth;//分辨率宽度,如1024
public int dmPelsHeight;//分辨率高度,如768
public int dmDisplayFlags;
public int dmDisplayFrequency;//刷新频率,如75Hz
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);
//设置分辨率,width宽,height高,displayFrequency刷新频率,设置成功返回true,否则false
//调用方式: setResolution(1024, 768, 75);
public bool setResolution(int width, int height, int displayFrequency)
{
bool ret = false;
long RetVal = 0;
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmDisplayFrequency = displayFrequency;
dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
RetVal = ChangeDisplaySettings(ref dm, CDS_TEST);
if (RetVal == 0)
{
RetVal = ChangeDisplaySettings(ref dm, 0);
ret = true;
}
return ret;
}
//设置分辨率,width宽,height高,displayFrequency刷新频率,bitsPerPel颜色位数,设置成功返回true,否则false
//调用方式: setResolution(1024, 768, 75, 32);
public bool setResolution(int width, int height, int displayFrequency, int bitsPerPel)
{
bool ret = false;
long RetVal = 0;
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmDisplayFrequency = displayFrequency;
dm.dmBitsPerPel = bitsPerPel;
dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
RetVal = ChangeDisplaySettings(ref dm, CDS_TEST);
if (RetVal == 0)
{
RetVal = ChangeDisplaySettings(ref dm, 0);
ret = true;
}
return ret;
}
//返回当前图形模式信息
public DEVMODE getResolution()
{
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, -1, ref dm);
return dm;
}
//返回所有支持图形模式
public List<DEVMODE> getAllResolution()
{
List<DEVMODE> allMode = new List<DEVMODE>();
DEVMODE dm = new DEVMODE();
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
int index = 0;
while (EnumDisplaySettings(null, index, ref dm))
{
allMode.Add(dm);
index++;
}
return allMode;
}
}
}