Screen capture fails to capture whole screen using C++ and GDI

≯℡__Kan透↙ 提交于 2019-12-05 14:38:52

If the computer is on high DPI settings, and the application is not DPI aware, then the system will lie to the application and give the wrong screen size.

You will find the following code shows width and height which are smaller than the actual screen size:

uint32_t Width = GetSystemMetrics(SM_CXSCREEN);
uint32_t Height = GetSystemMetrics(SM_CYSCREEN);
std::cout << Width << " x " << Height << "\n";

The solution is to add DPI awareness to application.

To add DPI compatibility:

In Visual Studio 2015, go to Project Properties -> Manifest tools, set DPI awareness to "Per Monitor High DPI Aware" or "High DPI Aware"

If you are using some old compiler...

1) create a file "myapp.manifest" with this content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
    </windowsSettings>
  </application>
</assembly>

2) Add *.rc file to your project with this content:

1 24 "myapp.manifest"

3) Rebuild the project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!