“GetConsoleWindow”在此范围内未声明?

5

#include<windows.h>已经添加,为什么GCC-mingw32编译器报告'GetConsoleWindow' was not declared in this scope

以下是我的代码:

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

using namespace std;

#define PI 3.14

int main() 
{
    //Get a console handle
    HWND myconsole = GetConsoleWindow();
    //Get a handle to device context
    HDC mydc = GetDC(myconsole);

    int pixel =0;

    //Choose any color
    COLORREF COLOR= RGB(255,255,255); 

    //Draw pixels
    for(double i = 0; i < PI * 4; i += 0.05)
    {
        SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
        pixel+=1;
    }

    ReleaseDC(myconsole, mydc);
    cin.ignore();
    return 0;
}

感谢您。^^

2
这是WinAPI的后期添加,您需要将#define _WIN32_WINNT至少设置为0x500。 - Hans Passant
请注意,我拥有的至少一个MinGW发行版不强制执行“_WIN32_WINNT >= 0x0500”的要求,因此如果其他人说“没有设置'_WIN32_WINNT'也可以正常工作”,那可能就是原因。 - Michael Burr
3个回答

9

来自 msdn:

要编译使用此函数的应用程序,请将 _WIN32_WINNT 定义为 0x0500 或更高版本。

因此,您可以尝试替换

#include<windows.h>

使用

#define _WIN32_WINNT 0x0500
#include<windows.h>

或者从Windows SDK中包含SDKDDKVer.h

包含SDKDDKVer.h定义了可用的最高版本Windows平台。


6

文档中说:

为了编译使用此函数的应用程序,请将_WIN32_WINNT定义为0x0500或更高版本。

我怀疑你没有这样做。

在包含windows.h之前,您需要定义条件。请注意,版本0x0500对应于Windows 2000,因此如果您想支持Windows NT4或更早版本,或Windows 9x,则需要切换到使用运行时链接。


1

或者,如果你收到“它已被重新定义”的错误提示,可以使用以下代码:

#if       _WIN32_WINNT < 0x0500
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT   0x0500
#endif
#include <windows.h> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接