C# 代码调整屏幕分辨率

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 代码调整屏幕分辨率相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 解决分辨率
{
    public partial class Form1 : Form
    {
        //保存当前屏幕分辨率
        int i = Screen.PrimaryScreen.Bounds.Width;
int j = Screen.PrimaryScreen.Bounds.Height;
        public Form1()
        {
            InitializeComponent();
        }
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        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;
        }
        [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);
        void ChangeRes()
        {

            DEVMODE DevM = new DEVMODE();
            DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            bool mybool;
            mybool = EnumDisplaySettings(null, 0, ref DevM);
            DevM.dmPelsWidth = 1024;//宽
            DevM.dmPelsHeight = 768;//高
            DevM.dmDisplayFrequency = 60;//刷新频率
            DevM.dmBitsPerPel = 32;//颜色象素
            long result = ChangeDisplaySettings(ref DevM, 0);
        }
        void FuYuan()
        {
            DEVMODE DevM = new DEVMODE();
            DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            bool mybool;
            mybool = EnumDisplaySettings(null, 0, ref DevM);
            DevM.dmPelsWidth = i;//恢复宽
            DevM.dmPelsHeight =j;//恢复高
            DevM.dmDisplayFrequency = 60;//刷新频率
            DevM.dmBitsPerPel = 32;//颜色象素
            long result = ChangeDisplaySettings(ref DevM, 0);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ChangeRes();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FuYuan();
            this.Close();
         
        }



本文出自 “温馨梦痕” 博客,请务必保留此出处http://317057112.blog.51cto.com/1361376/1859438

以上是关于C# 代码调整屏幕分辨率的主要内容,如果未能解决你的问题,请参考以下文章

如何根据设备/屏幕分辨率调整屏幕?

在C#环境下使用winfrom时如何让窗体根据用户显示器的分辨率来调整窗体的分辨率?

缩放 html 以根据屏幕分辨率按比例调整大小

WPF运行中怎么用C# 代码调整窗口位置,特别是怎么居中到屏幕中央

如何编码我的网站,以便我的100%宽度图像根据屏幕分辨率变化调整大小?

c#控件怎么样随屏幕分辨率的变化而变化?