Add Class File which is name is CResolution.cs
Copy and paste below code into above class
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Linq;
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public struct DEVMODE
{
public const int CCHDEVICENAME = 32;
public const int CCHFORMNAME = 32;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
[System.Runtime.InteropServices.FieldOffset(0)]
public string dmDeviceName;
[System.Runtime.InteropServices.FieldOffset(32)]
public Int16 dmSpecVersion;
[System.Runtime.InteropServices.FieldOffset(34)]
public Int16 dmDriverVersion;
[System.Runtime.InteropServices.FieldOffset(36)]
public Int16 dmSize;
[System.Runtime.InteropServices.FieldOffset(38)]
public Int16 dmDriverExtra;
[System.Runtime.InteropServices.FieldOffset(40)]
public DM dmFields;
[System.Runtime.InteropServices.FieldOffset(44)]
Int16 dmOrientation;
[System.Runtime.InteropServices.FieldOffset(46)]
Int16 dmPaperSize;
[System.Runtime.InteropServices.FieldOffset(48)]
Int16 dmPaperLength;
[System.Runtime.InteropServices.FieldOffset(50)]
Int16 dmPaperWidth;
[System.Runtime.InteropServices.FieldOffset(52)]
Int16 dmScale;
[System.Runtime.InteropServices.FieldOffset(54)]
Int16 dmCopies;
[System.Runtime.InteropServices.FieldOffset(56)]
Int16 dmDefaultSource;
[System.Runtime.InteropServices.FieldOffset(58)]
Int16 dmPrintQuality;
[System.Runtime.InteropServices.FieldOffset(44)]
public POINTL dmPosition;
[System.Runtime.InteropServices.FieldOffset(52)]
public Int32 dmDisplayOrientation;
[System.Runtime.InteropServices.FieldOffset(56)]
public Int32 dmDisplayFixedOutput;
[System.Runtime.InteropServices.FieldOffset(60)]
public short dmColor;
[System.Runtime.InteropServices.FieldOffset(62)]
public short dmDuplex;
[System.Runtime.InteropServices.FieldOffset(64)]
public short dmYResolution;
[System.Runtime.InteropServices.FieldOffset(66)]
public short dmTTOption;
[System.Runtime.InteropServices.FieldOffset(68)]
public short dmCollate;
[System.Runtime.InteropServices.FieldOffset(72)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
public string dmFormName;
[System.Runtime.InteropServices.FieldOffset(102)]
public Int16 dmLogPixels;
[System.Runtime.InteropServices.FieldOffset(104)]
public Int32 dmBitsPerPel;
[System.Runtime.InteropServices.FieldOffset(108)]
public Int32 dmPelsWidth;
[System.Runtime.InteropServices.FieldOffset(112)]
public Int32 dmPelsHeight;
[System.Runtime.InteropServices.FieldOffset(116)]
public Int32 dmDisplayFlags;
[System.Runtime.InteropServices.FieldOffset(116)]
public Int32 dmNup;
[System.Runtime.InteropServices.FieldOffset(120)]
public Int32 dmDisplayFrequency;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DISPLAY_DEVICE
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
public struct DISPLAY_SET
{
public DEVMODE DeviceMode;
public DISPLAY_DEVICE DisplayDevice;
}
public struct POINTL
{
public Int32 x;
public Int32 y;
}
[Flags()]
public enum DM : int
{
Orientation = 0x1,
PaperSize = 0x2,
PaperLength = 0x4,
PaperWidth = 0x8,
Scale = 0x10,
Position = 0x20,
NUP = 0x40,
DisplayOrientation = 0x80,
Copies = 0x100,
DefaultSource = 0x200,
PrintQuality = 0x400,
Color = 0x800,
Duplex = 0x1000,
YResolution = 0x2000,
TTOption = 0x4000,
Collate = 0x8000,
FormName = 0x10000,
LogPixels = 0x20000,
BitsPerPixel = 0x40000,
PelsWidth = 0x80000,
PelsHeight = 0x100000,
DisplayFlags = 0x200000,
DisplayFrequency = 0x400000,
ICMMethod = 0x800000,
ICMIntent = 0x1000000,
MediaType = 0x2000000,
DitherType = 0x4000000,
PanningWidth = 0x8000000,
PanningHeight = 0x10000000,
DisplayFixedOutput = 0x20000000
}
[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
//public enum DISP_CHANGE : int
//{
// Successful = 0,
// Restart = 1,
// Failed = -1,
// BadMode = -2,
// NotUpdated = -3,
// BadFlags = -4,
// BadParam = -5,
// BadDualView = -6
//}
public class User_32
{
[DllImport("User32.dll")]
public static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);
[DllImport("User32.dll")]
public static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
[DllImport("User32.dll")]
public static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
//[DllImport("User32.dll")]
//public static extern int EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE1 devMode);
//[DllImport("User32.dll")]
//public static extern int ChangeDisplaySettings(ref DEVMODE1 devMode, int flags);
public const int ENUM_CURRENT_SETTINGS = -1;
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;
}
namespace CResolution
{
public class CResolution
{
//get current resulation
public DisplayInfo GetCurrentResulation()
{
DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
var disInfo = new DisplayInfo();
if (0 != User_32.EnumDisplaySettings(null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
{
disInfo.DeviceName = dm.dmDeviceName;
disInfo.BitsPerPel = dm.dmBitsPerPel;
disInfo.DisplayFixedOutput = dm.dmDisplayFixedOutput;
disInfo.Width = dm.dmPelsWidth;
disInfo.Height = dm.dmPelsHeight;
}
return disInfo;
}
//get maximum resulation
public DisplayInfo GetMaxResulation()
{
DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String(new char[32]);
dm.dmFormName = new String(new char[32]);
dm.dmSize = (short)Marshal.SizeOf(dm);
var resulationList = new List<DisplayInfo>();
int i = 0;
while (0 != User_32.EnumDisplaySettings(null, i, ref dm))
{
var disInfo = new DisplayInfo();
disInfo.Width = dm.dmPelsWidth;
disInfo.Height = dm.dmPelsHeight;
resulationList.Add(disInfo);
i++;
}
var diInfo = new DisplayInfo();
int MaxHeight = resulationList.Max(x => x.Height);
diInfo = resulationList.First(x => x.Height == MaxHeight);
return diInfo;
}
//set maximum resulation
public void SetResulation(DisplayInfo disInfo)
{
int iWidth = disInfo.Width;
int iHeight = disInfo.Height;
//DisplayDevice is a wrapper ... you can find it [here](http://pinvoke.net/default.aspx/Structures/DISPLAY_DEVICE.html)
List<DISPLAY_DEVICE> devices = new List<DISPLAY_DEVICE>();
bool error = false;
//Here I am listing all DisplayDevices (Monitors)
for (int devId = 0; !error; devId++)
{
try
{
DISPLAY_DEVICE device = new DISPLAY_DEVICE();
device.cb = Marshal.SizeOf(typeof(DISPLAY_DEVICE));
error = User_32.EnumDisplayDevices(null, devId, ref device, 0) == 0;
devices.Add(device);
}
catch (Exception)
{
error = true;
}
}
List<DISPLAY_SET> devicesAndModes = new List<DISPLAY_SET>();
foreach (var dev in devices)
{
error = false;
//Here I am listing all DeviceModes (Resolutions) for each DisplayDevice (Monitors)
for (int i = 0; !error; i++)
{
try
{
//DeviceMode is a wrapper. You can find it [here](http://pinvoke.net/default.aspx/Structures/DEVMODE.html)
DEVMODE mode = new DEVMODE();
mode.dmDeviceName = new String(new char[32]);
mode.dmFormName = new String(new char[32]);
mode.dmSize = (short)Marshal.SizeOf(mode);
error = User_32.EnumDisplaySettings(dev.DeviceName, -1 + i, ref mode) == 0;
//Display
devicesAndModes.Add(new DISPLAY_SET { DisplayDevice = dev, DeviceMode = mode });
}
catch (Exception)
{
error = true;
}
}
}
//Select any 800x600 resolution ...
DEVMODE dm = devicesAndModes.Where(s => s.DeviceMode.dmPelsWidth == iWidth).First().DeviceMode;
dm.dmBitsPerPel = (int)disInfo.BitsPerPel;
dm.dmDisplayFixedOutput = disInfo.DisplayFixedOutput;
//Apply the selected resolution ..
int iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_TEST);
if (iRet == User_32.DISP_CHANGE_FAILED)
{
MessageBox.Show("Unable to process your request");
MessageBox.Show("Description: Unable To Process Your Request. Sorry For This Inconvenience.", "Information", MessageBoxButton.OK);
}
else
{
iRet = User_32.ChangeDisplaySettings(ref dm, User_32.CDS_UPDATEREGISTRY);
switch (iRet)
{
case User_32.DISP_CHANGE_SUCCESSFUL:
{
break;
//successfull change
}
case User_32.DISP_CHANGE_RESTART:
{
MessageBox.Show("Description: You Need To Reboot For The Change To Happen.\n If You Feel Any Problem After Rebooting Your Machine\nThen Try To Change Resolution In Safe Mode.", "Information", MessageBoxButton.OK);
break;
//windows 9x series you have to restart
}
default:
{
MessageBox.Show("Description: Failed To Change The Resolution.", "Information", MessageBoxButton.OK);
break;
//failed to change
}
}//end of swithc case
}//end of else
}//end of method
}
public class DisplayInfo
{
public string DeviceName { get; internal set; }
public int BitsPerPel { get; internal set; }
public int DisplayFixedOutput { get; internal set; }
public int Width { get; internal set; }
public int Height { get; internal set; }
}
}
Calling your application loading by this method
#region Set Screen Resulation
private void SetScreenResulation()
{
CResolution cR = new CResolution();
//get current resulation
DisplayInfo CurrentResulation = cR.GetCurrentResulation();
////get current resulation
DisplayInfo MaximumResulation = cR.GetMaxResulation();
MaximumResulation.BitsPerPel = CurrentResulation.BitsPerPel;
MaximumResulation.DisplayFixedOutput = CurrentResulation.DisplayFixedOutput;
//set height resulation
if (MaximumResulation.Width > CurrentResulation.Width)
cR.SetResulation(MaximumResulation);
}
#endregion
I think this is very important code for silverlight out of browser application.
N.B: Check 'require elevated trust when running in-browser'
Check 'require elevated trust when running outside the browser'