关闭

.NET中调用Windows API函数强制显示器指定分辨率

1403人阅读 评论(0) 收藏 举报

案例:在软件开发过程中有时候需要使显示器的分辨率能够自适应软件设计图片或窗体的大小,因此想在软件启动的过程中重新设定显示的显示分辨率,同时当软件退出时恢复原来的分辨率设置.
分析:由于用到设置显示器分辨率的问题,因此要用到GDI的Windows API:
函数:ChangeDisplaySettings(LPDEVMODE lpDevMode,DWORD dwflags)
结构体:DEVMODE(The DEVMODE data structure contains information about the initialization and environment of a printer or a display device)
知识点:在C#中引用Windows API和结构体的方法.  
System.Runtime.InteropServices 命名空间提供各种各样支持 COM interop 及平台调用服务的成员。
StructLayout(LayoutKind.Explicit)通过使用属性可以自定义结构在内存中的布局方式。
MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)指示如何在托管代码和非托管代码之间封送数据。
DllImport extern 修饰符用于声明在外部实现的方法。extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static.
主要代码:
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        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.dmPelsHeight = 768;
            devM.dmPelsWidth = 1024;
            devM.dmDisplayFrequency = 60;
            devM.dmBitsPerPel = 32;
            long result = ChangeDisplaySettings(ref devM, 0);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            ChangeRes();
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:182215次
    • 积分:3708
    • 等级:
    • 排名:第9279名
    • 原创:152篇
    • 转载:20篇
    • 译文:0篇
    • 评论:13条
    最新评论
    量子统计