[C#]設定螢幕解析度

  • 16093
  • 0
  • 2010-08-02

[C#]設定螢幕解析度

 

1. 問題描述

如何設定螢幕解析度,回應網友 Eddie 問題

 

2. 方法

透過 Windwos API 中的 ChangeDisplaySettings Function : changes the settings of the default display device to the specified graphics mode.

 

程式碼

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;

namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        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;
            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", CharSet = CharSet.Auto)]
        static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "目前的螢幕解析度 :" + Screen.PrimaryScreen.Bounds.Width.ToString() + " * " + Screen.PrimaryScreen.Bounds.Height.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            change(800, 600, 75);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            change(1024, 768, 75);
        }

        public enum DMDO
        {
            DEFAULT = 0,
            D90 = 1,
            D180 = 2,
            D270 = 3
        }

        /// <summary>
        /// 改變螢幕解析度
        /// </summary>
        /// <param name="intWidth"></param>  寬
        /// <param name="intHeight"></param>  長
        /// <param name="intFrequency"></param>  頻率
        private void change(int intWidth, int intHeight, int intFrequency)
        {
            long RetVal = 0;
            DEVMODE dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            dm.dmPelsWidth = intWidth;
            dm.dmPelsHeight = intHeight;
            dm.dmDisplayFrequency = intFrequency;
            dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
            RetVal = ChangeDisplaySettings(ref dm, 0);
            this.Text = "目前的螢幕解析度 :" + intWidth.ToString() + "*" + intHeight.ToString();
        }
    }
}

 

3. 結果

可點選 Button 改變螢幕解析度,而有時候沒有辦法改變的原因是有時候電腦硬體不支援其解析度。

image

 

範例下載

[C#]變更螢幕解析度.rar