如何执行与更改 Windows 7 显示从横向到纵向相同的操作

How to perform same action as changing Windows 7 display from Landscape to Portrait

我需要编写一个程序,以编程方式将 Windows 中触摸屏的横向模式更改为纵向模式。分辨率为 1920 x 1080,我想我可以将 1920 切换为 1080 并使用 ChangeDisplaySettings或 ChangeDisplaySettingsEx 进行更改。但它不起作用。

Windows CE 有某种旋转功能,所以我试了一下。如:

devmode.dmFields = DM_DISPLAYORIENTATION;
devmode.dmDisplayOrientation = DMDO_90;
ChangeDisplaySettingsEx(NULL,&devmode,NULL,CDS_RESET,NULL);

但这在普通测试台式电脑上不起作用。

我可以通过“控制面板”、“显示”、“屏幕分辨率”、选择“纵向”来做到这一点,所以我不能通过 Windows API 来做到这一点吗?

我该如何更改代码?

编辑

我试过这段代码,但我得到了 ChangeDisplaySettings 返回的 DISP_CHANGE_BADMODE。

#include <Windows.h>
#include <iostream>

int main() {
DEVMODE dm;
   // initialize the DEVMODE structure
   ZeroMemory(&dm, sizeof(dm));
   dm.dmSize = sizeof(dm);

   if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
   {
      std::cout << "Orientation\n";
      switch(dm.dmDisplayOrientation) {
      case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
      case DMDO_90: std::cout << "DMDO_90\n"; break;
      case DMDO_180: std::cout << "DMDO_180\n"; break;
      case DMDO_270: std::cout << "DMDO_270\n"; break;
      }

      std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
                << "Panning width: " << dm.dmPanningWidth << '\n';

      std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";

      std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
      std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';

      if(dm.dmPelsHeight > dm.dmPelsWidth) {
          std::cout << "Screen should be rotated 90 degrees\n";

          std::cout << "Do you want to rotate display 90 degrees? (y or n)\n";
          char ch;
          std::cin >> ch;
          if(ch == 'y') {
              // swap height and width
              DWORD tmp = dm.dmPelsHeight;
              dm.dmPelsHeight = dm.dmPelsWidth;
              dm.dmPelsWidth = tmp;
              dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;

              // determine new orientation
              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 ret = ChangeDisplaySettings(&dm, CDS_TEST); //0);
              std::cout << "ChangeDisplaySettings returned " << ret << '\n';
              switch(ret) {
                case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
                case DISP_CHANGE_BADDUALVIEW: std::cout << "The settings change was unsuccessful because the system is DualView capable\n"; break;
                case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
                case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
                case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
                case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
                case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
                case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;


              }


          }

      }
      else
          std::cout << "Screen orientation is just fine\n";
   }
}

我在网上看到一篇文章说可能无法完成。但是这段代码有效。

#include <Windows.h>
#include <iostream>

int main(int argc, char* argv[]) {
   DEVMODE dm;
   // initialize the DEVMODE structure
   ZeroMemory(&dm, sizeof(dm));
   dm.dmSize = sizeof(dm);

   // only change first/default display (index=0)
   if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
   {
      std::cout << "Current orientation\n";
      switch(dm.dmDisplayOrientation) {
      case DMDO_DEFAULT: std::cout << "DMDO_DEFAULT\n"; break;
      case DMDO_90: std::cout << "DMDO_90\n"; break;
      case DMDO_180: std::cout << "DMDO_180\n"; break;
      case DMDO_270: std::cout << "DMDO_270\n"; break;
      }

      std::cout << "Panning height: " << dm.dmPanningHeight << '\n'
                << "Panning width: " << dm.dmPanningWidth << '\n';

      std::cout << "Colour resolution: " << dm.dmBitsPerPel << " bits per pixel\n";

      std::cout << "Height in pixels: " << dm.dmPelsHeight << '\n';
      std::cout << "Width in pixels: " << dm.dmPelsWidth << '\n';

      if(argc != 2) {
          std::cout << "Usage: rotate_screen <angle>. Angle values: 0, 90, 180, 270. Angles are rotation clockwise. Use 0 to revert back\n";
          exit(0);
      }

      // parse parameter
      int rotate = atoi(argv[1]);  // should do something better and safer here
      if(rotate != 0 && rotate != 90 && rotate != 180 && rotate != 270) {
          std::cout << "incorrect rotation selected\n";
      }
      else {
        // swap height and width
        DWORD tmp = dm.dmPelsHeight;
        dm.dmPelsHeight = dm.dmPelsWidth;
        dm.dmPelsWidth = tmp;

        // 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:
            std::cout << "Something went wrong, aborting\n";
            exit(0);
        }

        LONG ret = ChangeDisplaySettingsEx(NULL, &dm, NULL, 0, NULL); //CDS_RESET, NULL); //0);
        std::cout << "ChangeDisplaySettingsEx returned " << ret << '\n';
        switch(ret) {
        case DISP_CHANGE_SUCCESSFUL: std::cout << "display successfully changed\n"; break;
        case DISP_CHANGE_BADDUALVIEW: 
            std::cout << "The settings change was unsuccessful because the system is DualView capable\n"; 
            break;
        case DISP_CHANGE_BADFLAGS: std::cout << "An invalid set of flags was passed in.\n"; break;
        case DISP_CHANGE_BADMODE: std::cout << "The graphics mode is not supported.\n"; break;
        case DISP_CHANGE_BADPARAM: std::cout << "An invalid parameter was passed in. This can include an invalid flag or combination of flags.\n"; break;
        case DISP_CHANGE_FAILED: std::cout << "The display driver failed the specified graphics mode.\n"; break;
        case DISP_CHANGE_NOTUPDATED: std::cout << "Unable to write settings to the registry.\n"; break;
        case DISP_CHANGE_RESTART: std::cout << "The computer must be restarted for the graphics mode to work.\n"; break;
        }
      }
   } // enum worked
}