Some Win32 API Calls
All in all, it's a pretty easy task to switch the resolution. All we really need are two Win32 API calls, EnumDisplaySettings and ChangeDisplaySettings. As well, we'll need to look at the structure DEVMODE, which is used by both methods.
If you're on the MSDN, you can do a look-up on that API function, but here's the gist of it.
EnumDisplaySettings essentially gets information about any one of the available video modes. By making several calls to this function, you can get a list of all the available graphics modes. I'm going to cheat a bit here, and grab some of the explanations of these functions from the MSDN web site. I'll explain them as they relate to what we're going to do. The function looks like this:
BOOL EnumDisplaySettings(
LPCTSTR lpszDeviceName, // display device
DWORD iModeNum, // graphics mode
LPDEVMODE lpDevMode // graphics mode settings
);
Parameters
lpszDeviceName
Pointer to a null-terminated string that specifies the display device whose graphics mode the function will obtain information about. In Windows 95 and 98 (and our app), lpszDeviceName must be NULL.
iModeNum
This specifies the type of information to retrieve. This value can be a graphics mode index which we will use in our program.
lpDevMode
This is a pointer to a DEVMODE structure into which the function stores information about the specified graphics mode. One of the things that we are going to have to do before calling EnumDisplaySettings, set the dmSize member to sizeof(DEVMODE).
If we call EnumDisplaySettings repeatedly, how do we know how many time to iterate? It's pretty simple. The boolean return value from EnumDisplaySettings indicates if we've still got more values in the internal list of graphic modes available to us. When we get a false value back, we've hit the end of the list.
So, without going too far into detail now, let's assume that we've iterated through the list of graphics modes, and stored them in a list (I'll get to the list in a bit). From that, we can now determine if a requested display mode is in the list. If it is, we can then change the display mode with a call to ChangeDisplaySettings. In a nutshell, here's what we're looking at:
LONG ChangeDisplaySettings(
LPDEVMODE lpDevMode, // graphics mode
DWORD dwflags, // graphics mode options
);
Parameters
lpDevMode
This is a pointer to a DEVMODE structure describing the graphics mode to switch to. We've used the DEVMODE structure before, now let's look at some of the more important members of that structure:
Member | Meaning |
dmBitsPerPel | Bits per pixel |
dmPelsWidth | Pixel width |
dmPelsHeight | Pixel height |
dmDisplayFlags | Mode flags |
dmDisplayFrequency | Mode frequency |
dmPosition | Windows 98, Windows 2000: Position of the device in a multimonitor configuration |
dmFields | Field Flags. See below. |
As you can see, dmFields needs a bit of explanation. dmFields is used to determine which member of this structure is going to be used in changing the display setting. The valid flags are as follows:
Flag | Meaning |
DM_BITSPERPEL | Use the dmBitsPerPel value. |
DM_PELSWIDTH | Use the dmPelsWidth value. |
DM_PELSHEIGHT | Use the dmPelsHeight value. |
DM_DISPLAYFLAGS | Use the dmDisplayFlags value. |
DM_DISPLAYFREQUENCY | Use the dmDisplayFrequency value. |
DM_POSITION | Windows 98, Windows 2000: Use the dmPosition value. |
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.
dwFlags
We use this to determine how the video mode will be changed. For our purposes, we use CDS_FULLSCREEN, since this is the least obtrusive. It doesn't touch the registry, screw around with desktop icons, or other such foolishness.
Return Values
The ChangeDisplaySettings function returns one of the following values.
Value | Meaning |
DISP_CHANGE_SUCCESSFUL | The settings change was successful. |
DISP_CHANGE_RESTART | The computer must be restarted in order for the graphics mode to work. |
DISP_CHANGE_BADFLAGS | An invalid set of flags was passed in. |
DISP_CHANGE_BADPARAM | An invalid parameter was passed in. This can include an invalid flag or combination of flags. |
DISP_CHANGE_FAILED | The display driver failed the specified graphics mode. |
DISP_CHANGE_BADMODE | The graphics mode is not supported. |
DISP_CHANGE_NOTUPDATED | Windows NT/2000: Unable to write settings to the registry. |
|