试图检测监视器

时间:2013-11-08 16:17:42

标签: c++ winapi

我正试图让显示器检查是否关闭。

在使用GetDevicePowerState核对之前,我正试图以这种方式检索监视器:

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winuser.h>
using namespace std;

int main(int argc, char *argv[])
{
    POINT* p = new POINT;
    p->x=0;
    p->y=0;
    HMONITOR* monitor = MonitorFromPoint(p,DWORD.MONITOR_DEFAULTTOPRIMARY);
    system("PAUSE");
    return EXIT_SUCCESS;
}

但它不断给我:

main.cpp `MonitorFromPoint' undeclared (first use this function) 

我哪里出错?

1 个答案:

答案 0 :(得分:7)

您的代码存在许多问题,但它们都不会导致您看到的错误消息。这里的代码有一些修正,还有一些补充,以显示至少某种测试结果:

#include <iostream>
#include <windows.h>

int main(int argc, char *argv[])
{
    POINT p{ 0, 0 };
    HMONITOR monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);

    if (monitor == NULL) 
        std::cout << "No monitor found for point (0, 0)\n";
    else {
        MONITORINFOEX info;
        info.cbSize = sizeof(info);

        GetMonitorInfo(monitor, &info);
        std::cout << "Monitor: " << info.szDevice << "\n";
    }
}

我已经用VC ++ 2013和MinGW 4.8.1对它进行了测试,在这两种情况下它的编译和运行没有任何问题,产生:

Monitor: \\.\DISPLAY1

...在两种情况下都是它的输出。