Skip to content

Instantly share code, notes, and snippets.

@botaor
Created December 5, 2013 11:43
Show Gist options
  • Save botaor/7803978 to your computer and use it in GitHub Desktop.
Save botaor/7803978 to your computer and use it in GitHub Desktop.
Change the screen orientation in Windows programatically
// code copied from: http://msdn.microsoft.com/en-us/library/ms812499.aspx
// Not acutally tested on a real system
DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
// swap height and width
DWORD dwTemp = dm.dmPelsHeight;
dm.dmPelsHeight= dm.dmPelsWidth;
dm.dmPelsWidth = dwTemp;
// determine new orientaion
switch (dm.dmDisplayOrientation)
{
case DMDO_DEFAULT:
dm.dmDisplayOrientation = DMDO_270;
break;
case DMDO_270:
dm.dmDisplayOrientation = DMDO_180;
break;
case DMDO_180:
dm.dmDisplayOrientation = DMDO_90;
break;
case DMDO_90:
dm.dmDisplayOrientation = DMDO_DEFAULT;
break;
default:
// unknown orientation value
// add exception handling here
break;
}
long lRet = ChangeDisplaySettings(&dm, 0);
if (DISP_CHANGE_SUCCESSFUL != lRet)
{
// add exception handling here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment