반응형
internal class DisplaySettings
{
[DllImport("user32.dll")]
private static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
[DllImport("user32.dll")]
private static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);
private const int ENUM_CURRENT_SETTINGS = -1;
private const int CDS_UPDATEREGISTRY = 0x01;
private const int CDS_FULLSCREEN = 0x04;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
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;
}
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
private const int LOGPIXELSX = 88;
private const int LOGPIXELSY = 90;
private const int HORZRES = 8;
private const int VERTRES = 10;
public static bool ChangeDisplayResolution(int width, int height)
{
DEVMODE devMode = new DEVMODE();
devMode.dmSize = (short)Marshal.SizeOf(devMode);
if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode))
{
devMode.dmPelsWidth = width;
devMode.dmPelsHeight = height;
int result = ChangeDisplaySettings(ref devMode, CDS_UPDATEREGISTRY);
return result == 0;
}
return false;
}
public static bool ChangeDisplayScaling(int scalingPercent)
{
DEVMODE devMode = new DEVMODE();
devMode.dmSize = (short)Marshal.SizeOf(devMode);
if (EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref devMode))
{
devMode.dmFields = devMode.dmFields | 0x20000; // DM_DISPLAYFLAGS
devMode.dmDisplayFlags = (scalingPercent == 100) ? 0 : 0x0001; // DMDFO_DEFAULT or DMDFO_STRETCH
int result = ChangeDisplaySettings(ref devMode, CDS_UPDATEREGISTRY);
return result == 0;
}
return false;
}
public struct ResolutionScaleInfo
{
public int Width;
public int Height;
public float ScalingFactorX;
public float ScalingFactorY;
}
public static ResolutionScaleInfo GetResolutionScaleInfo()
{
IntPtr hdc = GetDC(IntPtr.Zero);
int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
int width = GetDeviceCaps(hdc, HORZRES);
int height = GetDeviceCaps(hdc, VERTRES);
float scalingFactorX = dpiX / 96f;
float scalingFactorY = dpiY / 96f;
Console.WriteLine($"Resolution: {width} x {height}");
Console.WriteLine($"Scaling: {scalingFactorX:F2} x {scalingFactorY:F2}");
// Release the device context
if (hdc != IntPtr.Zero) ReleaseDC(IntPtr.Zero, hdc);
return new ResolutionScaleInfo()
{
Width = width,
Height = height,
ScalingFactorX = scalingFactorX,
ScalingFactorY = scalingFactorY,
};
}
}
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
ObservableCollection 의 RemoveAll Extention (0) | 2023.09.04 |
---|---|
C# Enum 타입을 int로 변환하는 Extention (0) | 2023.05.16 |
WPF - 여러개의 이미지 합치기 (0) | 2023.04.25 |
C# List에서 중복 제거한 리스트 만들기 (0) | 2023.04.06 |
WPF 컨트롤 위치값 구하기 (0) | 2023.03.28 |