在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