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

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

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

ContractedBlock.gif ExpandedBlockStart.gif Using directives #region Using directives
InBlock.gif
InBlock.gif
using System;
InBlock.gif
using System.Collections.Generic;
InBlock.gif
using System.Windows.Forms;
InBlock.gif
ExpandedBlockEnd.gif
#endregion

None.gif
None.gif
namespace  ScreenResolution
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.EnableVisualStyles();
InBlock.gif            Application.EnableRTLMirroring();
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

Form1.cs
None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  ScreenResolution
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    partial 
class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public enum DMDO
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DEFAULT 
= 0,
InBlock.gif            D90 
= 1,
InBlock.gif            D180 
= 2,
InBlock.gif            D270 
= 3
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet 
= CharSet.Auto)]
InBlock.gif        
struct DEVMODE
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public const int DM_DISPLAYFREQUENCY = 0x400000;
InBlock.gif            
public const int DM_PELSWIDTH = 0x80000;
InBlock.gif            
public const int DM_PELSHEIGHT = 0x100000;
InBlock.gif            
private const int CCHDEVICENAME = 32;
InBlock.gif            
private const int CCHFORMNAME = 32;
InBlock.gif
InBlock.gif            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= CCHDEVICENAME)]
InBlock.gif            
public string dmDeviceName;
InBlock.gif            
public short dmSpecVersion;
InBlock.gif            
public short dmDriverVersion;
InBlock.gif            
public short dmSize;
InBlock.gif            
public short dmDriverExtra;
InBlock.gif            
public int dmFields;
InBlock.gif
InBlock.gif            
public int dmPositionX;
InBlock.gif            
public int dmPositionY;
InBlock.gif            
public DMDO dmDisplayOrientation;
InBlock.gif            
public int dmDisplayFixedOutput;
InBlock.gif
InBlock.gif            
public short dmColor;
InBlock.gif            
public short dmDuplex;
InBlock.gif            
public short dmYResolution;
InBlock.gif            
public short dmTTOption;
InBlock.gif            
public short dmCollate;
InBlock.gif            [MarshalAs(UnmanagedType.ByValTStr, SizeConst 
= CCHFORMNAME)]
InBlock.gif            
public string dmFormName;
InBlock.gif            
public short dmLogPixels;
InBlock.gif            
public int dmBitsPerPel;
InBlock.gif            
public int dmPelsWidth;
InBlock.gif            
public int dmPelsHeight;
InBlock.gif            
public int dmDisplayFlags;
InBlock.gif            
public int dmDisplayFrequency;
InBlock.gif            
public int dmICMMethod;
InBlock.gif            
public int dmICMIntent;
InBlock.gif            
public int dmMediaType;
InBlock.gif            
public int dmDitherType;
InBlock.gif            
public int dmReserved1;
InBlock.gif            
public int dmReserved2;
InBlock.gif            
public int dmPanningWidth;
InBlock.gif            
public int dmPanningHeight;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet = CharSet.Auto)]
InBlock.gif        
//static extern int ChangeDisplaySettings( DEVMODE lpDevMode,  int dwFlags);
InBlock.gif

InBlock.gif        
static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
InBlock.gif        
//private System.ComponentModel.Container components = null;
InBlock.gif
        public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
//        protected override void Dispose(bool disposing)
InBlock.gif
//        {
InBlock.gif
//            if (disposing)
InBlock.gif
//            {
InBlock.gif
//                if (components != null)
InBlock.gif
//                {
InBlock.gif
//                    components.Dispose();
InBlock.gif
//                }
InBlock.gif
//            }
InBlock.gif
//            base.Dispose(disposing);
InBlock.gif
//        }
InBlock.gif

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code
InBlock.gif
//        private void InitializeComponent()
InBlock.gif
//        {
InBlock.gif
//            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
InBlock.gif
//            this.ClientSize = new System.Drawing.Size(292, 273);
InBlock.gif
//            this.Text = "改变屏幕分辨率的例子";
InBlock.gif
//
InBlock.gif
//        }
ExpandedSubBlockEnd.gif
        #endregion

InBlock.gif
InBlock.gif
//        static void Main()
InBlock.gif
//        {
InBlock.gif
//            Form1 r = new Form1();
InBlock.gif
//            r.ChangeRes();
InBlock.gif
//            Application.Run(new Form1());
InBlock.gif
//        }
InBlock.gif

InBlock.gif        
void ChangeRes(int chMode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Form1 t 
= new Form1();
InBlock.gif            
long RetVal = 0;
InBlock.gif            DEVMODE dm 
= new DEVMODE();
InBlock.gif            dm.dmSize 
= (short)Marshal.SizeOf(typeof(DEVMODE));
InBlock.gif            
if (chMode == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dm.dmPelsWidth 
= 1600;
InBlock.gif                dm.dmPelsHeight 
= 1024;
InBlock.gif                dm.dmDisplayFrequency 
= 85;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (chMode == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dm.dmPelsWidth 
= 1024;
InBlock.gif                dm.dmPelsHeight 
= 768;
InBlock.gif                dm.dmDisplayFrequency 
= 85;
ExpandedSubBlockEnd.gif            }

InBlock.gif            dm.dmFields 
= DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;
InBlock.gif            RetVal 
= ChangeDisplaySettings(ref dm, 0);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ChangeRes(
1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ChangeRes(
2);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

3、在Design视图下将WindowsState设置成Minimized,ShowInTaskbar设置成False

其次根据每个用户的需要修改ChangeRes方法里对分辨率的设置,生成工程后将可执行文件放在此用户的启动文件夹内

总结:
程序的原理很简单,在用户登录时将分辨率设置成用户的期望值,程序在用户登出前始终运行,但用户不会察觉,在用户登出时,程序被终止,分辨率被设置回特定值,以此实现统一登录分辨率并且各用户有自己的分辨率。

改进:
如果用户较多,可以通过程序参数来改变屏幕分辨率,避免多次生成工程并产生多个版本的混乱。

测试平台:
Windows Server 2003,Visual C# Express 2005 Beta 1

转载于:https://www.cnblogs.com/daniel_ngn/archive/2004/10/05/49091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值