改变屏幕分辨率

 

采用ChangeDisplaySettings这个API函数就可以了。

参考

http://msdn2.microsoft.com/en-us/library/ms533260.aspx

http://www.codeproject.com/csharp/CSDynamicScrRes.asp

http://www.study888.com/computer/pro/cc/200512/128708.html

Windows GDI
ChangeDisplaySettings

The ChangeDisplaySettings function changes the settings of the default display device to the specified graphics mode.

To change the settings of a specified display device, use the ChangeDisplaySettingsEx function.

LONG ChangeDisplaySettings(
    LPDEVMODE lpDevMode,  // graphics mode
    DWORD dwflags         // graphics mode options
);
Parameters
lpDevMode
[in] Pointer to a DEVMODE structure that describes the new graphics mode. If lpDevMode is NULL, all the values currently in the registry will be used for the display setting. Passing NULL for the lpDevMode parameter and 0 for the dwFlags parameter is the easiest way to return to the default mode after a dynamic mode change.

The dmSize member of DEVMODE must be initialized to the size, in bytes, of the DEVMODE structure. The dmDriverExtra member of DEVMODE must be initialized to indicate the number of bytes of private driver data following the DEVMODE structure. In addition, you can use any or all of the following members of the DEVMODE structure.

MemberMeaning
dmBitsPerPelBits per pixel
dmPelsWidthPixel width
dmPelsHeightPixel height
dmDisplayFlagsMode flags
dmDisplayFrequencyMode frequency
dmPosition Windows 98/Me, Windows 2000/XP: Position of the device in a multimonitor configuration

In addition to using one or more of the preceding DEVMODE members, you must also set one or more of the following values in the dmFields member to change the display setting.

ValueMeaning
DM_BITSPERPELUse the dmBitsPerPel value.
DM_PELSWIDTHUse the dmPelsWidth value.
DM_PELSHEIGHTUse the dmPelsHeight value.
DM_DISPLAYFLAGSUse the dmDisplayFlags value.
DM_DISPLAYFREQUENCYUse the dmDisplayFrequency value.
DM_POSITION Windows 98/Me, Windows 2000/XP: Use the dmPosition value.

dwflags
[in] Indicates how the graphics mode should be changed. This parameter can be one of the following values.
ValueMeaning
0The graphics mode for the current screen will be changed dynamically.
CDS_FULLSCREENThe mode is temporary in nature.

Windows NT/2000/XP: If you change to and from another desktop, this mode will not be reset.

CDS_GLOBALThe settings will be saved in the global settings area so that they will affect all users on the machine. Otherwise, only the settings for the user are modified. This flag is only valid when specified with the CDS_UPDATEREGISTRY flag.
CDS_NORESETThe settings will be saved in the registry, but will not take effect. This flag is only valid when specified with the CDS_UPDATEREGISTRY flag.
CDS_RESETThe settings should be changed, even if the requested settings are the same as the current settings.
CDS_SET_PRIMARYThis device will become the primary device.
CDS_TESTThe system tests if the requested graphics mode could be set.
CDS_UPDATEREGISTRYThe graphics mode for the current screen will be changed dynamically and the graphics mode will be updated in the registry. The mode information is stored in the USER profile.

Specifying CDS_TEST allows an application to determine which graphics modes are actually valid, without causing the system to change to that graphics mode.

If CDS_UPDATEREGISTRY is specified and it is possible to change the graphics mode dynamically, the information is stored in the registry and DISP_CHANGE_SUCCESSFUL is returned. If it is not possible to change the graphics mode dynamically, the information is stored in the registry and DISP_CHANGE_RESTART is returned.

Windows NT/2000/XP: If CDS_UPDATEREGISTRY is specified and the information could not be stored in the registry, the graphics mode is not changed and DISP_CHANGE_NOTUPDATED is returned.

Return Values

The ChangeDisplaySettings function returns one of the following values.

ValueMeaning
DISP_CHANGE_SUCCESSFULThe settings change was successful.
DISP_CHANGE_BADDUALVIEWWindows XP: The settings change was unsuccessful because system is DualView capable.
DISP_CHANGE_BADFLAGSAn invalid set of flags was passed in.
DISP_CHANGE_BADMODEThe graphics mode is not supported.
DISP_CHANGE_BADPARAMAn invalid parameter was passed in. This can include an invalid flag or combination of flags.
DISP_CHANGE_FAILEDThe display driver failed the specified graphics mode.
DISP_CHANGE_NOTUPDATEDWindows NT/2000/XP: Unable to write settings to the registry.
DISP_CHANGE_RESTARTThe computer must be restarted in order for the graphics mode to work.

Remarks

To ensure that the DEVMODE structure passed to ChangeDisplaySettings is valid and contains only values supported by the display driver, use the DEVMODE returned by the EnumDisplaySettings function.

When the display mode is changed dynamically, the WM_DISPLAYCHANGE message is sent to all running applications with the following message parameters.

ParametersMeaning
wParamNew bits per pixel
LOWORD(lParam)New pixel width
HIWORD(lParam)New pixel height

Windows 95/98/Me: If the calling thread has any windows, ChangeDisplaySettings sends them the WM_DISPLAYCHANGE message immediately (for windows in all other threads, the message is sent when the thread can receive nonqueued messages). This may cause the shell to get its message too soon and could squash icons. To avoid this problem, have ChangeDisplaySettings do resolution switching by calling on a thread with no windows, for example, a new thread.

Windows 95/98/Me: ChangeDisplaySettingsW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Requirements

    Windows NT/2000/XP/Vista: Included in Windows NT 3.5 and later.
    Windows 95/98/Me: Included in Windows 95 and later.
    Header: Declared in Winuser.h; include Windows.h.
    Library: Use User32.lib.
    Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.

See Also

Device Contexts Overview, Device Context Functions, ChangeDisplaySettingsEx, CreateDC, DEVMODE, EnumDisplayDevices, EnumDisplaySettings, WM_DISPLAYCHANGE

Introduction

A common problem that some or all Windows developers are facing is changing the Screen Resolution dynamically. The resolution of the user screen is not necessarily the same as that of the development environment screen. Here what we will do is, in the requirement and installation note, we will mention that the user has to use xxxx * xxx resolution for better performance. Is this good?. If the end user does not have enough experience, what will happen? In this case, the user tries to open that product in lesser or greater resolution than the developed screen resolution and that will create some problems; problem in the sense that some portion of the window won't be visible to the user if he uses a lesser resolution or the window will become stretched if he uses a higher resolution. So here, you will get the ultimate solution for this sort of problem.

Through this article I will explain how we can:

  • get the end user resolution
  • change the resolution to our software/product compatible resolution
  • get the end user resolution back

Getting the end user resolution

In dotnet we can access the values of the user's screen resolution through the Screen class which ships with the Framework. Here we can get the Screen resolution through a Screen.PrimaryScreen Property, the signature of this property is as follows:

public static Screen PrimaryScreen {get;}

This property is a read-only property which has a return of type Screen. So our first goal has been achieved: that is getting the screen resolution.

Screen screen = Screen.PrimaryScreen;
int S_width=screen.Bounds.Width;
int S_height=screen.Bounds.Height;

Note: You can see the implementation of this logic in the project file (see download file above). You can apply this logic to any event you want.

Changing the resolution to our software/product compatible resolution

Our next goal is to set the screen resolution. Here you have to put in a little effort. In .NET there is no supported built-in class or classes for setting the resolution. You can achieve this through calling some Win32 API.

Before continuing, better read something about COM Interop service in dotnet (early and late binding), attribute, DLL import attribute and Platform Invoke.

I will explain the above technologies a little. I can't go into detail because here, my aim is something different.

Using the DllImport Attribute you are going to read the definition of unmanaged code to your managed environment. Here there are two functions available in the User32.dll file which are related to changing the screen resolution.

  • EnumDisplaySettings
  • ChangeDisplaySettings

If you want to use this unmanaged code definition here in your managed environment what you will do? There comes the DllImport Attribute. Using DllImport attribute you can access the functionality of the user32.dll file. See the code block.

Note: You can see the implementation of this logic in the project file (see download file above). You can apply this logic to any event that you want.

Here you are calling the Win32 API's User32.dll files.

class User32
{
          [DllImport("user32.dll")]
          public static extern int EnumDisplaySettings (
            string deviceName, int modeNum, ref DEVMODE devMode );           
          [DllImport("user32.dll")]
          public static extern int ChangeDisplaySettings(
                ref DEVMODE devMode, int flags); 
 
          public const int ENUM_CURRENT_SETTINGS = -1;
          public const int CDS_UPDATEREGISTRY = 0x01;
          public const int CDS_TEST = 0x02;
          public const int DISP_CHANGE_SUCCESSFUL = 0;
          public const int DISP_CHANGE_RESTART = 1;
          public const int DISP_CHANGE_FAILED = -1;
}

The line [DllImport("user32.dll")] is for importing the DLL file to your managed environment.

If you want to use the Win32 API functions then you have to give the full signature of the function that you are going to use in your managed environment.

public static extern int EnumDisplaySettings 
      (string deviceName, int modeNum, ref DEVMODE devMode);

And the remaining lines are self explanatory. If you check the signature of both functions above, you can see DEVMODE references. What is this?

DEVMODE is a structure that is explained in the platform documentation as well as included with Visual Studio .NET. The structure is defined in C#.

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE 
{
          [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] 
            public string dmDeviceName;
          public short    dmSpecVersion;
          public short    dmDriverVersion;
          ... 
}

Note: You will get the full code block in project file that you can download at the top of this article. You can apply this logic to any event that you want.

Special care needs to be taken to make sure that the types are the right size and that fixed length strings are appropriately defined. In this case WORD maps to short, DWORD to int and short stays as short.

The function EnumDisplaySettings is called and as long as the return is non-zero we can call the ChangeDisplaySettings after changing the appropriate values in the DEVMODE structure:

DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new String (new char[32]);
dm.dmFormName = new String (new char[32]);
dm.dmSize = (short)Marshal.SizeOf (dm); 
 
if (0 != User32.EnumDisplaySettings (null, 
         User32.ENUM_CURRENT_SETTINGS, ref dm))
{

At this point the DEVMODE structure is filled with the settings for the default display. We can modify it and then call the change function:

dm.dmPelsWidth = iWidth;
dm.dmPelsHeight = iHeight; 
 
int iRet = User32.ChangeDisplaySettings (
    ref dm, User32.CDS_UPDATEREGISTRY);

The code in the download does this a little differently to cope with various error conditions and to test that we can set the display mode before we actually do so. I would encourage you to look at the full source file and see what it does. That's all there is to it. Now we achieved our second goal.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值