Thread: find console coords

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    7

    find console coords

    hi everyone, my name is Atique and i am new to the forum so my regards to all, i'm working in vc++ 6.0 and i need a function that gives the current coordinates of the console window.
    thanks for your precious time.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In C there is ncurses library.In C++ i do not anything that answer your question.So i suggest you to write a class in order to wrap this C library.Then you can handle it as an object and that's it However i have never tried it

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    that's a bit of an ambiguous question.

    do you want the coordinates of the cursor in the window?
    do you want the coordinates of the window itself?
    did you not notice the windows programming forum, which would have been a more appropriate place for this question?

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    I think you want the GetCursorPos function find console coords-verde-gif:


    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(void)
    {
        LPPOINT Coord;
        while(1)
        {
            GetCursorPos(Coord);
            printf("X: %d   Y: %d\n", Coord->x, Coord->y);
            Sleep(1000);
        }
        //return 0;
    }


    I hope that I have helped find console coords-smiled-gif.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Boom -> seg fault
    better would be
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
        POINT Coord;
        while(1)
        {
            GetCursorPos(& Coord);
            printf("X: %d   Y: %d\n", Coord.x, Coord.y);
            Sleep(1000);
        }
        //return 0;
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    thanks a lot everyone for such a good response,

    Elkvis i want the windows coordinates itself, i was a little reluctant from that windows forum because i know nothing about windows programming,

    Actually i am making a chess game which uses a lot of bitmaps, and is handled with mouse, since i am not aware of windows programming so the game involves a little hard coded stuff, because of that i have to move the console-window to the corner otherwise it creates problem, so i need a function that tells the current window position, i would subtract it from the mouse cords and the problem is solved.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better would be,
    std::cout << "X: " << Coord.x << " Y: " << Coord.y << "\n";
    Since this is C++, after all.

    Anyway, I recommend you ditch this approach and VC++ 6 (why do you use such an old version anyway?) and focus your efforts on finding a GUI library instead.
    Noone wants to use a console for a game.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Noone wants to use a console for a game.
    Is this is a bad time for a "PS3 has no games" joke?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    GetCursorPos is not even the right API. That's for moving the thing that your mouse controls.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What?
    GetCursorPos gets the current coordinates where the mouse (pointer) is.
    SetCursorPos moves the mouse (pointer).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I believe that the intent was to say that `GetConsoleScreenBufferInfo' and `SetConsoleCursorPosition' would be needed for the console cursor instead of `GetCursorPos' and `SetCursorPos' which is for the mouse cursor.

    Soma

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    but the OP said

    Quote Originally Posted by atique View Post
    i want the windows coordinates itself
    I interpret this to mean the window's position on screen, but of course I could be misunderstanding. atique appears to be a non-native english speaker, so it could be very easy to misunderstand.

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    thanks a lot everyone for such a good response, i have just got the answer form that windows programming forum ' thanks to Elkvis '.
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HWND conwin = GetConsoleWindow();
        RECT conrect;
        if (!conwin || !GetWindowRect(conwin, &conrect))
        {
            cerr << "Error" << endl;
            return 1;
        }//if
    
        cout << "(" << conrect.left << "," << conrect.top << ")-("
             << conrect.right << "," << conrect.bottom << ")" << endl;
        
        int width = conrect.right - conrect.left;
        int height = conrect.bottom - conrect.top;
        cout << "W=" << width << endl;
        cout << "H=" << height << endl;
        
        cout << "Moving to 0,0...." << endl;
        MoveWindow(conwin, 0, 0, width, height, TRUE);
        
        return 0;
    }//main

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well it appears you asked the wrong question then because it appears that all you wanted was to know how to move the window to the top-left corner of your screen.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    Quote Originally Posted by iMalc View Post
    Well it appears you asked the wrong question then because it appears that all you wanted was to know how to move the window to the top-left corner of your screen.
    i'm sorry iMalc if it appeared so but i asked for current windows cords
    and the function
    GetWindowRect() gave me the answer.
    The
    MoveWindow() function was just an addtional treat,anyways i'm new here so my question my have been confusing, thanks for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get child's x, y coords?
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 11-19-2007, 05:54 AM
  2. 3D to 2D coords....
    By ellis in forum Game Programming
    Replies: 10
    Last Post: 10-18-2006, 11:18 AM
  3. How to find the coords of an image map?
    By Geo-Fry in forum Tech Board
    Replies: 3
    Last Post: 09-08-2003, 05:54 PM
  4. Using X,Y Coords in DOS Console
    By BigSter in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2001, 08:39 AM

Tags for this Thread