C#中如何设置显示器分辨率为800×600?

是否要调用API,如何使用?... 是否要调用API,如何使用? 展开
 我来答
思念天灵
2008-08-23 · TA获得超过199个赞
知道小有建树答主
回答量:149
采纳率:0%
帮助的人:109万
展开全部
是肯定要调用API,类库中没有提供过这样的类。

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

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

参考资料: http://www.dreamlandcn.com/html/CSharpDotNet/412/

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

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式