Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CODE To change Screen Resolution

Status
Not open for further replies.

williamu

Programmer
Apr 8, 2002
494
GB
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, &quot;An error has occured while trying to change screen resolution.\nPlease Technical Support.&quot;) ;
break ;
case DISP_CHANGE_FAILED:
sprintf(pMsg, &quot;The required resolution of %d x %d is not supported.\nPlease contact your Systems Hardware Vendor.&quot;, SCR_HEIGHT, SCR_WIDTH) ;
break ;
case DISP_CHANGE_SUCCESSFUL:
sprintf(pMsg, &quot;%s requires this video mode to operate.\nYour normal view will be returned on exit.&quot;, &quot;MyApp&quot;) ;
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top