2

I want to get the position of the current window (its a console window). By current I mean the window I'm programming in, for example if the console is in the top left of the screen I should get X = 0, Y = 0. (By the position of the window I mean the top left of the window respective of the monitor)

#include <windows.h>
#include <iostream>
using namespace std;

int main(){
    int X, Y;
    GetCurrentWindowPos(&X, &Y); /* How do I do this? */
    return 0;
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Bruno
  • 77
  • 1
  • 2
  • 5
  • [`GetWindowRect`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633519(v=vs.85).aspx), [`GetConsoleWindow`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175(v=vs.85).aspx) – chris Jun 08 '14 at 22:59

1 Answers1

4
void GetWindowPos( int *x, int *y ) {
    RECT rect = { NULL };
    if( GetWindowRect( GetConsoleWindow(), &rect ) ) {
        *x = rect.left;
        *y = rect.top;
    }
}

Cheers

markhc
  • 659
  • 6
  • 15
  • Thanks! I had to do #define _WIN32_WINNT 0x0500. I actualy was lookinf for this function "GetConsoleWindow()" I didnt find how to get the handle of the console window! it helped a lot – Bruno Jun 13 '14 at 15:32