"GetConsoleWindow"未在此范围内声明?

'GetConsoleWindow' was not declared in this scope?

本文关键字:声明 范围内 GetConsoleWindow      更新时间:2023-10-16

#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;
}

谢谢。^^

来自 msdn:

若要编译使用此函数的应用程序,请定义_WIN32_WINNT 作为0x0500或更晚。

所以你可以尝试更换

#include<windows.h>

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

或者包含来自 Windows SDK 的SDKDDKVer.h

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

文档说:

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

我怀疑你没有这样做。

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

或者,如果您收到错误说它已重新定义,则可以使用以下命令:

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