C++控制台实现显示字符图片

2022/2/2 1:29:34

本文主要是介绍C++控制台实现显示字符图片,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这个控制台字符图片编译器叫2D player pro 2.0完全支持鼠标化操作

本代码遵循Mozilla开源协议.

#include<bits/stdc++.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<cstdio>
#include<time.h>
#define fk_n 200
#define make_n 100 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
CONSOLE_FONT_INFO consoleCurrentFont;
using namespace std;
string vesion="2D_player-2.0";
string word[15]={" ","▲","△","▼","▽","○","●","◆","◇","■","□","★","☆","♀","♂"};
struct
{ 
	void color(int all){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),all);}
	void wait(int all){Sleep(all);}
	void cls(){system("cls");}
	void b(short hz,double time){Beep(hz,time);}
	void gt(int x,int y) 
	{
		COORD coord;
		coord.X=y;//Here, the order is reversed, otherwise, the output pointer is moved to Y row and X column
		coord.Y=x;
		HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleCursorPosition(a,coord);
	}
}basic; 
struct
{
	LONG x=-1,y=-1,lest_x=-1,lest_y=-1;
	double real_x,real_y;
	POINT pt= {0,0};
	CONSOLE_FONT_INFO l_z;
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_FONT_INFO x_z;
	int do_=1;
    void check_mouse() 
    {
		pt.y=pt.y/real_y,pt.x=(pt.x/real_x);
		if(pt.y<0)pt.y=0;
		if(pt.x<0)pt.x=0;
	}
	void mouse()
	{
		GetCurrentConsoleFont(hOutput, FALSE, &x_z);
		if(do_==1)do_=0;
		else if((do_==0)&&(l_z.dwFontSize.X!=x_z.dwFontSize.X||l_z.dwFontSize.Y!=x_z.dwFontSize.Y))
		{
			do_=1;
			basic.color(15) ;
			basic.cls();
			printf("检测到您在修改字体!\n请您修改完字体后按任意键进行强制鼠标校正");getch();
			basic.cls(); 
			get_real_mouse();
		}
		l_z.dwFontSize.X=x_z.dwFontSize.X,l_z.dwFontSize.Y=x_z.dwFontSize.Y;
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
		GetCursorPos(&pt);    //获取鼠标当前位置 
		ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt); 
		check_mouse();
		x=pt.x,y=pt.y;
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
	} 
	void get_real_mouse()
    {
    	basic.cls();printf("鼠标校正,请用鼠标点击右下角的\"Hi\"");
		int x=20,y=20;
		basic.gt(x,y);printf("Hi");
		while(1)
		{
			GetCursorPos(&pt);    //获取鼠标当前位置
			ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt); 
			if(KEY_DOWN(VK_LBUTTON)&&MessageBox(0,"您确定您点击了\"Hi\"吗?\n如果您没有点击此点可能会导致程序错乱.","鼠标校正",4)==IDYES)
			{
				real_x=pt.x/y,real_y=pt.y/x;
				break;
			}
		}
		basic.wait(500); basic.cls();
	}
	string whats[10]={},title="",about_things="null";
	int ai_choose(void) 
	{
		CONSOLE_CURSOR_INFO cursor_info={1,0};
		SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
		short how_long=0,wait_=1;
		for(int i=1;i<=9;i++)
		{
			if(whats[i]!="")how_long++;
			else break;
		}
		int choose; 
		while(1) 
		{
			choose=-1; 
			basic.gt(1,0);
			mouse();
			if(y==1)choose=1;
			printf("%s",title.c_str()); 
			if(choose==1){basic.color(16+7);printf("(?)\n");} 
			else {basic.color(7);printf("     \n");} 
			short x=1;
			for(int i=1;i<=how_long;i++)
			{
				x++;
				if(y==x)choose=x;
				if(choose==i+1)basic.color(16*15);
				else basic.color(7);
				printf("%s\n",whats[i].c_str()); 
			}
			basic.color(0) ;printf("\nheveral"); basic.color(7);
			if(wait_==1){wait_=0;Sleep(600);}
			else if(KEY_DOWN(VK_LBUTTON)&&choose>1)break;
			else if(KEY_DOWN(VK_LBUTTON)&&choose==1)
			{
				if(about_things!=""&&about_things!="null")
				{
					basic.cls(); 
					string about="关于"+title;
					basic.color(7);
					printf("%s\n",about.c_str());
					basic.color(15);
					printf("%s",about_things.c_str());
					basic.color(8);
					printf("\n按任意继续...");
					getch();
					basic.cls(); 
				}
			
			} 
		}
		for(int i=1;i<=9;i++)whats[i]="";
		title="",about_things="null";
		basic.cls();
		return choose-1; 
	}
}m;
int player(string name)
{
	basic.cls() ;
	ifstream fin(name+".photo_Pro");
	string a,V,n;int zx,zy,change;
	fin>>a>>V;
	if(V!=vesion)
	{
		V="无法打开\""+name+".photo_Pro\"!";
		MessageBox(0,"Error",V.c_str() ,MB_OK);
	}
	fin>>a>>n; 
	string title="title 2D player pro 2.0-"+name+"(原始名:"+n+")";
	system(title.c_str());
	fin>>a>>change;
	fin>>a>>zx>>a>>zy;
	short o_[zx+1][zy+1]={};
	string P_[zx+1][zy+1]={};
	bool c_[zx+1][zy+1]={};
	fin.close();
	if(change>=1)
	{
		//MessageBox(0,"Hello","Caption",MB_OK);

		ifstream in(name+".photo_Pro");
		in>>a>>a;
		in>>a>>a;
		in>>a>>a;
		in>>a>>a;
		in>>a>>a; 
		in>>a;
		for(int x=0;x<=zx;x++)
		{
			for(int y=0;y<=zy;y++)
			{
				in>>o_[x][y]>>P_[x][y]; 
			}
		}
		in.close();
	}
	else
	{
		for(int x=0;x<=zx;x++)
		{
			for(int y=0;y<=zy;y++)
			{
				P_[x][y]=" "; 
			}
		}
	 } 
	int qian=0,bei=0,hc=0; 
	string ziti=" ";
	bool save=false,other_save=false,exit=false,cpp=false;/
	while(1)
	{
		CONSOLE_CURSOR_INFO cursor_info={1,0};
		SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
		basic.gt(0,0) ;
		for(int i=0;i<=15;i++)
		{
			basic.color(i*16+i) ;
			printf(" ");
		}
		basic.color(7);
		printf("  背景颜色,选择色块:"); 
		basic.color(bei*16+bei);
		printf(" ");
		basic.color(7);
		printf("\n"); 
		for(int i=0;i<=15;i++)
		{
			basic.color(i*16+i);
			printf(" ");
		}
		basic.color(7);
		printf("  字体颜色,选择色块:"); 
		basic.color(qian*16+qian);
		printf(" ");
		basic.color(7);
		printf("\n"); 
		basic.color(bei*16+qian);
		printf(" ▲△▼▽○●◆◇■□★☆♀♂");
		basic.color(7);
		printf("+  推荐字体,选择字体:"); 
		basic.color(bei*16+qian);
		printf("%s",ziti.c_str());
		basic.color(7);
		printf("\n[保存并退出] [另存为并退出] [打开文件位置] [导出cpp可编译文件] [退出] [保存] [另存为]"); 
		m.mouse();
		for(int x=0,cx=4;x<=zx;x++,cx++)
		{
			for(int y=0,cy=0;y<=zy;y++,cy=cy+2)
			{
				if(!c_[x][y])
				{
					basic.gt(cx,cy);
					basic.color(o_[x][y]);
					printf("%s",P_[x][y].c_str());
					c_[x][y]=true;
				}
			}
			printf("\n");
		}
		if(KEY_DOWN(VK_LBUTTON)) 
		{
			if(m.y<=2)
			{
				hc=1;
				if(m.x<=30&&m.y==0)bei=m.x/2;
				else if(m.x<=30&&m.y==1)qian=m.x/2;
				else if(m.x<=28&&m.y==2)ziti=word[m.x/2];
				else if((m.x==29||m.x==30)&&m.y==2)
				{
					string as;
					basic.cls();
					printf("添加字体:");
					cin>>as;
					ziti=as; 
					for(int x=0;x<=zx;x++)
					{
						for(int y=0;y<=zy;y++)
						{
							c_[x][y]=false;
						}
					}
				}
			} 
			else if(m.y==3&&hc==0) //操作 
			{
				if(m.x<=12)save=true,exit=true; //保存并退出
				else if(m.x>=13&&m.x<=27) save=true,other_save=true,exit=true; //另存为并退出
				else if(m.x>=28&&m.x<=42) //打开文件位置
				{
					string start="start "+name+".photo_Pro";
					system(start.c_str());
				} 
				else if(m.x>=43&&m.x<=60) cpp=true;//生成可编译文件 
				else if(m.x>=61&&m.x<=67) exit=true;//退出 
				else if(m.x>=68&&m.x<=74) save=true;//保存 
				else if(m.x>=75&&m.x<=83) save=true,other_save=true;
				/
				string n=name;
				if(other_save)
				{
					basic.cls();
					printf("另存为名称:");
					cin>>n;
					basic.cls(); 
					for(int x=0;x<=zx;x++)
					{
						for(int y=0;y<=zy;y++)
						{
							c_[x][y]=false;
						}
					}
				}
				if(save)
				{
					change++;
					ofstream fout(n+".photo_Pro");
					fout<<"[system] "+vesion+"\n[name] "<<name<<"\n[change] "<<change<<"\n[long-x] "<<zx<<"\n[long-y] "<<zy<<"\n---\n"; 
					for(int x=0;x<=zx;x++)
					{
						for(int y=0;y<=zy;y++)
						{
							fout<<o_[x][y]<<" "<<P_[x][y]+" ";
						}
						fout<<"\n"; 
					}
					fout.close();
					MessageBox(0,"保存成功!","提示",MB_OK);
				}
				if(cpp)//生成可编译文件 
				{
					ofstream fout(n+".cpp");
					fout<<"/*\nMade by heveral\nvesion:"<<vesion<<"\nDate:2022.02.01\nThis code complies with the Mozilla agreement.\n*/\n#include<windows.h>\n#include<stdio.h>\n#include<conio.h>\n#include<iostream>\n#include<cstdio>\nusing namespace std;\nint main()\n{\n"; 
					fout<<"system(\"title "<<name<<".photo_Pro\");\n\nint zx="<<zx<<",zy="<<zy<<";\n\nstring W["<<(zx+1)<<"]["<<(zy+1)<<"]={\n"; 
					for(int x=0;x<=zx;x++)
					{
						fout<<"{\"";
						for(int y=0;y<=zy-1;y++)
						{	
							fout<<P_[x][y]<<"\",\"";
						}
						fout<<P_[x][zy]<<"\"},\n";
					}
						fout<<"};\n\nshort w["<<(zx+1)<<"]["<<(zy+1)<<"]={\n";
						for(int x=0;x<=zx;x++)
						{
							fout<<"{";
							for(int y=0;y<=zy-1;y++)
							{
								fout<<o_[x][y]<<",";
							}
							fout<<o_[x][zy]<<"},\n";
						}
						
						fout<<"};\n\nfor(int x=0;x<=zx;x++)\n{\nfor(int y=0;y<=zy;y++)\n{\nSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),w[x][y]);";
						fout<<"\ncout<<W[x][y];\n}cout<<endl;\n}\n\nSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);\ncout<<endl<<\"请按任意键继续\";\n"; 
						fout<<"getch();\nreturn 0;\n}"; 
						fout.close();
						MessageBox(0,"导出成功!\n生成代码遵守Mozilla开源协议","提示",MB_OK);
						string start="start "+name+".cpp";
						system(start.c_str());
					}
					if(exit)
					{
						basic.cls();
						break;	
						
					}
					save=false,other_save=false,exit=false,cpp=false;
			} 
			else if(m.y<=4+zx&&m.y>=4&&m.x/2<=zy)
			{ 
				P_[m.y-4][m.x/2]=ziti;
				o_[m.y-4][m.x/2]=bei*16+qian;
				c_[m.y-4][m.x/2]=false;
				hc=3;
			}
			else hc=0;
		}
		
	}
	system("title 2D player pro 2.0"); 
}
int main()
{
	system("title 2D player pro 2.0"); 
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);  
    DWORD mode;  
    GetConsoleMode(hStdin, &mode);  
	mode &= ~ENABLE_QUICK_EDIT_MODE;  //移除快速编辑模式
	mode &= ~ENABLE_INSERT_MODE;      //移除插入模式
    mode &= ~ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin, mode);  
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
	printf("2D player pro 2.0\nMade by heveral");Sleep(100);m.get_real_mouse();
	while(1)
	{
		m.title="",m.about_things="",m.whats[1]="新建  ___.photo_Pro",m.whats[2]="修改  ___.photo_Pro";
		short a=m.ai_choose(); 
		basic.cls();
		if(a==1)
		{
			printf("新建名称(自动补齐尾缀名:___.photo_Pro)\n"); 
			string name;
			int x,y; 
			cin>>name;
			ifstream fin(name+".photo_Pro");
			if(fin)
			{
				basic.cls();
				m.title="命名重复",m.about_things="当前命名文件夹中存在同名文件",m.whats[1]="撤销",m.whats[2]="覆盖";
				int u=m.ai_choose() ;
				if(u==1) 
				{
					basic.cls();
					continue;
				}
			}
			fin.close();
			printf("\nx 长度\n") ;
			cin>>x; 
			printf("\ny 长度\n") ;
			cin>>y; 
			ofstream fout(name+".photo_Pro");
			fout<<"[system] "+vesion+"\n[name] "<<name<<"\n[change] 0\n[long-x] "<<x<<"\n[long-y] "<<y<<"\n---\n"; 
			fout.close();
			player(name);
		}
		else 
		{
			printf("修改名称(自动补齐尾缀名:.photo_Pro)\n"); 
			string name;
			cin>>name; 
			ifstream fin(name+".photo_Pro");
			if(fin)
			{
				player(name);
			}
			else
			{
				basic.cls();
				MessageBox(0,"您要打开的文件不存在!","Error",MB_OK);
			}
			fin.close();
		}
	}
	
}



这篇关于C++控制台实现显示字符图片的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程