#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=100;i++)
cout<<"字符";
return 0;
}
求解怎么用easyX循环输出文字。效果和上面C++的一样,而不是让字符动起来
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=100;i++)
cout<<"字符";
return 0;
}
求解怎么用easyX循环输出文字。效果和上面C++的一样,而不是让字符动起来
想要实现 cout<<"字符"; 的效果,可以自己写一个函数。例如可以这么做:
// 实现类似控制台样式的字符串输出
// 编译环境:VC2010、VC2019,EasyX_20200315(beta)
//
#include <graphics.h>
#include <conio.h>
void mycout(wchar_t* s)
{
static int x = 0, y = 0;
for (unsigned int i = 0; i < _tcslen(s); i++)
{
if (s[i] == L'\n')
x = 0, y += textheight(L'A');
else
{
int w = textwidth(s[i]);
if (x + w > 639)
x = 0, y += textheight(s[i]);
outtextxy(x, y, s[i]);
x += w;
if (x > 635)
x = 0, y += textheight(s[i]);
}
}
}
int main()
{
// 创建图形窗口
initgraph(640, 480);
settextstyle(16, 0, L"宋体");
mycout(L"123测试45");
mycout(L"abc\n");
mycout(L"OK");
_getch();
// 关闭绘图窗口
closegraph();
return 0;
}
补充:
关于中文的处理:
对于 MBCS 编码,这时候一个汉字会被分成两个值为 161~254 的字符,需要判断先后两个字节,来判断是否是中文,以便决定输出一个字符,还是一个长度为 2 的字符串。
对于 Unicode 编码,这就简单多了,不管中文还是英文,都是一个 wchar_t 字符,以上代码完全没问题。
稍微封装了一下,目前看来没有什么bug(除了用一些稀奇古怪的字体),用微软雅黑会出现玄学bug,懒得排了,不用就行
econsole.h:懒得分开写,毕竟就这点码量
#pragma once
#include<easyx.h>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
class EConsole {
private:
RECT board;//相对于窗口的边框坐标
int width, height;//控制台能容纳的字符行列数(建议用等宽字符,否则会出奇奇怪怪的错误,比如微软雅黑)
POINT cursor;//控制台光标位置
TCHAR** buffer;//缓冲区,一个滚动数组
int start;//滚动数组的起始行
//滚动
void Scroll() {
if (++cursor.x == height)cursor.x = 0;
memset(buffer[cursor.x], 0, sizeof(TCHAR) * width);
if (cursor.x == start && ++start == height)start = 0;
}
public:
bool autoflush = true;//用于批量打印
EConsole(RECT _board) {
board = _board;
LOGFONT font;
ZeroMemory((void*)&font, sizeof font);
font.lfCharSet = GB2312_CHARSET;//如果遇到想要的字体显示不出来可以试试注释掉这句,原理未知
font.lfHeight = 24;
font.lfWeight = FW_NORMAL;
const TCHAR* fontname = L"Microsoft YaHei Mono";
wcscpy_s(font.lfFaceName, fontname);
settextstyle(&font);
width = (board.right - board.left) / textwidth(L'A');
height = (board.bottom - board.top) / textheight(L'A');
cursor = POINT{ 0,0 };
buffer = new TCHAR * [height];
for (int i = 0; i < height; i++) {
buffer[i] = new TCHAR[width];
memset(buffer[i], 0, sizeof(TCHAR) * width);
}
start = 0;
}
//清空缓冲区
void Clear() {
for (int i = 0; i < height; i++)memset(buffer[i], 0, sizeof(TCHAR) * width);
cursor = { 0,0 };
start = 0;
}
//刷新缓冲区
void Flush() {
BeginBatchDraw();
cleardevice();//如果有别的需求可以把这行删掉
int x = board.left, y = board.top;
for (int i = 0; i < height; i++) {
int j = (i + start) % height;
x = board.left;
TCHAR* p = buffer[j];
for (; p < buffer[j] + width; p++) {
outtextxy(x, y, *p);
x += textwidth(*p);
}
y += textheight(L'A');
}
EndBatchDraw();
}
//重载运算符,懒得写其他的
EConsole& operator<<(const TCHAR* wstr) {
for (unsigned int i = 0; i < wcslen(wstr); i++) {
buffer[cursor.x][cursor.y++] = wstr[i];
if (textwidth(wstr[i]) > textwidth(L'a')) {
if (cursor.y == width) {
//处理最后一个是宽字符可能会卡出去的问题
buffer[cursor.x][cursor.y - 1] = 0;
buffer[cursor.x + 1][0] = wstr[i];
Scroll();
cursor.y = 2;
}
else cursor.y++;
}
if (cursor.y == width) {
cursor.y = 0;
Scroll();
}
}
if (autoflush)Flush();
return *this;
}
};
test.cpp:一个伪打字机,不能识别回车,不能打中文,但是可以粘贴(玄学)
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include"econsole.h"
int main() {
initgraph(640, 480);
EConsole cout({40,40,600,440});
while (1) {
while (!_kbhit())Sleep(1);
char c = _getch();
TCHAR* wc=new TCHAR;
memset(wc, 0, sizeof(wc));
swprintf_s(wc,sizeof wc, L"%wc", c);
cout << wc;
}
_getch();
return 0;
}