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.