C# 調整解析度,主要是用在網吧裡面,有幾種配置的機子。統一調成顯示器最大的解析度。

來源:互聯網
上載者:User

///主程式.

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++;
               

            }
 
            //找出裡面支援最大解析度
           

      
 
        }
        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類.

 

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;

            [MarshalAs(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;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
            public string dmFormName;
            public short dmLogPixels;
            public int dmBitsPerPel;//顏色品質,如32位,24位
            public int dmPelsWidth;//解析度寬度,如1024
            public int dmPelsHeight;//解析度高度,如768
            public int dmDisplayFlags;
            public int dmDisplayFrequency;//重新整理頻率,如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);

        //設定解析度,width寬,height高,displayFrequency重新整理頻率,設定成功返回true,否則false
        //調用方式: 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;
        }

        //設定解析度,width寬,height高,displayFrequency重新整理頻率,bitsPerPel顏色位元,設定成功返回true,否則false
        //調用方式: 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;
        }

        //返回當前圖形模式資訊
        public DEVMODE getResolution()
        {
            DEVMODE dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            bool mybool;
            mybool = EnumDisplaySettings(null, -1, ref dm);
            return dm;
        }

        //返回所有支援圖形模式
        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;
        }

    }
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.