C# 动态调整分辨率

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:05   7372   0

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

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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP