C语言速学——可视化easyx应用之《海贼王图册》

经过一周的学习,终于初步弄懂了easyx库制作电子相册的基本步骤和方法。
下面是展示:C语言速学——可视化easyx应用之《海贼王图册》_第1张图片
一、要领
用C语言制作上面的电子相册主要有两个要领:
1.sprintf()函数的应用:

#include
#include
int main()
{
	char s[20]="";///规定的目标容量要足够大,否则会报错
	char name[5]="张三";
	char what[7]="爱编程";
	char num[20]="";
	char number[10]="3.1415926";
	sprintf(s,"%s,%s!\n",name,what);
	//sprintf(num,"%d\n",131415926);
	//sprintf(num,"%s\n",number);
	sprintf(num,"%4.2f\n",4.1413);
	printf(s);
	printf(num);
	return 0;
}

上面代码的部分结果如下:
C语言速学——可视化easyx应用之《海贼王图册》_第2张图片
Tips:将上面的代码换着数字运行运行吧!多练习练习!

2.运用循环方式批量载入图片:

#include
#include
#include
IMAGE photo[15];
void loadphotos()
{
	for(int i=0;i<15;i++)//如果i的范围大于photo定义数组时的长度会报错(i从0开始到14共15,但是创建只有14个,将报错)
	{
		char file[20]="";
		sprintf(file,"%d.jpg",i);
		loadimage(photo+i,file,100,100);

	}
}

void putphotos()
{
	putimage(200,0,photo+0);
	putimage(200,100,photo+1);
	putimage(200,200,photo+2);
	putimage(200,300,photo+3);
	putimage(200,400,photo+4);
	putimage(200,500,photo+5);
	putimage(200,600,photo+6);
	putimage(200,700,photo+7);	
	putimage(400,0,photo+8);
	putimage(400,100,photo+9);
	putimage(400,200,photo+10);
	putimage(400,300,photo+11);
	putimage(400,400,photo+12);
	putimage(400,500,photo+13);
	putimage(400,600,photo+14);
	putimage(400,700,photo+15);
}
int main()
{
	initgraph(800,700);
	setbkcolor(WHITE);
	cleardevice();//添加背景颜色后清屏才能看到,这样理解“要cleardevice()放在setbkcolor()后”
	loadphotos();
	putphotos();
	getchar();//用getchar()就好像按下了暂停键,最后按暂停才能让前面的操作运行,来理解“要把getchar放在最后”
	return 0;
}

上面代码的运行结果如下:
C语言速学——可视化easyx应用之《海贼王图册》_第3张图片
Tips:利用for循环和设置图片的数组时,要注意设置值得范围!
二、完整代码
如下:

#include
#include
#include
void drawframe()
{
	setlinecolor(BLACK);
	rectangle(20,50,580,480);//框架
	setlinestyle((0,0,1));
	line(200,50,200,480);//分隔竖线
	line(20,120,200,120);//分隔横线
	line(20,300,200,300);//照片分隔横线
}
void drawmenu()
{
	setfillcolor(RGB(0,204,255));
	setlinecolor(WHITE);
	setlinestyle((0,0,0));
	solidcircle(75,80,25);//左圆
	circle(75,80,20);
	line(57,80,90,80);
	line(57,80,70,70);
	line(57,80,70,90);//左按钮
	solidcircle(160,80,25);//右圆
	circle(160,80,20);
	line(142,80,175,80);
	line(175,80,162,70);
	line(175,80,162,90);//右按钮
}
IMAGE smallphoto[11];
IMAGE bigphoto[11];
int flag=1;
int smallnum=1;
void loadphoto()
{
	for(int i=1;i<11;i++)//利用数组批量加载图片的方式
	{
		char filename[20]="";
		sprintf(filename,"%dhzw.jpg",i);//格式化输出,把格式化的数据写入某个字符串
		loadimage(smallphoto+i,filename,120,150);//利用循环加载图片文件
		loadimage(bigphoto+i,filename,320,400);
	}
}
void originphoto()
{
	IMAGE m;
	loadimage(&m,"1hzw.jpg",120,150);
	putimage(65,135,&m);
	IMAGE m1;
	loadimage(&m1,"2hzw.jpg",120,150);
	putimage(65,315,&m1);
	IMAGE m2;
	loadimage(&m2,"1hzw.jpg",320,400);
	putimage(260,65,&m2);

}
void drawrectangle(int flag)
{
	if (flag==1)
	{
		setlinecolor(BLACK);
		rectangle(60,130,190,290);//上图片框显示///line(50,120,200,120);//分隔横线
		setlinecolor(WHITE);
		rectangle(60,310,190,470);
	}
	else if(flag==2)
	{
		setlinecolor(BLACK);
		rectangle(60,310,190,470);//下图片框显示
		setlinecolor(WHITE);
		rectangle(60,130,190,290);
	}
}
void mousemove()
{
	MOUSEMSG m;
	while(1)
	{
		m=GetMouseMsg();
		switch(m.uMsg)
		{
		case WM_MOUSEMOVE:
			if(m.x>=65&&m.x<=115&&m.y>=55&&m.y<=105)
			{
				setlinecolor(RED);
				circle(75,80,20);
				line(57,80,90,80);
				line(57,80,70,70);
				line(57,80,70,90);//左按钮
			}
			else if(m.x>=135&&m.x<=185&&m.y>=55&&m.y<=105)
			{	
				setlinecolor(RED);
				circle(160,80,20);
				line(142,80,175,80);
				line(175,80,162,70);
				line(175,80,162,90);
			}
			else if(m.x>=470&&m.x<=525&&m.y>=10&&m.y<=40)//下一环节按钮
			{
					setlinecolor(RED);
					line(475,20,510,20);
					line(475,30,510,30);
					line(475,20,475,30);
					line(500,15,520,25);
					line(500,35,520,25);
			}
			else
			{	
					setlinecolor(WHITE);
					circle(75,80,20);
					line(57,80,90,80);
					line(57,80,70,70);
					line(57,80,70,90);//左按钮
					circle(160,80,20);
					line(142,80,175,80);
					line(175,80,162,70);
					line(175,80,162,90);//右按钮
					line(475,20,510,20);
					line(475,30,510,30);
					line(475,20,475,30);
					line(500,15,520,25);
					line(500,35,520,25);//下一环节
			}
			break;
		case WM_LBUTTONDOWN:
			if(m.x>=50&&m.x<=100&&m.y>=55&&m.y<=105)//左按钮区域
			{
				drawrectangle(flag);
			
				if(smallnum==4)
				{
					putimage(65,135,smallphoto+3);
					putimage(65,315,smallphoto+4);
				}
				if (smallnum==2)
				{
					putimage(65,135,smallphoto+1);
					putimage(65,315,smallphoto+2);
				}
				if(smallnum==6)
				{
					putimage(65,135,smallphoto+5);
					putimage(65,315,smallphoto+6);
				}
				if(smallnum==8)
				{
					putimage(65,135,smallphoto+7);
					putimage(65,315,smallphoto+8);
				}
				if(smallnum==0)
				{
					putimage(65,135,smallphoto+9);
					putimage(65,315,smallphoto+10);
					smallnum=10;
				}
				putimage(260,65,bigphoto+smallnum);	
				smallnum--;			
				flag++;
				if (flag==3)
					flag=1;
			}				
			else if(m.x>=135&&m.x<=185&&m.y>=55&&m.y<=105)//右按钮区域
			{
				drawrectangle(flag);
			
				if(smallnum==3)
				{
					putimage(65,135,smallphoto+3);
					putimage(65,315,smallphoto+4);
				}
				if (smallnum==11)
				{
					putimage(65,135,smallphoto+1);
					putimage(65,315,smallphoto+2);
					smallnum=1;
				}
				if(smallnum==5)
				{
					putimage(65,135,smallphoto+5);
					putimage(65,315,smallphoto+6);
				}
				if(smallnum==7)
				{
					putimage(65,135,smallphoto+7);
					putimage(65,315,smallphoto+8);
				}
				if(smallnum==9)
				{
					putimage(65,135,smallphoto+9);
					putimage(65,315,smallphoto+10);
				}
				putimage(260,65,bigphoto+smallnum);	
				smallnum++;			
				flag++;
				if (flag==3)
					flag=1;
			}
			else if(m.x>=470&&m.x<=525&&m.y>=10&&m.y<=40)//solidrectangle(470,10,525,40);//退出按钮
			{
				exit(0);
			}
			break;
			}
		}
	
}
void drawtitle()
{
	settextcolor(BLUE);
	settextstyle(30,0,"黑体");
	setbkmode(TRANSPARENT);
	outtextxy(180,0,"《海贼王图册》");
	settextstyle(15,0,"黑体");
	setbkmode(TRANSPARENT);
	//字体
	settextcolor(BLACK);
	outtextxy(25,60,"往");
	outtextxy(25,80,"前");
	outtextxy(110,60,"往");
	outtextxy(110,80,"后");
	settextstyle(30,0,"黑体");
	settextcolor(RED);
	setbkmode(TRANSPARENT);
	outtextxy(25,150,"上");
	outtextxy(25,230,"图");
	outtextxy(25,330,"下");
	outtextxy(25,410,"图");
	outtextxy(215,130,"当");
	outtextxy(215,200,"前");
	outtextxy(215,270,"图");
	outtextxy(215,340,"片");

}
void nextbutton()
{
	setfillcolor(RGB(96,96,96));
	solidrectangle(470,10,525,40);//退出按钮
	line(475,20,510,20);
	line(475,30,510,30);
	line(475,20,475,30);
	line(500,15,520,25);
	line(500,35,520,25);
}
void firstcontent()
{
	originphoto();
	drawtitle();
	nextbutton();
	loadphoto();
	drawframe();
	drawmenu();
	mousemove();
}
int main()
{
	initgraph(600,500);
	setbkcolor(WHITE);
	cleardevice();
	firstcontent();
	getchar();
	closegraph();
	return 0;
}

三、总结
①鼠标操作的函数MOUSEMSG需要加强,里面有许多参数,搞清楚,感觉可以实现更多交互操作
②switch函数的使用,可以方便实际操作
③多加练习,编程语言只要找到兴趣点,也还是蛮有意思的,争取早日通过语言实现自己的梦想吧!加油!加油加油!
(Tips:快去试试,制作一个自己的专属电子相册吧!)

附学习视频网址:https://www.bilibili.com/video/av48583817/

你可能感兴趣的:(C语言)