Thread: How to save previous coordinates

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy How to save previous coordinates

    Hi!

    I want to move text "Y" with the mouse. Now my main problem is that I just can't figure it out how to save previous coordinates of the text "Y". I need these coordinates for clearing the old text "Y".
    Here's my code:
    Code:
    # include <windows.h>
    # include <stdio.h>
    # include <conio.h>
    
    void clrscr (short x, short y, short EraseLength, short NoOfLines);
    void gotoxy (short x, short y);
    BOOL SetColor (WORD Color);
    
    int main ()
    {
        HANDLE hInput;
        INPUT_RECORD ir;
        DWORD dwRead;
        BOOL Loop = TRUE, FLAG = FALSE;
        short y = 0, x = 0, xPrev = 0, yPrev = 0;
    
        hInput = GetStdHandle (STD_INPUT_HANDLE);
        clrscr (0, 0, 80, 24); // clear the whole screen
        gotoxy (0, 0);
        printf ("X");
        gotoxy (10, 6);
        printf ("Y");
        x = 10;
        y = 6;
        while (Loop)
        {
            if (ReadConsoleInput (hInput, &ir, 1, &dwRead))
            {
                if (FLAG)
                {
                    xPrev = x;
                    yPrev = y;
                    SetColor (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | BACKGROUND_RED);
                    clrscr (xPrev, yPrev, 0, 0);
                    x = ir.Event.MouseEvent.dwMousePosition.X;
                    y = ir.Event.MouseEvent.dwMousePosition.Y;
                    FLAG = FALSE;
                }
                if (ir.EventType == MOUSE_EVENT)
                {
                    switch (ir.Event.MouseEvent.dwButtonState)
                    {
                        case FROM_LEFT_1ST_BUTTON_PRESSED:
                            // exit from program
                            if ( (ir.Event.MouseEvent.dwMousePosition.Y == 0) &&
                                 (ir.Event.MouseEvent.dwMousePosition.X == 0) )
                            {
                                Loop = FALSE;
                            }
                            if ( (ir.Event.MouseEvent.dwMousePosition.X == x) &&
                                 (ir.Event.MouseEvent.dwMousePosition.Y == y) )
                            {
                                 FLAG = TRUE;
                                 gotoxy (x, y);
                                 printf ("Y");
                            }
                            break;
                    }
                }
            }
        }
        return 0;
    }
    void gotoxy (short x, short y)
    {
        COORD coord;
    
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
    }
    void clrscr (short x, short y, short EraseLength, short NoOfLines)
    {
        COORD coord;
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        HANDLE hOutput;
        short i = 0;
    
        hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
        for (i = 0;	i <= NoOfLines; i++)
        {
            coord.X	 = x;
            coord.Y	 = y+i;
            FillConsoleOutputCharacter (hOutput, TEXT (' '), EraseLength, coord, &cCharsWritten);
            GetConsoleScreenBufferInfo (hOutput, &csbi);
            FillConsoleOutputAttribute (hOutput, csbi.wAttributes, EraseLength, coord, &cCharsWritten);
        }
    }
    BOOL SetColor (WORD Color)
    {
        HANDLE hOutput;
    
        hOutput = GetStdHandle (STD_OUTPUT_HANDLE);	
        return SetConsoleTextAttribute (hOutput, Color);
    }
    Please, HELP.
    Last edited by GaPe; 06-02-2002 at 10:15 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well use another variable after changinh the value.. When the new coordinates are formed the old value is retained in the varaible... If you got the idea fine.. If not PM me i will help u..

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    Ok,

    First, declare a separate variable (or set of variables) for saving the old coodinates.

    Before, the current coordinate variable is changed, save its value in the separate variable for future use as the old coordinates...

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Yes I know that I should use another variable. That's why I have xPrev and yPrev. The problem is that I do not know where to put it.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Globaly? Outside main?
    Static?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Not that. Where to put them in the main function.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Ok pal put that as soon as you print something.... And then use the value to remove the text just before printing anything...


    So it is like this


    gotoxy(x1,y1);
    printf(" ");
    gotoxy(x,y);
    int x1=x;
    int y1=y;
    printf("Message");
    loop


    so what happens here is.. It deletes the previous message.. GOt the logicq

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I can't get it working. PLEASE HELP.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does realloc save the previous buffer data?
    By Niara in forum C Programming
    Replies: 6
    Last Post: 07-23-2008, 04:40 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. save webpage? some save some not
    By kryptkat in forum Tech Board
    Replies: 3
    Last Post: 06-07-2005, 09:21 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM