Change display settings.

Posted on 1998-08-24
Last Modified: 2012-06-27
I want to change the display settings to 800x600, 256 colors everytime my program starts and i need to return to the screen resolution the user had when the program finishes. What's the code to accomplish this?
Question by:rutexavier
  • 5
  • 5
LVL 14

Expert Comment

ID: 1430633
'Display Properties (Settings):
Shell "rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3", 5

After this, select the right setting you need using sendkeys.

NB : maybe you should need to restart your windows. Sometimes Windows ask it to the user.


Author Comment

ID: 1430634
I don't want the user to view the change display settings, it must be transparent for him and i don't want to restart the computer and i think that using sendkeys is not secure.
I want to do what microsoft does when a game starts.
LVL 14

Expert Comment

ID: 1430635
This is the only solution I have for the moment.

I am looking in the registry where is stored the value of this setting. I think it is there that you should find and change what you want.

The games form microsoft use Direct X. You can surely change the display settings but you should use DirectX. That is not a business solution.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 1430636
i already did

Author Comment

ID: 1430637
Adjusted points to 200
LVL 14

Accepted Solution

waty earned 200 total points
ID: 1430638
I found this article. I will continue to search.

The Win32 API has some new functions that allow you to work with screen resolutions. The first function is

 BOOL EnumDisplaySettings(LPCTSTR lpszDeviceName,
                         DWORD iModeNum,
                         LPDEVMODE lpDevMode);

This function enumerates all of the possible display settings for a given display. The first parameter, lpszDeviceName, indicates the display for which you want to enumerate settings. For now, you must pass NULL, but Microsoft is hard at work adding multiple-display support to Windows. In the future you'll be able to pass a string like "\\.\DisplayX", where X can have the values 1, 2, or 3.

Each display has a collection of settings that it supports. The iModeNum parameter indicates the collection entry that you want to obtain (the first setting is index 0). EnumDisplaySettings returns TRUE unless you pass an index in iModeNum that is outside the collection, in which case it returns FALSE. The display's setting information is returned in the DEVMODE structure pointed to by the lpDevMode parameter. DEVMODE has many members, but only 5 members have anything to do with display settings (see Figure 4).

Figure 4 DEVMODE's Relevant Members for Screen Settings

 Indicates the display's color resolution
 4 bits for 16 colors

8 bits for 256 colors
16 bits for 65536 colors
 Indicates the width of the display
 640 pixels
 Indicates the height of the display
 480 pixels
 Indicates the display's mode
 DM_GRAYSCALE indicates that the display does not support color
DM_INTERLACED indicates that the display mode is interlaced
 Indicates the refresh frequency of the display (Windows 95 always returns 0)
 60 Hz

OK, so that's how you get the settings supported by your display. To change a display's settings, you'll need to create a DEVMODE structure, initialize the members that pertain to the display, and call ChangeDisplaySettings.

 LONG ChangeDisplaySettings(LPDEVMODE lpDevMode,
                           DWORD dwflags);

The first parameter is the address of the initialized DEVMODE structure. The second parameter is one of the flags shown in Figure 5. Possible return values for ChangeDisplaySettings are shown in Figure 6. If DISP_CHANGE_SUCCESSFUL returns, a WM_DISPLAYCHANGE message is broadcast to all the top-level windows indicating the new bits-per-pixel, width, and height of the display. Finally, to get the current display settings, you'll use a Win32 function that's been around for years and years: GetDeviceCaps.

Figure 5 Change Display Settings Flags

 Change the display settings now
 Change the display settings now and make these settings the default by saving them in the registry under HKEY_CURRENT_USER
 Just test to see if the requested settings are valid
 This undocumented flag tells the system that the calling application wants to enter/leave full screen mode—this prevents the system from repositioning other windows to keep them visible

Figure 6 Change Display Settings Return Values

Return value
 The display's settings have changed immediately—Windows NT 4.0 cannot change settings dynamically, and always returns DISP_CHAGE_RESTART instead
 The display's settings have been changed but require the computer to be rebooted before they can take affect
 An invalid flag was passed
 The display driver failed to change the settings
 The requested settings are not supported by the display
 The settings are not changed because they couldn't also be saved in the registry—this only happens on Windows NT if access to the registry key is denied by the administrator

The following example shows how to get the current display settings:

 DEVMODE dvmdOrig;
HDC hdc = GetDC(NULL);  // Screen DC used to get
                        // current display
                        // settings
dvmdOrig.dmPelsWidth        = GetDeviceCaps(hdc,
dvmdOrig.dmPelsHeight       = GetDeviceCaps(hdc,
dvmdOrig.dmBitsPerPel       = GetDeviceCaps(hdc,
dvmdOrig.dmDisplayFrequency = GetDeviceCaps(hdc,
ReleaseDC(NULL, hdc);

To demonstrate the ChangeDisplaySettings function, I wrote the ChgResAndRun application (see Figure 7). This is a small, useful utility that changes the display's settings and spawns another application. It waits for the child process to terminate, then changes the resolution back to its original settings. I use this application myself all the time when playing games. For example, I usually run my machine in 1024´768 resolution, but when I want to play "You Don't Know Jack" I switch my display to 640´480 mode. When I'm finished, I want the display settings to reset to 1024´768.

To switch resolution and run my game, I created a shortcut with the following command line:

 "C:\Program Files\ChgResAndRun.exe" 640 480 0 0                                     =C:\YDKJ\YDKJ32.EXE

ChgResAndRun requires five command line arguments. The first two ("640" and "480") indicate the requested width and height of the display. The third argument indicates the bits-per-pixel, and the fourth argument indicates the refresh-frequency rate. If you pass a zero for any of the arguments, that particular setting is not changed. In the command line shown above, I pass zero for both the bits-per-pixel and the refresh frequency so these settings will not be affected.

After the four display setting arguments, you must have an equal sign followed by the command line that you want to execute. In my example, YDKJ32.EXE is invoked after the display settings are changed. While I'm playing, ChgResAndRun lingers in the background. When I quit the game, ChgResAndRun changes the display settings back to the original values and terminates.

LVL 14

Expert Comment

ID: 1430639
Here is a more complete sample :

Changing the Screen Resolution at Run Time in Visual Basic

Because Microsoft Windows 95 users may have different screen resolution settings, you may need to set the screen resolution to a specific setting while your Microsoft® Visual Basic® version application is running. This article explains how to change your screen resolution from within Visual Basic.

Setting the Screen Resolution
Under the Microsoft® Windows® 95 operating system, you can set your screen resolution by running the Display applet in Control Panel. In a Microsoft Visual Basic® version application, you can use the Windows application programming interface (API) EnumDisplaySettings and ChangeDisplaySettings functions to change the screen resolution while your program is running.

The EnumDisplaySettings function allows you to retrieve information about your display's graphics modes. This information is then stored in a DEVMODE structure.

After you have interrogated the computer system with the EnumDisplaySettings function, you use the ChangeDisplaySettings function to tell the operating system to use a different screen resolution.

The ChangeDisplaySettings function lets you set the screen resolution to a different graphics mode. The DEVMODE structure holds the graphics mode information to which you want to change.

In the example program below, you first retrieve the current screen resolution information by calling the EnumDisplaySettings function. The DEVMODE structure contains the graphics modes information for the display type. Next, you modify the dmPelsWidth and dmPelsHeight fields in the DEVMODE structure to reflect the new screen resolution you want to set. Finally, you call the ChangeDisplaySettings function to tell the operating system to set the new screen resolution as the default resolution.

Example Program
This program shows how to set the screen resolution from within a Visual Basic application.

Create a new project in Visual Basic. Form1 is created by default.

Add the following code to the General Declarations section of Form1 (note that each Declare statement must be typed as a single line of code):
Option Explicit
Private Declare Function EnumDisplaySettings Lib "user32" Alias
   "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long,
   ByVal iModeNum As Long, lpDevMode As Any) As Boolean

Private Declare Function ChangeDisplaySettings Lib "user32" Alias
   "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwflags As Long) As Long

Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000

Private Type DEVMODE
    dmDeviceName As String * CCDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer

    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer

    dmFormName As String * CCFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

Add a Command Button control to Form1. Command1 is created by default.

Add the following code to the Click event for Command1:
Private Sub Command1_Click()
    Dim a As Boolean
    Dim i&
    i = 0
        a = EnumDisplaySettings(0&, i&, DevM)
        i = i + 1
    Loop Until (a = False)
End Sub

Add a second Command Button control to Form1. Command2 is created by default.

Add the following code to the Click event for Command2:
Private Sub Command2_Click()
    Dim b&

    DevM.dmPelsWidth = 800
    DevM.dmPelsHeight = 600

    b = ChangeDisplaySettings(DevM, 0)
End Sub

Run the example program by pressing Click the first Command Button control. This retrieves all the graphics modes for your display. Next, click the second Command Button control to change the display's screen resolution to 800 x 600 graphics mode.


Author Comment

ID: 1430640
I don't want the user to view the change display settings, it must be transparent for him and i don't want to restart the computer and i think that using sendkeys is not secure.
I want to do what microsoft does when a game starts.
LVL 14

Expert Comment

ID: 1430641

This is the same comment as the previous


Author Comment

ID: 1430642
I want to give you an Excelent.

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question