C# 动态调整分辨率

找了很久都没找到相关方法,在这里记录一下。

1、获取当前电脑分辨率:

 int SH = Screen.PrimaryScreen.Bounds.Height;//分辨率高度
 int SW = Screen.PrimaryScreen.Bounds.Width;//分辨率宽度

2、修改屏幕分辨率:


using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE1 
{
	[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 short 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;
};


class User_32
{
	[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 Resolution
{
	class CResolution
	{
        public CResolution() {

        }

        public CResolution(int a,int b)
		{
			int iWidth =a;
			int iHeight =b;
			DEVMODE1 dm = new DEVMODE1();
			dm.dmDeviceName = new String (new char[32]);
			dm.dmFormName = new String (new char[32]);
			dm.dmSize = (short)Marshal.SizeOf (dm);

			if (0 != User_32.EnumDisplaySettings (null, User_32.ENUM_CURRENT_SETTINGS, ref dm))
			{
				
				dm.dmPelsWidth = iWidth;
				dm.dmPelsHeight = iHeight;

				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",MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
				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("警告: 分辨率重启成功,重启电脑后生效","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
							break;
							//windows 9x series you have to restart
						}
						default:
						{
							
							MessageBox.Show("失败: 自动分辨率修改失败,请您手动调整电脑分辨率","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
							break;
							//failed to change
						}
					}
				}
				
			}
		}
    }
}

调用方法: Resolution.CResolution ChangeRes = new Resolution.CResolution(powerwidth, powerHeight);

3、获取电脑所有分辨率:(写了个去重,hs 里面就是去重后的)

DEVMODE1 vDevMode = new DEVMODE1();

            HashSet<string> hs = new HashSet<string>();

            int i = 0;
            while (User_32.EnumDisplaySettings(null, i, ref vDevMode) != 0)
            {
                hs.Add(vDevMode.dmPelsWidth.ToString()+"*"+ vDevMode.dmPelsHeight.ToString());
                i++;
            }
            foreach (String de in hs)
            {
                Console.WriteLine("Width:{0} ", de.ToString());
            }

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WPF 中实现页面的自适应分辨率,可以采取以下几个步骤: 1. 使用相对布局:使用 XAML 中的布局容器(如 Grid、StackPanel、WrapPanel 等),并设置控件的行、列以及其他布局属性,以实现自适应布局。 2. 使用 Grid 中的行和列定义:通过设置 Grid 中的行和列的定义,可以让控件根据窗口大小自动调整位置和大小。可以使用 "*" 表示自动调整大小的列或行,使用具体数值(如 "200")表示固定大小的列或行。 3. 使用 ViewBox 控件:ViewBox 是一个用于缩放其内容的容器控件。将需要自适应分辨率的内容放置在 ViewBox 中,并设置 Stretch 属性为 Uniform 或 UniformToFill,即可实现内容的自动缩放。 4. 响应窗口大小改变事件:在窗口的 SizeChanged 事件中编写代码,根据窗口的大小变化,调整控件的位置和大小来实现自适应分辨率。 5. 使用 VisualStateManager:通过使用 VisualStateManager,可以在不同的状态下应用不同的布局。根据窗口大小或其他条件,设置不同的视觉状态,在不同的状态下使用不同的布局。 6. 使用分辨率相关信息:可以通过 System.Windows.Forms.Screen 类获取当前屏幕的分辨率信息,并根据该信息调整控件的布局和大小。 综上所述,以上是一些常用的方法来实现 C# WPF 页面的自适应分辨率。根据具体的需求和场景,可以选择适合的方法来实现页面的自适应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值