C#程序中怎么修改屏幕的分辨率

 我来答
魔法软糖™
2016-12-05 · TA获得超过837个赞
知道小有建树答主
回答量:822
采纳率:94%
帮助的人:139万
展开全部

软糖一言不合就上代码:

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

namespace CSharp类库参考._08Windows技巧
{
    public class 修改屏幕分辨率
    {
        int i = Screen.PrimaryScreen.Bounds.Width;
        int j = Screen.PrimaryScreen.Bounds.Height;
        [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);
        public void 改变分辨率(int 宽, int 高,int 刷新率 = 60)
        {
            DEVMODE DevM = new DEVMODE();
            DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            bool mybool;
            mybool = EnumDisplaySettings(null, 0, ref DevM);
            DevM.dmPelsWidth = 宽;
            DevM.dmPelsHeight = 高;
            DevM.dmDisplayFrequency = 刷新率;
            DevM.dmBitsPerPel = 32;//颜色象素 
            long result = ChangeDisplaySettings(ref DevM, 0);
        }
        public void 还原分辨率()
        {
            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);
        }        

    }
}

满意请采纳,谢谢。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式