Solved

Change display settings.

Posted on 1998-08-24
10
558 Views
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?
0
Comment
Question by:rutexavier
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:waty
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.

0
 

Author Comment

by:rutexavier
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.
0
 
LVL 14

Expert Comment

by:waty
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.
0
 

Author Comment

by:rutexavier
ID: 1430636
i already did
0
 

Author Comment

by:rutexavier
ID: 1430637
Adjusted points to 200
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Accepted Solution

by:
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


DEVMODE Member
 Description
 Example
 
dmBitsPerPel
 Indicates the display's color resolution
 4 bits for 16 colors

8 bits for 256 colors
16 bits for 65536 colors
 
dmPelsWidth
 Indicates the width of the display
 640 pixels
 
dmPelsHeight
 Indicates the height of the display
 480 pixels
 
dmDisplayFlags
 Indicates the display's mode
 DM_GRAYSCALE indicates that the display does not support color
DM_INTERLACED indicates that the display mode is interlaced
 
dmDisplayFrequency
 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

Flag
 Description
 
0
 Change the display settings now
 
CDS_UPDATEREGISTRY
 Change the display settings now and make these settings the default by saving them in the registry under HKEY_CURRENT_USER
 
CDS_TEST
 Just test to see if the requested settings are valid
 
CDS_FULLSCREEN
 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
 Description
 
DISP_CHANGE_SUCCESSFUL
 The display's settings have changed immediately—Windows NT 4.0 cannot change settings dynamically, and always returns DISP_CHAGE_RESTART instead
 
DISP_CHANGE_RESTART
 The display's settings have been changed but require the computer to be rebooted before they can take affect
 
DISP_CHANGE_BADFLAGS
 An invalid flag was passed
 
DISP_CHANGE_FAILED
 The display driver failed to change the settings
 
DISP_CHANGE_BADMODE
 The requested settings are not supported by the display
 
DISP_CHANGE_NOTUPDATED
 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,
                                            HORZRES);
dvmdOrig.dmPelsHeight       = GetDeviceCaps(hdc,
                                            VERTRES);
dvmdOrig.dmBitsPerPel       = GetDeviceCaps(hdc,
                                            BITSPIXEL);
dvmdOrig.dmDisplayFrequency = GetDeviceCaps(hdc,
                                            VREFRESH);
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.

0
 
LVL 14

Expert Comment

by:waty
ID: 1430639
Here is a more complete sample :

Changing the Screen Resolution at Run Time in Visual Basic

Abstract
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 CCDEVICENAME = 32
Const CCFORMNAME = 32
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
Dim DevM As DEVMODE

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
    Do
        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.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT

    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.

0
 

Author Comment

by:rutexavier
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.
0
 
LVL 14

Expert Comment

by:waty
ID: 1430641
????

This is the same comment as the previous

????
0
 

Author Comment

by:rutexavier
ID: 1430642
I want to give you an Excelent.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now