在屏幕上定位光标
Positioning the Cursor on the Screen
已经做了好几个小时了,还是不知道怎么把它变成我想要的样子。
我需要它在控制台输出中居中,像这样…
*******************************************
ABC Industries
Report
*******************************************
相反,它是这样出来的…
**********************************
ABC Industries
Report
**********************************
这是我到目前为止得到的,任何帮助都将非常感激。
#include <string>
#include <iomanip>
#include <iostream>
#include <windows.h>
#include <cctype>
using namespace std;
class Heading
{
private:
string companyName;
string reportName;
public:
Heading();
void placeCursor(HANDLE, int, int);
void printStars(int);
void getData(HANDLE, Heading&);
void displayReport(HANDLE, Heading);
};
int main()
{
Heading display;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
display.getData(screen, display);
display.displayReport(screen, display);
cin.get();
return 0;
}
Heading::Heading()
{
companyName = "ABC Industries";
reportName = "Report";
}
void Heading::placeCursor(HANDLE screen, int row, int col)
{
COORD position;
position.Y = row;
position.X = col;
SetConsoleCursorPosition(screen, position);
}
void Heading::printStars(int n)
{
for(int star=1; star<=n; star++)
cout << '*';
cout <<endl;
}
void Heading:: getData(HANDLE screen, Heading &input)
{
string str;
placeCursor(screen, 2, 5);
cout <<"Enter company name";
placeCursor(screen, 2, 26);
getline(cin, str);
if(str!="")
input.companyName = str;
placeCursor(screen, 4, 5);
cout<<"enter report name";
placeCursor(screen, 4, 26);
getline(cin, str);
if(str!="")
input.reportName = str;
}
void Heading::displayReport(HANDLE screen, Heading input)
{
int l;
placeCursor(screen, 8, 5);
printStars(69);
string str=input.companyName;
l= str.length();
l=39-l/2;
placeCursor(screen, 9, 1);
cout << str;
str=input.reportName;
l=str.length();
l=39-l/2;
placeCursor(screen, 10, 1);
cout << str;
placeCursor(screen, 11, 5);
printStars(69);
}
你会后悔的
注意,在Heading::displayReport中,您计算公司名称的长度,然后将其除以2,再减去39。然后将光标定位到9,1(9,1)。
您要做的是将光标位置设置为9,l (9, el)。
然后您也对reportName字段重复监督。; p
那么,让这两个改变
// placeCursor(screen, 9, 1);
placeCursor(screen, 9, l);
和
// placeCursor(screen, 10, 1);
placeCursor(screen, 10, l);
相关文章:
- 如何在Qt C++中更改光标
- 重新定位图像时如何前进到下一个内存块
- 流填充字符的默认定位
- Win32 API 控制台光标在 WriteConsole 后不移动
- 通过Python Distutils(用于Python C扩展)使用可重定位的设备代码编译CUDA代码
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- NS3 - python.h 文件无法定位编译错误
- 设置我的应用程序 API 感知并防止系统使其模糊和错误定位
- 如何更改输出的位置或光标在输出处,以便在"Code Blocks"中C++
- 使用 C++将文本光标移动到屏幕中不可见的行
- 在 C# 中更改鼠标光标图标
- ASCII 旋转光标 (TUI) 动画出现问题
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- GLEW 未在 MSYS2 上定位 OpenGL 函数
- 将光标单独移动到当前位置
- 如何在C++中使用光标定位
- 如果用户没有输入数据,我如何重新定位光标
- 将光标定位在RichEdit控件的文本末尾
- 在屏幕上定位光标