• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中改变显示器的分辨率

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#中改变显示器的分辨率


下面的代码实现修改显示器分辨率和刷新频率的功能:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
 
namespace ScreenResolution
{
 
  public class Form1 : System.Windows.Forms.Form
  {
    public enum DMDO
    {
      DEFAULT = 0,
      D90 = 1,
      D180 = 2,
      D270 = 3
    }
 
    [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( DEVMODE lpDevMode,  int dwFlags);
 
    static extern int ChangeDisplaySettings( [In] ref DEVMODE lpDevMode,  int dwFlags);
    private System.ComponentModel.Container components = null;
    public Form1()
    {
      InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
 
    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "改变屏幕分辨率的例子";
 
    }
    #endregion
 
    static void Main()
    {
      Form1 r = new Form1();
      r.ChangeRes();
      Application.Run(new Form1());
    }
 
    void ChangeRes()
    {
      Form1 t = new Form1();
      long RetVal=0;
      DEVMODE dm = new DEVMODE();
      dm.dmSize= (short)Marshal.SizeOf(typeof(DEVMODE));
      dm.dmPelsWidth = 1024;
      dm.dmPelsHeight= 768;
      dm.dmDisplayFrequency=85;
      dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
      RetVal = ChangeDisplaySettings(ref dm, 0);
    }
  }
} 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#向win32程序窗口中的文本框设置指定文本发布时间:2022-07-13
下一篇:
《Cocos2d-x实战(卷Ⅰ):C++开发》发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap