程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 在Windows下讓不同用戶使用不同的分辨率(C# 2005)

在Windows下讓不同用戶使用不同的分辨率(C# 2005)

編輯:C#入門知識

在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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved