5

#include<windows.h> have already added, so why the GCC-mingw32 Compiler reported that 'GetConsoleWindow' was not declared in this scope ?

Here's my code:

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

Thanks. ^^

not2qubit
  • 14,531
  • 8
  • 95
  • 135
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 2
    It is a later addition to the winapi, you need to #define _WIN32_WINNT to at least 0x500. – Hans Passant Nov 16 '13 at 10:47
  • Note that at least one MinGW distro that I have doesn't enforce the `_WIN32_WINNT >= 0x0500` requirement, so if someone else says "it works for me without setting `_WIN32_WINNT`", that might be why. – Michael Burr Nov 16 '13 at 10:55

3 Answers3

9

From msdn:

To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later.

So you can try to replace

#include<windows.h>

with

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

Or include SDKDDKVer.h from Windows SDK:

Including SDKDDKVer.h defines the highest available Windows platform.

Evgeny Timoshenko
  • 3,119
  • 5
  • 33
  • 53
6

The documentation says:

To compile an application that uses this function, define _WIN32_WINNT as 0x0500 or later.

I suspect that you did not do that.

You need to define the conditional before you include windows.h. Note that version 0x0500 corresponds to Windows 2000 so in the unlikely event that you want to support Windows NT4 or earlier, or Windows 9x, you'll need to switch to using run-time linking instead.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Or, if you get errors saying that it is redefined, you can use this :

#if       _WIN32_WINNT < 0x0500
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT   0x0500
#endif
#include <windows.h> 
Samuel
  • 315
  • 3
  • 14