Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. Change windows Orientation using C++ code
Forum Update on Monday, May 27th 2025

Change windows Orientation using C++ code

Scheduled Pinned Locked Moved Unsolved Showcase
2 Posts 2 Posters 439 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    keksi venksi
    wrote on 26 Aug 2024, 15:28 last edited by
    #1
    void MainAppWindow::setLayoutOrientation(int rotate)
    {
        // initialize the DEVMODE structure
        DEVMODE dm;
        ZeroMemory(&dm, sizeof(dm));
        dm.dmSize = sizeof(dm);
    
        // only change first/default display (index=0)
        if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
        {
           LOG_INFO("Current orientation");
           switch(dm.dmDisplayOrientation)
           {
                case DMDO_DEFAULT:
                    LOG_INFO("DMDO_DEFAULT");
                    break;
                case DMDO_90:
                    LOG_INFO("DMDO_90");
                    break;
                case DMDO_180:
                    LOG_INFO("DMDO_180");
                    break;
                case DMDO_270:
                    LOG_INFO("DMDO_270");
                    break;
           }
    
    
           // Determine the current orientation
           int currentOrientation = dm.dmDisplayOrientation;
           int newOrientation;
    
           // Set the new orientation
           switch (rotate)
           {
               case 0:
                   newOrientation = DMDO_DEFAULT;
                   break;
               case 90:
                   newOrientation = DMDO_90;
                   break;
               case 180:
                   newOrientation = DMDO_180;
                   break;
               case 270:
                   newOrientation = DMDO_270;
                   break;
               default:
                   LOG_INFO("Something went wrong, aborting");
                   return;
           }
    
           LOG_INFO("Panning height     : " + QString::number(dm.dmPanningHeight) +  " Panning width: " + QString::number(dm.dmPaperWidth) );
           LOG_INFO("Colour resolution  : " + QString::number(dm.dmBitsPerPel) + " bits per pixel");
           LOG_INFO("Height in pixels   : " + QString::number(dm.dmPelsHeight));
           LOG_INFO("Width in pixels    : " + QString::number(dm.dmPelsWidth));
    
           // parse parameter
           if(rotate != 0 && rotate != 90 && rotate != 180 && rotate != 270)
           {
               LOG_INFO("incorrect rotation selected");
           }
           else
           {
    
               if ((currentOrientation == DMDO_DEFAULT && newOrientation == DMDO_90) ||
                       (currentOrientation == DMDO_90 && newOrientation == DMDO_DEFAULT) ||
                       (currentOrientation == DMDO_DEFAULT && newOrientation == DMDO_270) ||
                       (currentOrientation == DMDO_270 && newOrientation == DMDO_DEFAULT))
               {
                   // swap height and width
                   DWORD tmp = dm.dmPelsHeight;
                   dm.dmPelsHeight = dm.dmPelsWidth;
                   dm.dmPelsWidth = tmp;
               }
    
             LOG_INFO("Height in pixels after swapping : " + QString::number(dm.dmPelsHeight));
             LOG_INFO("Width in pixels after swapping : " + QString::number(dm.dmPelsWidth));
    
             // select fields which have changed
             dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYORIENTATION;
    
             // set angle
             switch(rotate)
             {
                 case 0:
                     dm.dmDisplayOrientation = DMDO_DEFAULT;
                     break;
                 case 90:
                     dm.dmDisplayOrientation = DMDO_90;
                     break;
                 case 180:
                     dm.dmDisplayOrientation = DMDO_180;
                     break;
                 case 270:
                     dm.dmDisplayOrientation = DMDO_270;
                     break;
                 default:
                     LOG_INFO("Something went wrong, aborting");
                     exit(0);
             }
    
             LONG ret = ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL); //CDS_RESET, NULL); //0);
             LOG_INFO("ChangeDisplaySettingsEx returned " + QString::number(ret));
             switch(ret)
             {
                 case DISP_CHANGE_SUCCESSFUL:
                     LOG_INFO("display successfully changed");
                     break;
                 case DISP_CHANGE_BADDUALVIEW:
                     LOG_INFO("The settings change was unsuccessful because the system is DualView capable");
                     break;
                 case DISP_CHANGE_BADFLAGS:
                     LOG_INFO("An invalid set of flags was passed in.");
                     break;
                 case DISP_CHANGE_BADMODE:
                     LOG_INFO("The graphics mode is not supported.");
                     break;
                 case DISP_CHANGE_BADPARAM:
                     LOG_INFO("An invalid parameter was passed in. This can include an invalid flag or combination of flags.");
                     break;
                 case DISP_CHANGE_FAILED:
                     LOG_INFO("The display driver failed the specified graphics mode.");
                     break;
                 case DISP_CHANGE_NOTUPDATED:
                     LOG_INFO("Unable to write settings to the registry.");
                     break;
                 case DISP_CHANGE_RESTART:
                     LOG_INFO("The computer must be restarted for the graphics mode to work.");
                     break;
             }
           }
        }
    }
    

    Hi All , I have done the orientation chnage successfully with the above code. I hope it may help some one.

    Note : I handled all four possibilties which windows provides -portrait -portrait (flipped) -landscape _landscape (flipped)

    we just need to call the method with the respective angle Landscape - 0 Portait - 90 landscape (flipped) - 180 portrait (flipped) - 270

    P 1 Reply Last reply 26 Aug 2024, 15:38
    0
    • K keksi venksi
      26 Aug 2024, 15:28
      void MainAppWindow::setLayoutOrientation(int rotate)
      {
          // initialize the DEVMODE structure
          DEVMODE dm;
          ZeroMemory(&dm, sizeof(dm));
          dm.dmSize = sizeof(dm);
      
          // only change first/default display (index=0)
          if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
          {
             LOG_INFO("Current orientation");
             switch(dm.dmDisplayOrientation)
             {
                  case DMDO_DEFAULT:
                      LOG_INFO("DMDO_DEFAULT");
                      break;
                  case DMDO_90:
                      LOG_INFO("DMDO_90");
                      break;
                  case DMDO_180:
                      LOG_INFO("DMDO_180");
                      break;
                  case DMDO_270:
                      LOG_INFO("DMDO_270");
                      break;
             }
      
      
             // Determine the current orientation
             int currentOrientation = dm.dmDisplayOrientation;
             int newOrientation;
      
             // Set the new orientation
             switch (rotate)
             {
                 case 0:
                     newOrientation = DMDO_DEFAULT;
                     break;
                 case 90:
                     newOrientation = DMDO_90;
                     break;
                 case 180:
                     newOrientation = DMDO_180;
                     break;
                 case 270:
                     newOrientation = DMDO_270;
                     break;
                 default:
                     LOG_INFO("Something went wrong, aborting");
                     return;
             }
      
             LOG_INFO("Panning height     : " + QString::number(dm.dmPanningHeight) +  " Panning width: " + QString::number(dm.dmPaperWidth) );
             LOG_INFO("Colour resolution  : " + QString::number(dm.dmBitsPerPel) + " bits per pixel");
             LOG_INFO("Height in pixels   : " + QString::number(dm.dmPelsHeight));
             LOG_INFO("Width in pixels    : " + QString::number(dm.dmPelsWidth));
      
             // parse parameter
             if(rotate != 0 && rotate != 90 && rotate != 180 && rotate != 270)
             {
                 LOG_INFO("incorrect rotation selected");
             }
             else
             {
      
                 if ((currentOrientation == DMDO_DEFAULT && newOrientation == DMDO_90) ||
                         (currentOrientation == DMDO_90 && newOrientation == DMDO_DEFAULT) ||
                         (currentOrientation == DMDO_DEFAULT && newOrientation == DMDO_270) ||
                         (currentOrientation == DMDO_270 && newOrientation == DMDO_DEFAULT))
                 {
                     // swap height and width
                     DWORD tmp = dm.dmPelsHeight;
                     dm.dmPelsHeight = dm.dmPelsWidth;
                     dm.dmPelsWidth = tmp;
                 }
      
               LOG_INFO("Height in pixels after swapping : " + QString::number(dm.dmPelsHeight));
               LOG_INFO("Width in pixels after swapping : " + QString::number(dm.dmPelsWidth));
      
               // select fields which have changed
               dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYORIENTATION;
      
               // set angle
               switch(rotate)
               {
                   case 0:
                       dm.dmDisplayOrientation = DMDO_DEFAULT;
                       break;
                   case 90:
                       dm.dmDisplayOrientation = DMDO_90;
                       break;
                   case 180:
                       dm.dmDisplayOrientation = DMDO_180;
                       break;
                   case 270:
                       dm.dmDisplayOrientation = DMDO_270;
                       break;
                   default:
                       LOG_INFO("Something went wrong, aborting");
                       exit(0);
               }
      
               LONG ret = ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL); //CDS_RESET, NULL); //0);
               LOG_INFO("ChangeDisplaySettingsEx returned " + QString::number(ret));
               switch(ret)
               {
                   case DISP_CHANGE_SUCCESSFUL:
                       LOG_INFO("display successfully changed");
                       break;
                   case DISP_CHANGE_BADDUALVIEW:
                       LOG_INFO("The settings change was unsuccessful because the system is DualView capable");
                       break;
                   case DISP_CHANGE_BADFLAGS:
                       LOG_INFO("An invalid set of flags was passed in.");
                       break;
                   case DISP_CHANGE_BADMODE:
                       LOG_INFO("The graphics mode is not supported.");
                       break;
                   case DISP_CHANGE_BADPARAM:
                       LOG_INFO("An invalid parameter was passed in. This can include an invalid flag or combination of flags.");
                       break;
                   case DISP_CHANGE_FAILED:
                       LOG_INFO("The display driver failed the specified graphics mode.");
                       break;
                   case DISP_CHANGE_NOTUPDATED:
                       LOG_INFO("Unable to write settings to the registry.");
                       break;
                   case DISP_CHANGE_RESTART:
                       LOG_INFO("The computer must be restarted for the graphics mode to work.");
                       break;
               }
             }
          }
      }
      

      Hi All , I have done the orientation chnage successfully with the above code. I hope it may help some one.

      Note : I handled all four possibilties which windows provides -portrait -portrait (flipped) -landscape _landscape (flipped)

      we just need to call the method with the respective angle Landscape - 0 Portait - 90 landscape (flipped) - 180 portrait (flipped) - 270

      P Offline
      P Offline
      Pl45m4
      wrote on 26 Aug 2024, 15:38 last edited by
      #2

      @keksi-venksi

      Hi,
      nice,
      maybe you want to move this topic to the Showcase category as it is not a question or an issue, but already a solution you want to share.

      • https://forum.qt.io/category/8/showcase

      (Or you wait until some @moderators might move it)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • SGaistS SGaist moved this topic from General and Desktop on 26 Aug 2024, 18:24

      1/2

      26 Aug 2024, 15:28

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved