
Introduction
This article will briefly describe how to get all possible display modes for a system, including the current mode and also how to change the display mode dynamically.
Enumerating All Modes
To get all display modes available on the system, we use the EnumDisplaySettings API function.
From MSDN:
The EnumDisplaySettings function retrieves information about one of the graphics modes for a display device. To retrieve information for all the graphics modes of a display device, make a series of calls to this function.
So, to get all modes, we need to call this function until it returns FALSE. Here's the code:
BOOL bRetVal;
CString sDevMode;
iMode = 0;
do
{
bRetVal = ::EnumDisplaySettings(NULL, iMode, &devmode);
iMode++;
if (bRetVal)
{
sDevMode.Format("%d x %d, %d bits %dhtz",
devmode.dmPelsWidth, devmode.dmPelsHeight,
devmode.dmBitsPerPel, devmode.dmDisplayFrequency);
if (m_lb1.AddString(sDevMode)==LB_ERR)
AfxMessageBox("An error occurred!!!");
}
}
while (bRetVal);
In the above code segment, we increment the iModeNum parameter before each subsequent call to EnumDisplaySettings. According to MSDN, graphics mode indexes start at zero and when you call EnumDisplaySettings with iModeNum set to zero, the OS initializes and caches information about the display device. When you call the function with iModeNum set to a non-zero value, the function returns the information that was cached the last time the function was called with iModeNum set to zero.
The Current Display Mode
To find the display mode currently in use, set the iModeNum parameter of EnumDisplaySettings to ENUM_CURRENT_SETTINGS.
if (::EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devmode))
{
sDevMode.Format("%i x %i, %i bits %dhtz",
devmode.dmPelsWidth, devmode.dmPelsHeight,
devmode.dmBitsPerPel, devmode.dmDisplayFrequency);
m_lb1.SelectString(0, sDevMode);
}
Changing Modes
If you want to change the current display mode, use the ChangeDisplaySettings API function.
BOOL bRetVal;
iMode = m_lb1.GetCurSel();
bRetVal = ::EnumDisplaySettings(NULL, iMode, &devmode);
if (bRetVal)
{
devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT |
DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
::ChangeDisplaySettings(&devmode, 0);
}
Conclusion
This code can be used for DirectX programming to make sure the display adapter supports the correct modes. Other than that, its probably not a good idea to change the user's display mode in your app. But hey, that's up to you. (It is fun to play with however :) ).
Revision History
30 Jun 2002 - Initial Revision
| You must Sign In to use this message board. |
|
|
 |
|
 |
Hi good article! I tried you code, but I receive only modes that monitor can support. How can I get list of all hidden (unsupported modes)? Thanks.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Dear Sir, In the "Display Settings", we can find two check points. One is "This is my main monitor". Another is "Extend the desktop onto this monitor".
(I have two display devices.) Do I can use the same method to change ID1 or ID2 to be the main monitor? Another, how can I change the extend setting? Thanks.
Best regard, Ted Lin
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Use the EnumDisplayDevices API function to get your 2 monitors. I'm not sure about extended settings. Usually you can look for the same function name with an Ex on the end.
"Acceptance without proof is the fundamental characteristic of Western religion, rejection without proof is the fundamental characteristic of Western science." - Gary Zukav |
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Some of display modes are hidden by Control Panel -> Display -> Settings -> Advanced -> Monitor -> Hide modes that this monitor cannot display check box.
The ChangeDisplaySettings() will failed, when I change to one of hidden mode. But if I uncheck the check-box, the API can be change to the hide mode.
My question is: How can I uncheck the "Hide modes that this monitor cannot display " checkbox in my program (VC++)?
Thanks.
Robert Lin Robert_Lin@wistron.com.tw
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Thanks for the information however I have one question. Is there any way to make the changes to the display perminant? I have an application that will run on windows 2000 systems as part of an environment setup. Your code snippet works fine untill the system is rebooted, then the default display settings return. Any ideas...???
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Use ChangeDisplaySettings[^] with the CDS_UPDATEREGISTRY flag.
"We have done so much in the last 2 years, and it doesn't happen by standing around with your finger in your ear, hoping everyone thinks that that's nice." - Donald Rumsfeld
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi, This is not exactly related to the code but about monitor modes and thought you might be the authority i can ask this question.
I just tried your component on a win2k pro system. When i changed from the current resolution of 1024x768 at 85 Mhz to 640x480 at the same refresh rate, i found that the new screen mode is oblique. When at 640x480 resolution I changed the refresh rate from 85 Mhz to 60 Mhz the obliqueness was gone. I thought the refreshrate should not have any afftect on the structure of the co-ordinates except for some flickering under very low refresh rates - Any ideas ?
And I also want to mention that your test tool came to great help. Thank you,
Avi.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'm not an authority on this, so your question may not get answered correctly.
I would assume, your monitor may not support those refresh rates at that high resolution. Check with the monitor manufacturer.
Jason Henderson "You must be the change you wish to see in the world." - Gandhi
articles profile
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
How do I get the modelines for multiple monitors. This code only returns the primary monitors modes.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Here's an excerpt from MSDN documentation for EnumDisplaySettings. (See bolded text):
The EnumDisplaySettings function retrieves information about one of the graphics modes for a display device. To retrieve information for all the graphics modes of a display device, make a series of calls to this function.
BOOL EnumDisplaySettings( LPCTSTR lpszDeviceName, // display device DWORD iModeNum, // graphics mode LPDEVMODE lpDevMode // graphics mode settings ); Parameters lpszDeviceName [in] Pointer to a null-terminated string that specifies the display device about whose graphics mode the function will obtain information. This parameter is either NULL or a DISPLAY_DEVICE.DeviceName returned from EnumDisplayDevices. A NULL value specifies the current display device on the computer on which the calling thread is running.
Windows 95: lpszDeviceName must be NULL.
Jason Henderson start page ; articles henderson is coming henderson is an opponent's worst nightmare * googlism * |
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
This is a very frequently asked question... thankyou for explaining it to the rest of us!
With time we live, with money we spend! Joel Holdsworth
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |
|
|