The following code can be used to temporarily change the screen resolution, returning it to what it was before your application started.
I normally put this code in the InitInstance of my apps as it will terminate the startup if it fails.
SCR_HEIGHT and SCR_WIDTH must be set to legitimate values, otherwise it fails. I.E. 600 x 800, 768 x 1024 etc.
As it stands the code assumes you want to make the screen larger but this may not always be the case. Simply change the < to > in the GetSystemMetrics() line.
// Get the current Screen Resolution and change if to small.
static const int SCR_HEIGHT = 768; // Change this to what you need.
static const int SCR_WIDTH = 1024; // As above
char pMsg[128] = {0} ;
if (GetSystemMetrics(SM_CXSCREEN) < SCR_WIDTH && GetSystemMetrics(SM_CYSCREEN) < SCR_HEIGHT)
{
long lRet = 0 ;
DEVMODE lpMode ;
memset(&lpMode, 0, sizeof(DEVMODE)) ;
lpMode.dmSize = sizeof(DEVMODE) ;
// Allocate memory for Driver custom data.
lpMode.dmDriverExtra = (unsigned short) GlobalAlloc(GHND, sizeof(DEVNAMES)) ;
// Interogate the current default device's settings.
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &lpMode))
{
char * msg = {0} ;
// Set the new video mode
lpMode.dmPelsHeight = SCR_HEIGHT ;
lpMode.dmPelsWidth = SCR_WIDTH ;
lpMode.dmFields = DM_PELSHEIGHT | DM_PELSWIDTH ;
lRet = ChangeDisplaySettings(&lpMode, CDS_FULLSCREEN) ;
// Test result to see if successful.
switch (lRet)
{
case DISP_CHANGE_BADFLAGS:
case DISP_CHANGE_BADMODE:
sprintf(pMsg, "An error has occured while trying to change screen resolution.\nPlease Technical Support." ;
break ;
case DISP_CHANGE_FAILED:
sprintf(pMsg, "The required resolution of %d x %d is not supported.\nPlease contact your Systems Hardware Vendor.", SCR_HEIGHT, SCR_WIDTH) ;
break ;
case DISP_CHANGE_SUCCESSFUL:
sprintf(pMsg, "%s requires this video mode to operate.\nYour normal view will be returned on exit.", "MyApp" ;
break ;
}
// Display the Message to the User.
if (pMsg) AfxMessageBox(pMsg, MB_ICONINFORMATION | MB_OK) ;
}
// Free Allocated Memory
GlobalFree((void*) lpMode.dmDriverExtra) ;
// If anything other than success bail out.
if (lRet != DISP_CHANGE_SUCCESSFUL) return false ;
}
William
Software Engineer
ICQ No. 56047340
I normally put this code in the InitInstance of my apps as it will terminate the startup if it fails.
SCR_HEIGHT and SCR_WIDTH must be set to legitimate values, otherwise it fails. I.E. 600 x 800, 768 x 1024 etc.
As it stands the code assumes you want to make the screen larger but this may not always be the case. Simply change the < to > in the GetSystemMetrics() line.
// Get the current Screen Resolution and change if to small.
static const int SCR_HEIGHT = 768; // Change this to what you need.
static const int SCR_WIDTH = 1024; // As above
char pMsg[128] = {0} ;
if (GetSystemMetrics(SM_CXSCREEN) < SCR_WIDTH && GetSystemMetrics(SM_CYSCREEN) < SCR_HEIGHT)
{
long lRet = 0 ;
DEVMODE lpMode ;
memset(&lpMode, 0, sizeof(DEVMODE)) ;
lpMode.dmSize = sizeof(DEVMODE) ;
// Allocate memory for Driver custom data.
lpMode.dmDriverExtra = (unsigned short) GlobalAlloc(GHND, sizeof(DEVNAMES)) ;
// Interogate the current default device's settings.
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &lpMode))
{
char * msg = {0} ;
// Set the new video mode
lpMode.dmPelsHeight = SCR_HEIGHT ;
lpMode.dmPelsWidth = SCR_WIDTH ;
lpMode.dmFields = DM_PELSHEIGHT | DM_PELSWIDTH ;
lRet = ChangeDisplaySettings(&lpMode, CDS_FULLSCREEN) ;
// Test result to see if successful.
switch (lRet)
{
case DISP_CHANGE_BADFLAGS:
case DISP_CHANGE_BADMODE:
sprintf(pMsg, "An error has occured while trying to change screen resolution.\nPlease Technical Support." ;
break ;
case DISP_CHANGE_FAILED:
sprintf(pMsg, "The required resolution of %d x %d is not supported.\nPlease contact your Systems Hardware Vendor.", SCR_HEIGHT, SCR_WIDTH) ;
break ;
case DISP_CHANGE_SUCCESSFUL:
sprintf(pMsg, "%s requires this video mode to operate.\nYour normal view will be returned on exit.", "MyApp" ;
break ;
}
// Display the Message to the User.
if (pMsg) AfxMessageBox(pMsg, MB_ICONINFORMATION | MB_OK) ;
}
// Free Allocated Memory
GlobalFree((void*) lpMode.dmDriverExtra) ;
// If anything other than success bail out.
if (lRet != DISP_CHANGE_SUCCESSFUL) return false ;
}
William
Software Engineer
ICQ No. 56047340