以编程方式启用显示

发布于 2024-12-08 15:44:59 字数 1509 浏览 0 评论 0原文

我正在尝试在 C++ 中启用辅助监视器。我似乎尝试更改显示设置,但什么也没发生,有人能告诉我哪里出了问题吗?

    std::wstring devName( L"Intel(R) HD Graphics Family" );
    std::wstring dispName( L"\\\\.\\DISPLAY3" );

    DISPLAY_DEVICE theDisplay;
    theDisplay.cb = sizeof(theDisplay);
    DWORD dev = 0;

    while(EnumDisplayDevices(0, dev, &theDisplay, 0))
    {
        if (devName.compare(theDisplay.DeviceString) == 0 && dispName.compare(theDisplay.DeviceName) == 0)
        {
            // found display adapter we're looking for
            if (theDisplay.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
            {
                // Display is part of desktop, turn al other monitors off
                cout << "Display is part of desktop\n";
            }
            else
            {
                // Display is off, turn it on
                DEVMODE dm;

                memset(&dm,0,sizeof(DEVMODE));
                dm.dmSize = sizeof (DEVMODE);
                dm.dmFields = DM_POSITION;
                dm.dmPosition.x = 3361;
                dm.dmPosition.y = 0;
                dm.dmPelsWidth = 1920;
                dm.dmPelsHeight = 1080;

                LONG ret = ChangeDisplaySettingsEx (theDisplay.DeviceName, &dm, NULL, CDS_UPDATEREGISTRY, NULL);
                if (ret != DISP_CHANGE_SUCCESSFUL)
                {
                    cout << "failed";
                }
            }
        }
        dev++;
    }

    system ("pause");
    return 0;

I am trying to enable a secondary monitor in C++. What I have seems to try and change the display settings but nothing really happens, can anyone tell me where I am going wrong?

    std::wstring devName( L"Intel(R) HD Graphics Family" );
    std::wstring dispName( L"\\\\.\\DISPLAY3" );

    DISPLAY_DEVICE theDisplay;
    theDisplay.cb = sizeof(theDisplay);
    DWORD dev = 0;

    while(EnumDisplayDevices(0, dev, &theDisplay, 0))
    {
        if (devName.compare(theDisplay.DeviceString) == 0 && dispName.compare(theDisplay.DeviceName) == 0)
        {
            // found display adapter we're looking for
            if (theDisplay.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
            {
                // Display is part of desktop, turn al other monitors off
                cout << "Display is part of desktop\n";
            }
            else
            {
                // Display is off, turn it on
                DEVMODE dm;

                memset(&dm,0,sizeof(DEVMODE));
                dm.dmSize = sizeof (DEVMODE);
                dm.dmFields = DM_POSITION;
                dm.dmPosition.x = 3361;
                dm.dmPosition.y = 0;
                dm.dmPelsWidth = 1920;
                dm.dmPelsHeight = 1080;

                LONG ret = ChangeDisplaySettingsEx (theDisplay.DeviceName, &dm, NULL, CDS_UPDATEREGISTRY, NULL);
                if (ret != DISP_CHANGE_SUCCESSFUL)
                {
                    cout << "failed";
                }
            }
        }
        dev++;
    }

    system ("pause");
    return 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文