Forum Discussion

rminnich's avatar
rminnich
Contributor
13 years ago

Suggestion: Check display settings before updating

The help section "Get and set display settings" includes an example function SetDisplay.  This function changes the current video settings using Win32.



Prior to the line

SetDisplay = Win32API.ChangeDisplaySettings(dm, 0)



It might be a good idea to check if the display settings are valid.  This is possible using SetDisplay = Win32API.ChangeDisplaySettings(dm, 2) and checking that its return value is 0.



Note: the value 2 in the ChangeDisplaySettings call is the value for CDS_TEST.  According to the MSDN entry for ChangeDisplaySettings, "Specifying CDS_TEST allows an application to determine which graphics modes are actually valid, without causing the system to change to that graphics mode."

1 Reply

  • Hi Russell,



    Thanks for your suggestion. Actually, even if the CDS_TEST flag is not passed through the second parameter of the ChangeDisplaySettings function, the latter doesn't apply invalid settings (if any) to a display device.



    When you pass valid settings through the first parameter and the value of 0 through the second one, ChangeDisplaySettings tries to change the display mode and returns the value of 0 (DISP_CHANGE_SUCCESSFUL) if the change was successful. When invalid settings are passed through the first parameter and 0 through the second one, the function does not change current display settings and returns -2 (DISP_CHANGE_BADMODE). In this case, the result is the same as if you pass invalid settings to ChangeDisplaySettings along with the CDS_TEST flag for checking whether the specified display mode is supported or not. Thus, if you try to pass invalid display settings to the function, that won't be a disaster. In any case, you can easily check the function result, compare it with DISP_CHANGE_BADMODE (or with the value of -2) and learn that the specified settings were not applied because they are not supported by the device.



    When you specify the CDS_TEST flag, ChangeDisplaySettings simply checks whether the specified display mode is supported or not and doesn't apply the settings to the device in any case. Therefore, calling the ChangeDisplaySettings function with this flag is really useful when it is needed just to verify whether a certain display mode is supported or not and when it is not needed to apply the settings to the device immediately.



    Since the SetDisplay routine in the "Get and set display settings" sample tries to change the specified display settings, I think there is no need to verify the settings right before applying them. If the settings are invalid, they are not applied and checking the function results allows you to learn about it. And if the settings are valid, calling the ChangeDisplaySettings function twice to check the settings and subsequently apply them (with re-checking) would be a redundant thing, I think.



    However, it would be really nice to check the results of the ChangeDisplaySettings function call and post an appropriate error message to the test log when the specified settings are invalid and not applied to the display device. We consider updating the sample code in this way. Again, thanks a lot for your suggestion.