/// Main program.
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 ++;
}
// Find the maximum resolution supported
}
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 class.
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;
[Financialas (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;
[Financialas (UnmanagedType. ByValTStr, SizeConst = CCHFORMNAME)]
Public string dmFormName;
Public short dmLogPixels;
Public int dmBitsPerPel; // color quality, such as 32-bit and 24-bit
Public int dmPelsWidth; // The resolution width, such as 1024.
Public int dmPelsHeight; // resolution height, such as 768
Public int dmDisplayFlags;
Public int dmDisplayFrequency; // refresh frequency, for example, 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 );
// Sets the resolution, width, height, and displayFrequency. If the setting is successful, true is returned. Otherwise, false is returned.
// Call method: 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;
}
// Sets the resolution, width, height, displayFrequency, and the number of bits in the bitsPerPel color. If the setting is successful, true is returned. Otherwise, false is returned.
// Call method: 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;
}
// Returns the current image mode information.
Public DEVMODE getResolution ()
{
DEVMODE dm = new DEVMODE ();
Dm. dmSize = (short) Marshal. SizeOf (typeof (DEVMODE ));
Bool mybool;
Mybool = EnumDisplaySettings (null,-1, ref dm );
Return dm;
}
// Return all supported graphic Modes
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;
}
}
}