首页 > 开发 > 综合 > 正文

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

2024-07-21 02:27:05
字体:
来源:转载
供稿:网友

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

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 );
    }

    windows form designer generated code#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);
    }
  }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表