How do I write text in a specific place on the screen in a console app?
Printable View
How do I write text in a specific place on the screen in a console app?
I know gotoxy() doesn't exist in VC++, but I can't seem to get graphics.h anywhere, so is there any way to bypass this and not use gotoxy()?
Try this:
Code:#include <windows.h>
#include <iostream.h>
COORD crd;
HWND hd;
int main()
{
int x;
crd.X = 50;
crd.Y = 50;
hd = GetConsoleWindow();
SetConsoleCursorPosition(hd, crd);
cout<<"This text is on different location";
cin>>x;
return 0;
}
I does not work for me because the compiler is not recognizing "GetConsoleWindow". But Platform SDK has the function to get a handle to the console.
Try this and see if it works!
Thanks, but my VC++6 doesn't recognize this function either. :(
Any other ideas?
Z.Code:HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD myCoord = {10, 20 };
SetConsolePosition(hConsole, myCoord);
cout << "Hello!" << endl;
There is no suck function as "SetConsolePosition" as far as I know :p
Hi, I just found a little solution to that problem.
You get the handle using "GetStdHandle" function
Here is the code I used to set cursor's position:
Code:#include <iostream.h>
#include <windows.h>
int main()
{
int x;
HANDLE choutput;
COORD crd;
crd.X = 8;
crd.Y = 9;
choutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(choutput, FOREGROUND_RED | FOREGROUND_GREEN
| FOREGROUND_BLUE | FOREGROUND_INTENSITY
| BACKGROUND_BLUE);
cout<<"\t\t\t Welcome to the DOS colour show\t\t\t\t";
SetConsoleCursorPosition(choutput, crd);
cin>>x;
return 0;
}
If you dont first set the consoleTextAttribute, it does not move the cursor. I dont know why it does that but I am just playing around with that and may be I will get a better solution for that:)
abdul. You probably should have read my post a bit better, before criticizing the code. The solution you just "found" was the first line.
Z.
But it was not totally right.. was it?
As I said above that there is no such function as "SetConsolePosition"
Sure, but i found the choice of "suck", instead of "such" quite interesting.
Z.
Sometime, Unrelated words make a related word:D
Thanks for the solutions. :)
I'll check them out.
No Problem:D