在Windows下让不同用户使用不同的分辨率(C# 2005)
2024-07-21 02:18:54
供稿:网友
在windows下要实现不同用户拥有不同分辨率其实可以自己动手来实现,看看如下实例吧:
首先制作一个能改变屏幕分辨率的c#程序,源代码如下,使用了vs.net 2005 beta 1:
1、新建windows application工程,取名为screenresolution
2、粘贴各文件的代码:
program.cs
--------------------------------------------------------------------------------
#region using directives
using system;
using system.collections.generic;
using system.windows.forms;
#endregion
namespace screenresolution
{
static class program
{
/// <summary>
/// the main entry point for the application.
/// </summary>
[stathread]
static void main()
{
application.enablevisualstyles();
application.enablertlmirroring();
application.run(new form1());
}
}
}
--------------------------------------------------------------------------------
form1.cs
--------------------------------------------------------------------------------
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
namespace screenresolution
{
partial 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(int chmode)
{
form1 t = new form1();
long retval = 0;
devmode dm = new devmode();
dm.dmsize = (short)marshal.sizeof(typeof(devmode));
if (chmode == 1)
{
dm.dmpelswidth = 1600;
dm.dmpelsheight = 1024;
dm.dmdisplayfrequency = 85;
}
else if (chmode == 2)
{
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);
}
private void form1_load(object sender, eventargs e)
{
changeres(1);
}
private void form1_formclosing(object sender, formclosingeventargs e)
{
changeres(2);
}
}
}
3、在design视图下将windowsstate设置成minimized,showintaskbar设置成false
其次根据每个用户的需要修改changeres方法里对分辨率的设置,生成工程后将可执行文件放在此用户的启动文件夹内
总结:
程序的原理很简单,在用户登录时将分辨率设置成用户的期望值,程序在用户登出前始终运行,但用户不会察觉,在用户登出时,程序被终止,分辨率被设置回特定值,以此实现统一登录分辨率并且各用户有自己的分辨率。
改进:
如果用户较多,可以通过程序参数来改变屏幕分辨率,避免多次生成工程并产生多个版本的混乱。
测试平台:
windows server 2003,visual c# 2005 beta 1