Change display settings.

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?
Who is Participating?
watyConnect With a Mentor Commented:
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.

'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.

rutexavierAuthor Commented:
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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
rutexavierAuthor Commented:
i already did
rutexavierAuthor Commented:
Adjusted points to 200
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.

rutexavierAuthor Commented:
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.

This is the same comment as the previous

rutexavierAuthor Commented:
I want to give you an Excelent.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.