0

我想做的是获取我的屏幕信息,如分辨率/刷新率等:

#pragma once
#include < windows.h >
#include <string>
#include <sstream>

struct DesktopScreenInfo
{
    int Width;
    int Height;
    int ScreenDepth;
    int FrameRate;
    std::string ScreenInfoString;
};

class DesktopScreen
{
public:
    DesktopScreen(void);
    ~DesktopScreen(void);

    DesktopScreenInfo GetScreenInfo();
private:
    DISPLAY_DEVICE GetPrimaryDevice();
};

#include "DesktopScreen.h"

DesktopScreen::DesktopScreen(void)
{
}

DesktopScreen::~DesktopScreen(void)
{
}

DISPLAY_DEVICE DesktopScreen::GetPrimaryDevice(){
    int index=0;
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(DISPLAY_DEVICE);

    while (EnumDisplayDevices(NULL, index++, &dd, 0))
    {
        if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) return dd;
    }
    return dd;
}

DesktopScreenInfo DesktopScreen::GetScreenInfo(){
    DISPLAY_DEVICE dd = GetPrimaryDevice();
    DesktopScreenInfo info;
    DEVMODE dm;
    dm.dmSize = sizeof(DEVMODE);

    if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
    {
        //printf("EnumDisplaySettings failed:%d\n", GetLastError());
        return info;
    }
    info.Width = dm.dmPelsWidth;
    info.Height = dm.dmPelsHeight;
    info.ScreenDepth = dm.dmBitsPerPel;
    info.FrameRate = dm.dmDisplayFrequency;
    std::stringstream ss;
    ss << info.Width << "x" << info.Height << ":" << info.ScreenDepth << "@" << info.FrameRate;
    info.ScreenInfoString = ss.str();
}

但是当我尝试构建它时,它给了我这个错误

Error   2   error LNK2019: unresolved external symbol __imp__EnumDisplayDevicesA@16 referenced in function "private: struct _DISPLAY_DEVICEA __thiscall DesktopScreen::GetPrimaryDevice(void)" (?GetPrimaryDevice@DesktopScreen@@AAE?AU_DISPLAY_DEVICEA@@XZ)
Error   3   error LNK2019: unresolved external symbol __imp__EnumDisplaySettingsA@12 referenced in function "public: struct DesktopScreenInfo __thiscall DesktopScreen::GetScreenInfo(void)" (?GetScreenInfo@DesktopScreen@@QAE?AUDesktopScreenInfo@@XZ)

我不知道出了什么问题:S

4

2 回答 2

2

您需要与 链接user32.lib

这些 API 的文档会在页面底部告诉您要链接到哪个库。)

于 2012-08-22T17:00:28.100 回答
0

它找不到 EnumDisplayDevices 或 EnumDisplaySettings。

弄清楚这些是在哪里定义的,如果你在 .h 中定义了它们,请确保包括在内。我没有看到任何 #include "EnumDisplayDevices.h" 或类似的东西,并且使用您发布的代码,我没有看到声明的枚举。

无论如何,由于链接器不知道 EnumDisplayDevices 或 EnumDisplaySettings 声明的位置,因此导致错误,因此无法正确链接它。

于 2012-08-22T17:03:14.220 回答