Initial experience of keyboard interaction (1) Define coordinate function

1. The effect is achieved

#include<windows.h>
#include<iostream>
using namespace std;


void gotoxy(int x,int y)//函数名使用gotoxy可以做到见名知意
{
    HANDLE n;//句柄,对象的索引
    COORD pos;
    pos.X=x;
    pos.Y=y;

    n=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(n,pos);
   
}
int main()
{
    gotoxy(33,22);
    cout<<"here"<<endl;
    return 0;
}


2. Understand the details

1. First understand the coordinate system of the windows console

Insert picture description here

2.COORD

​ COORD is a structure defined in the Windows API, which represents the coordinates of a character on the console screen

​ It is defined as:

typedef struct _COORD {
SHORT X; // horizontal coordinate
SHORT Y; // vertical coordinate
} COORD;

COORD pos in the code is the object pos that defines the coordinates of a character on the console screen

3.HANDLE

HANDLE: Handle, which is used by Windows to represent objects (not C++ objects)

In windows programs, there are various resources (windows, icons, cursors, etc.). The system allocates memory for these resources when they are created, and returns the identifier that identifies these resources, that is, the handle [1].

The handle refers to the unique index of a core object in a certain process, not a pointer.

4.GetStdHandle

GetStdHandle is a Windows API function. It is used to obtain a handle (used to identify the value of different devices) from a specific standard device (standard input, standard output, or standard error). Can be used in nesting.

Insert picture description here

5.SetConsoleCursorPosition

SetConsoleCursorPosition is a window api; the function is to set the console (cmd) cursor position

SetConsoleCursorPosition(n,pos);
    /*定位光标位置的函数,坐标为GetStdHandle()返回标准的输出的句柄,也就是获得输出屏幕缓冲区的句柄,并赋值给对象pos*/

Three. Experience

For the freshman year project of the school, I plan to use Qt to implement a game, but it takes dozens of hours and dozens of hours of tutorials on the Internet to be a headache and daunting. If you need to squeeze out a lot of time every day for gradual learning, use the intermediate breakthrough. The learning method, first get started, see the results, and then learn the details of grammar, you can more quickly master the knowledge and applications required for the project, and improve the learning efficiency

Guess you like

Origin blog.csdn.net/qq_52431436/article/details/115018178