簡體   English   中英

嘗試檢測監視器

[英]Trying to detect monitor

我正在嘗試獲取顯示器,以檢查是否關閉。

在使用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) 

我哪里出問題了?

您的代碼有很多問題,但是沒有一個會導致您看到錯誤消息。 這是經過一些更正的代碼,並增加了一些代碼以顯示至少某種測試結果:

#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

...作為兩種情況下的輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM