有没有方法可以用VB.NET实现更改屏幕的分辨率

dangdangdangdang 2003-10-15 09:28:15
如题
请赐教
不胜感谢!
...全文
59 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-10-17
  • 打赏
  • 举报
回复
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);
}
}
}


Louis,

You have many of the member and parameter datatypes one size too big.
longs should be ints, and ints should be shorts.

Also, I'd recommend that you switch to CharSet.Auto throughout, to get
best performance.

This code works for me.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
// specify the size of the string arrays to 32
public const int SA_SIZE = 32;

// struct (class) members
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst=SA_SIZE)]
public string lpzDeviceName;

public short iSpecVersion;
public short iDriverVersion;
public short iSize;
public short iDriverExtra;
public int iFields;
public short iOrientation;
public short iPaperSize;
public short iPaperLength;
public short iPaperWidth;
public short iScale;
public short iCopies;
public short iDefaultSource;
public short iPrintQuality;
public short iColor;
public short iDuplex;
public short iYRes;
public short iTTOption;
public short iCollate;

// specify marshalling attrib appropriately for this
type
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst=SA_SIZE)]
public string lpszFormName;

public short iLogPixels;
public int iBitsPerPixel;
public int lPelsWidth;
public int lPelsHeight;
public int lDisplayFlags;
public int lDisplayFreq;
public int lICMMethod;
public int lICMIntent;
public int lMediaType;
public int lDitherType;
public int lReserved1;
public int lReserved2;
public int lPanWidth;
public int lPanHeight;
}

class LibWrapper
{
// do a little P/Invoke stuff here...
// specify the dll containing the desired function and
// the fact that we want to use the default character
set for the call
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool
EnumDisplaySettings(string lpszDeviceName,
int lModeNum,
ref DEVMODE lpdm);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int
ChangeDisplaySettings([In] ref DEVMODE lpdm,
int iFlags);

}

[STAThread]
static void Main(string[] args)
{
DEVMODE dm = new DEVMODE();

dm.iSize = (short)Marshal.SizeOf(typeof(DEVMODE));
int lMode = -1;

bool b = LibWrapper.EnumDisplaySettings(null, lMode,
ref dm);

}
dangdangdangdang 2003-10-17
  • 打赏
  • 举报
回复
非常感谢!
saucer 2003-10-15
  • 打赏
  • 举报
回复
use ChangeDisplaySettings API

http://abstractvb.com/code.asp?A=947

http://go.6to23.com/jinqu/vb/8.htm

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧