Extra article: Getting started with the EasyX graphics library (takes you into different program windows, super detailed explanation)

foreword

Without further ado, let's see what we all need to know!
insert image description here

1. Install EasyX

Official website: Please click me to enter
Note : EasyX is a free drawing library for Visual C++, supporting VC6.0 ~ VC2022

Download: insert image description here
After installation: insert image description here
Note: The installed version will automatically recognize the version you have installed, just click to install, the EasyX document can be downloaded or not (the difference is offline and online), it is best to exit your vs when installing.

Once installed we can create a new project.

2. Create and close the window

1. Include the header file

Note: The set source file is the suffix .cpp

Header file: #include <graphics.h>

2. Create window and close window function

Create window function: initgraph();
Parameters:
1. Width
2. Height
Close window function: closegraph();
Parameters: None
Note: Here is the function that will close the window immediately after execution, so a buffer function is needed: getchar() to let The window remains displayed.

Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>//getchar的头文件
int main()
{
    
    
	initgraph(520, 520);//宽520,高520.
	getchar();
	closegraph();
	return 0;
}

Effect
insert image description here
At this point we have created a window! But it's a bit monotonous now, and some more elements need to be added, so take your time.

3. Coordinate principle of window

We should all have learned to establish a plane Cartesian coordinate system, so how is the coordinate system here established?
Look at the picture below:
insert image description here

Note: Please remember that the coordinate origin here is directly below the window name.

4. Set the background color

Looking at this mass of black is not conducive to drawing pictures. Here we need to use the function of setting the background.
Function: setbkcolor();
Parameter: color
Here, white is suitable for drawing, so let's use white! But last time the default black is still there, we need to clear it, and we need to use a function.
Function: cleardevice();
Parameters: None
Function: Use the current background color to clear the drawing device
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Is it for nothing?

3. Drawing of basic graphics

1. Round (hollow)

Function: circle();
Parameters:
1. The abscissa of the center of the circle
2. The ordinate of the center of the circle
3. The radius of the circle
Code implementation:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	//画一个圆
	circle(50, 50, 50);
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
why is this? Is it because the color is the same coverage?
Let's demonstrate:
code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(BLACK);//设置背景为黑色
	cleardevice();
	//画一个圆
	circle(50, 50, 50);
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Really, what if we want to draw a circle on a white background?
This is used to set the function of the line.
Function: setfillcolor()
Parameters: Color
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	setlinecolor(BLACK);
	//画一个圆
	circle(50, 50, 50);
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

2. Solid circle with border

Function: fillcircle();
Parameters:
1. The abscissa of the center of the circle
2. The ordinate of the center of the circle
3. The radius of the circle
Write the code:


#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	setlinecolor(BLACK);
	//画一个圆
	circle(50, 50, 50);
	fillcircle(50, 150, 50);//有边框
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Here, because the background color of the circle is white, it seems that there is no filling, so let's design it.
Function: setfillcolor()
Parameters: Color
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	setlinecolor(BLACK);
	setfillcolor(BLUE);
	//画一个圆
	circle(50, 50, 50);
	fillcircle(50, 150, 50);//有边框
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

3. Solid circle without border

Function: solidcircle();
Parameters:
1. The abscissa of the center of the circle
2. The ordinate of the center of the circle
3. The radius of the circle
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);//设置一个窗口
	setbkcolor(WHITE);//设置背景色
	cleardevice();//用背景色覆盖窗口
	setlinecolor(BLACK);//设置图形线条颜色
	setfillcolor(BLUE);//设置图形填充颜色
	//画一个圆
	circle(50, 50, 50);
	fillcircle(50, 150, 50);//有边框
	solidcircle(50, 250, 50);//无边框
	getchar();//缓冲
	closegraph();//关闭窗口
	return 0;
}

Effect:
insert image description here

4. Text printing

1. Preparations

insert image description here
insert image description here
Ready to finish! Here is to better use the function of printing text.

2. Print text

Function: outtextxy();
Parameters:
1. The abscissa of the text
2. The ordinate of the text
3. The content of the text
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>

int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Still the problem just now, the text color is white by default, so we need to set the font color.
function: settextcolor()
parameter: color
code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>

int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");

	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

3. Set text format

Function: settextstyle()
Parameters:
1. The size of the character
2. The width of the character, if it is 0, it will automatically match
3. The form of the character, such as italic, Song
type Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>

int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	settextstyle(20, 0, "楷体");//文字的大小,紧凑程度,字体的样式
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");

	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Doesn't it look better?

3. Set the color of the current font

Function: setcolor()
parameter: color
code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	settextstyle(20, 0, "楷体");//文字的大小,紧凑程度,字体的样式
	setcolor(GREEN);
	//setcolor(RGB(70, 127, 50));
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
Note: The three parameters in RGB() are pigment points.
insert image description here

4. Set the background of the font

Because the background of the font is white by default, it will cover other objects, so we can change the background of the font to a transparent color in order not to cover it.
Function: setbkmode()
Parameter: Color
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	settextstyle(20, 0, "楷体");//文字的大小,紧凑程度,字体的样式
	setcolor(GREEN);
	setbkmode(TRANSPARENT);
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");
	getchar();
	closegraph();
	return 0;
}

Effect:
before: insert image description here
after:
insert image description here

5. The text is displayed in the center of the graphics

We need a rectangle for better display.
Function: fillrectangle()
Parameters:
1. The x coordinate of the left part of the rectangle
2. The y coordinate of the top part of the rectangle
3. The x coordinate of the right part of the rectangle
4. The y coordinate of the bottom part of the rectangle
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	settextstyle(20, 0, "楷体");//文字的大小,紧凑程度,字体的样式
	setcolor(GREEN);
	setbkmode(TRANSPARENT);
	fillrectangle(100, 100, 500, 500);
	outtextxy(100, 100, "谢谢观众老爷,赏脸看shun_hua的博客");
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
The principle of centered display:

insert image description here
cdceaa85.png)
Note: Here the height of the rectangle is the bottom y coordinate minus the top y coordinate, and the width of the rectangle is the right x coordinate minus the left x coordinate.
code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
int main()
{
    
    
	initgraph(520, 520);
	setbkcolor(WHITE);
	cleardevice();
	settextcolor(RED);//文字格式为红色
	settextstyle(20, 0, "楷体");//文字的大小,紧凑程度,字体的样式
	setcolor(GREEN);
	setbkmode(TRANSPARENT);
	fillrectangle(100, 100, 500, 500);
	//求字体的宽度与高度
	char arr[] = "谢谢观众老爷,赏脸看shun_hua的博客";
	int width = textwidth(arr);
	int height = textheight(arr);
    //求所加坐标的值
	//所加x坐标的值
	int x = (500 - 100) / 2 - width / 2;
	//所加y坐标的值
	int y = (500 - 100) / 2 - height / 2;
	outtextxy(100+x, 100+y, "谢谢观众老爷,赏脸看shun_hua的博客");
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

5. Keyboard information

1. Get keyboard information

Function: _kbhit()——Boolean type
Header file: conio.h
Parameters: None

2. Output keyboard information

Function: _getch()
Header file: conio.h

3. Move a ball

code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
    
    
	initgraph(520, 520,SHOWCONSOLE);//这里的第三个参数为显示控制台窗口
	setbkcolor(WHITE);
	int x = 200, y = 300;
	while (1)
	{
    
    
		cleardevice();
		setfillcolor(RED);
		solidcircle(x, y, 50);
		if (_kbhit())
		{
    
    
			char key = _getch();
			printf("%d %c\n", key, key);//可以获取键的ASCII值
			switch (key)
			{
    
    
			case 72://上键的ASCII值
				y -= 5;
				break;
			case 80://下键的ASCII值
				y += 5;
				break;
			case 75://左键的ASCII值
				x -= 5;
				break;
			case 77://右键的ASCII值
				x += 5;
				break;
			}
		}
	}
	getchar();
	closegraph();
	return 0;
}

Effects:
insert image description here
Disadvantages: There will be a splash screen, and due to the nature of the switch, it is impossible to move diagonally.

Solution;

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
    
    
	initgraph(520, 520,SHOWCONSOLE);
	setbkcolor(WHITE);
	int x = 200, y = 300;
	BeginBatchDraw();//这个函数用于开始批量绘图。执行后,任何绘图操作都将暂时不输出
	//到绘图窗口上,直到执行 FlushBatchDraw 或 EndBatchDraw 才将之前的绘图输出。

void BeginBatchDraw();
	while (1)
	{
    
    
		cleardevice();

		setfillcolor(RED);
		solidcircle(x, y, 50);
		FlushBatchDraw();//这个函数用于执行未完成的绘制任务。比EndBatchDraw更稳定
		//四个if为四个接口,可以斜向上移动,并且更加丝滑。
		if (GetAsyncKeyState(VK_UP))
		{
    
    
			y--;
		}
		if (GetAsyncKeyState(VK_DOWN))
		{
    
    
			y++;
		}
		if (GetAsyncKeyState(VK_LEFT))
		{
    
    
			x--;
		}
		if (GetAsyncKeyState(VK_RIGHT))
		{
    
    
			x++;
		}
	}
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

6. Mouse information

1. Determine whether there is mouse information

Function: MouseHit()——Boolean type
Parameters: No parameters

2. Read mouse information

Type of mouse information: MOUSEMSG
Define variable (example): MOUSEMSG msg;
function to read mouse information: GetMouseMsg()

3. Message processing

msg.uMsg converts variables into constant variables (guess)
WM_LBUTTONDOWN——Left mouse button
WM_RBUTTONDOWN——Right mouse button
msg.x gets the abscissa of the mouse
msg.y gets the ordinate of the mouse
Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
    
    
	initgraph(520, 520,SHOWCONSOLE);
	setbkcolor(BLACK);
	cleardevice();
	while (1)
	{
    
    
		if (MouseHit())
		{
    
    
			MOUSEMSG msg = GetMouseMsg();
			switch (msg.uMsg)
			{
    
    

			 case WM_LBUTTONDOWN:
				outtextxy(200, 200, "鼠标左键");
				printf("坐标(%d,%d)", msg.x, msg.y);
				break;
			 case WM_RBUTTONDOWN:
				outtextxy(100, 100, "鼠标右键");
				printf("坐标(%d,%d)", msg.x, msg.y);
				break;
			}
		}
	}
	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here

7. Insert a picture

1. Define image variables

Image type: IMAGE
Definition: IMAGE mage;

2. Read pictures

Function: loadimage()
Parameters:
1. Image address
2. Image folder - ./ is the current file directory.

3. Load pictures

Function: putimage()
Parameters:
1. The abscissa of the picture
2. The ordinate of the picture
3. The address of the picture
Code

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
    
    
	initgraph(2000, 2000,SHOWCONSOLE);
	setbkcolor(WHITE);
	cleardevice();
	IMAGE mage;
	loadimage(&mage, "2.jpg");
	putimage(0, 0, &mage);
	getchar();
	closegraph();
	return 0;
}

Note: You have to look at the size of your picture and window to achieve your desired effect.
Effect:
insert image description here

8. Play music

1. Read music

Function: mciSendString()
Header file: mmsystem.h
Preprocessing directive: #pragma comment(lib,"winmm.lib")

2. Play and close music

#define _CRT_SECURE_NO_WARNINGS 1
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<mmsystem.h>//包含多媒体接口
#pragma comment(lib,"winmm.lib")//处理接口
void BGM()
{
    
    
   //文件为.mp3为后缀才可播放,要把音乐拷贝到项目所在文件夹里
	mciSendString("open ./ww.mp3", 0, 0, 0);//0,0,0为默认输出控制台
	mciSendString("play ./ww.mp3", 0, 0, 0);
	if (0)
	{
    
    
		mciSendString("close ./ww.mp3",0, 0, 0);
	}
}
int main()
{
    
    
	initgraph(500, 500,SHOWCONSOLE);
	setbkcolor(WHITE);
 	cleardevice();
	BGM();
	getchar();
	closegraph();
	return 0;
}

The effect is a piece of music.

Nine. Window settings

1. Set the graphics window

Window type: HWND
Window definition: HWND hand;
Function: GetHWnd()
Function: Get window information
Function: SetWindowTex
Function: Set window content
Parameters: 1. Type variable name 2. Modify content

2. Set the prompt window

Function: MessageBox
Function: Set window content
Parameters: 1. Type variable name 2. Modify content
Code:

#include<conio.h>
#include<mmsystem.h>//包含多媒体接口
#pragma comment(lib,"winmm.lib")
void BGM()
{
    
    

	mciSendString("open ./ww.mp3", 0, 0, 0);//默认输出控制台
	mciSendString("play ./ww.mp3", 0, 0, 0);
	if (0)
	{
    
    
		mciSendString("close ./ww.mp3",0, 0, 0);
	}
}
void change()
{
    
    
	HWND hand = GetHWnd();
	SetWindowText(hand, "欢迎来到我的博客");
	int i = MessageBox(hand, "请留下你的三连", "提示",MB_OKCANCEL);
	if (IDOK == i)
	{
    
    
		printf("谢谢大佬!");
	}
	else if (IDCANCEL == i)
	{
    
    
		printf("感谢您的观看!");
	}
}
int main()
{
    
    
	initgraph(500, 500,SHOWCONSOLE);
	setbkcolor(WHITE);

	cleardevice();
	change();

	getchar();
	closegraph();
	return 0;
}

Effect:
insert image description here
insert image description here

Summarize

If you can see this seriously, I firmly believe that you can gain a lot! I also hope this article can help you. If you think it is good, please click to like it without money. If there is a mistake, please point it out gently. Thank you everyone here!

Guess you like

Origin blog.csdn.net/Shun_Hua/article/details/128182566
Recommended