#include<Windows.h>
#include <windows.h>                			//GetAsyncKeyState所需头文件
#include <bits/stdc++.h>
#include<conio.h>
#define KEY_DOWN(VK_NONAME) (GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0 //用来检测按键的点击事件 
using namespace std;
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;

//----------移动光标----------
void gotoxy(int x, int y) {
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
	SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}

//----------隐藏光标----------
void HideTheCursor() {
	CONSOLE_CURSOR_INFO cciCursor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
		cciCursor.bVisible = FALSE;
		SetConsoleCursorInfo(hStdOut, &cciCursor);
	}
}

bool mousehit()
{
	return KEY_DOWN(VK_LBUTTON);
 } 

int mousex()
{
	POINT p;
	GetCursorPos(&p);
	ScreenToClient(h,&p);               //获取鼠标在窗口上的位置
	GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
	int x=p.x/=consoleCurrentFont.dwFontSize.X;
	return x;
}
int mousey()
{
	POINT p;
	GetCursorPos(&p);
	ScreenToClient(h,&p);               //获取鼠标在窗口上的位置
	GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
	int y=p.y/=consoleCurrentFont.dwFontSize.Y;
	return y;
}

void print(string s, int color)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
	cout<<s;
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
int ruber;
void rub(int x,int y)
{
	for(int i=x-ruber;i<=x+ruber;i++)
	{
		for(int j=y-ruber;j<=y+ruber;j++)
		{
			gotoxy(i,j);
			cout<<" ";
		}
	 } 
}
void rubb()
{
	system("cls");
	cout<<"请输入橡皮擦大小(大于等于0且小于等于5)否则默认为1:";
	cin>>ruber;
	if(ruber<1||ruber>10)ruber=1;
	cout<<"橡皮擦大小为:"<<ruber;
	cout<<"\n";
	system("pause");
	system("cls"); 
}
int pri;
void pr()
{
	system("cls");
	cout<<"请输入画笔大小(大于等于0且小于等于5)否则默认为1:";
	cin>>pri;
	if(pri<1||pri>10)pri=1;
	cout<<"画笔大小为:"<<pri;
	cout<<"\n";
	system("pause");
	system("cls"); 	
}
void prin(int x,int y,string tubiao,int color)
{
	for(int i=x-pri;i<=x+pri;i++)
	{
		for(int j=y-pri;j<=y+pri;j++)
		{
			gotoxy(i,j);
			print(tubiao,color);
		}
	 } 
}
void shezhi()
{
	rubb();
	pr();
}
int main() {
	system("title 画画"); 
	
	//----------移除快速编辑模式(对于win10用户)----------
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
	DWORD mode;
	GetConsoleMode(hStdin, &mode);
	mode &= ~ENABLE_QUICK_EDIT_MODE;
	SetConsoleMode(hStdin, mode);

	//----------循环检测----------
	while(1)
	{
		system("cls");
		bool rubber;
		int color=7;
		bool cf;
		cout<<"欢迎来到画画软件v1.2.0,请输入图标:";
		string tubiao;
		cin>>tubiao;
		if(tubiao.length()>1)tubiao=tubiao[0]; 
		HideTheCursor(); 
		system("cls"); 
		while(1) {                      			//循环检测
			if(color>15)
			{
				color=0;
			}
			gotoxy(0,0);
			cout<<"橡皮:"<<ruber<<"\n"; 
			cout<<"画笔:"<<pri<<"\n";
			cout<<"颜色:"<<color<<"\n";
			int x=mousex();
			int y=mousey();
			cout<<"鼠标:("<<x<<","<<y<<")\n"; 
			if(kbhit())
			{ 
				char c=getch();
				if(c=='r')
				{
	
					rubber=true;
				}
				else if(c=='p')
				{
					rubber=false;
				}
				else if(c=='c')
				{
					system("cls");
				}
				else if(c=='k')
				{
					color++;
				}
				else if(c=='s')
				{
					shezhi();
				}
				else if(c==(char)27)
				{
					break;
				}
			}
			if(mousehit())
			{
				if(!rubber)  
				{
					prin(x,y,tubiao,color);
				}                      
				
				else
				{
					rub(x,y);
				}
				
			}		
			Sleep(20);                  			//延时
		}
	}
	return 0;
}