c++ - 如何确定应用程序正在使用哪个监视器以及如何获取它的句柄?

标签 c++ windows

我正在开发一个从主窗口调用另一个窗口的应用程序。我的问题是如何确定主应用程序窗口位于哪个监视器(如果有 2 个或更多)上以及如何获取该监视器的句柄? 到目前为止我的代码如下所示:

RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect( hDesktop, &desktop );

int width = SInt32( desktop.right / 2 );
int height = SInt32( desktop.bottom / 2 );

OpenNewWindow( width, height );

但这只是获取桌面(主显示器)的句柄,右侧和底部是主显示器的分辨率大小。 我正在用 C++ 写这个 谢谢!

最佳答案

我找到了解决方案:

        HMONITOR currentMonitor = MonitorFromWindow( GetActiveWindow(), MONITOR_DEFAULTTONEAREST );
        MONITORINFO activeMonitorInfo;
        activeMonitorInfo.cbSize = sizeof( MONITORINFO );
        GetMonitorInfo( currentMonitor, (LPMONITORINFO) &activeMonitorInfo );

        int width = SInt32( ( activeMonitorInfo.rcMonitor.right - activeMonitorInfo.rcMonitor.left ) * 0.75 );
        int height = SInt32( ( activeMonitorInfo.rcMonitor.bottom - activeMonitorInfo.rcMonitor.top ) * 0.75 );

        OpenNewWIndow( width, height );

碰巧 GetActiveWindow 返回当前事件窗口的句柄,剩下的就很简单了。

关于c++ - 如何确定应用程序正在使用哪个监视器以及如何获取它的句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875313/

相关文章:

c++ - 简单的动画导致c++中的屏幕闪烁

c++ - 是我的随机生成器不工作,还是我判断人/骷髅是否击中对手的方式有误?

windows - 使用 NVIDIA 显卡运行 ".exe"文件的命令

c++ - 我的命名空间扩展未在“文件打开”对话框中列出

c++ - 如何在 Windows 上使用 std::system 运行带空格的可执行文件

windows - 从 powershell 触发任务调度程序作业

php - 如何使用 php 从 Linux 服务器连接到 Windows 服务器中的数据库?

c++ - 没有标准库的准系统 C++?

c++ - 使用基类指针调用派生类中的非虚函数

c++ - 转发声明使用 enable_if : ambiguous call 的函数