Daniel's blog

.Net - Just cool!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  23 随笔 :: 0 文章 :: 108 评论 :: 1 引用

在windows下要实现不同用户拥有不同分辨率,为什么不自己动手来实现呢,看看如下实例吧

首先制作一个能改变屏幕分辨率的C#程序,源代码如下,使用了Visual C# Express 2005 BETA 1:
1、新建Windows Application工程,取名为ScreenResolution
2、粘贴各文件的代码:
Program.cs

Using directives

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

        
Windows Form Designer generated code

//        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# Express 2005 Beta 1

posted on 2004-10-05 13:44  Daniel  阅读(2679)  评论(1编辑  收藏