Place "text or image" in a position on the screen?

How can I position a text or image anywhere on the screen?
I'm using Visual Studio C ++!
Last edited on
@rhdd

Here is a small program I wrote using a gotoXY command, that will place text at a desired location, and allow for it to appear to move, if desired.

Some people don't like using conio.h header, so you can just leave the waitkey function out and use a cin command to halt the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// gotoXY Demo.cpp : main project file.

#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y); 

void WaitKey();

int main()
{
   int x = 1;// Column 1
   int y = 5;// Row 5
   // Start position of the text 

   gotoXY(24,2); // Placing title
   cout <<   "gotoXY(x,y) Demonstration..";

   for (x=1;x<27;x++)  // Moves the text to the right
   {
      gotoXY(x,y);
      cout << "* column " << x <<  ", row " << y << "!";

      Sleep(200);
      gotoXY(x,y); // Locate the asterisk
	  cout << " "; // Remove it from screen, so we don't have a trail of asterisks
   }
   x--; // Remove 1 from x, so we start where the text is now located

   for (y=5;y<23;y++) // Moves the text down the screen
   {
      Sleep(200);
      gotoXY(x,y);
	  cout << "                                   "; // Remove the line of text
      Sleep(200);
      gotoXY(x,y+1);
      cout << "* column " << x << ", row " << y+1 << "!"; // Print new text line
   }
   Sleep(800);
   for (y=23;y>8;y--) // Moves text up the screen to row 8
   {
      Sleep(200);
      gotoXY(x,y);
	  cout << "                                   ";
      Sleep(200);
      gotoXY(x,y-1);
      cout << "* column " << x << ", row " << y-1 << "!";
   }

   Sleep(1000); // Program finished, wait 1 second
   gotoXY(23,24);
   cout << "Press any key to continue . . ."; // Let user know we're done
   gotoXY(23,24); // Cursor to blink on the letter 'P' of 'Press.. ', above
   WaitKey();  // Wait for a keypress
   return 0;
}

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; // Locates column
CursorPosition.Y = y; // Locates Row
SetConsoleCursorPosition(console,CursorPosition); // Sets position for next thing to be printed 
}

void WaitKey()
{
   while (_kbhit()) _getch(); // Empty the input buffer
   _getch(); // Wait for a key
   while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
Last edited on
nothing about like or dislike, its simply that some of these tools are not available on all systems. If you are not on windows, doing this needs completely different headers. Ncurses is available on almost everything, and is generally favored over the windows only tools, but I personally use getch & gotoxy in my windows code, they work fine if you are on that platform.

the c++ standards folks really, really need to make getch moved into the official tools, or something like it.
Last edited on
@jonnin

I've heard many people rave about Ncurses, but, even after several attempts, have never been able to get it to work with my MS Visual Studios 2012. I got so frustrated, I just quit trying to do it anymore.
pdcurses is the Windows version of curses.

https://pdcurses.org/
@Furry Guy

Thanks for the URL. Yes, that's the one I've tried installing (lower version number though), but I've never used the make command, nor do I know how to. I build program, console only, with the library files my compiler comes with. Guess I'll just stick with what I know. Thanks anyway.
Topic archived. No new replies allowed.