0
点赞
收藏
分享

微信扫一扫

C语言图形化编程中级

丹柯yx 2022-04-13 阅读 50

C语言图形化编程中级

所有图形化都是在.cpp下面的

音乐播放

  • 要包含多媒体库
#include <windows.h>		
//或#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")    //静态库资源
  • 了解播放音乐的函数
mciSendString("指令 播放音乐路径 ",0,0,0);
//参数:
/*
第一个参数:要发送的命令字符串。字符串结构是:[命令][设备别名][命令参数]。
第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量。
第三个参数:缓冲区的大小,就是字符变量的长度。
第四个参数:回调方式,一般设为零。
返回值:函数执行成功返回零,否则返回错误代码。
*/
//指令:
/*
	open: 打开 
	play: 播放   
			repeat:重复的方式  不阻塞,不影响程序下一步的执行 默认方式
			wait:等待播放结束   阻塞的方式,只有音乐播放结束后才会执行下一行代码
	pause: 暂停
	resume:继续
	close: 关闭
*/
mciSendString("open 播放音乐路径 ",0,0,0);
mciSendString("play 播放音乐路径 repeat",0,0,0);
mciSendString("play 播放音乐路径 wait",0,0,0);
mciSendString("pause 播放音乐路径 ",0,0,0);
mciSendString("resume 播放音乐路径 ",0,0,0);
mciSendString("close 播放音乐路径 ",0,0,0);
  • 播放音乐路径可为相对路径和绝对路径

  • 注意: 支持mp3播放,网易云的好像不行

  • 路径:

    • 相对路径 (从exe文件开始)
      • ./当前路径
      • …/上一个路径
    • 绝对路径:即从盘符开始(一般不用,发给别人的时候一般用不了)
//例如:
//相对:当前路径
mciSendString("play ./music/1.mp3 repeat",0,0,0);
//绝对:
mciSendString("play D:/ProgrammeFiles14/【VIP14】第16课 图形化中级篇/音乐播放/音乐播放/music/1.mp3 repeat",0,0,0);

鼠标操作

  • 鼠标操作: 左键按下,右键按下,滚轮滑动等等操作反馈给计算机的是一个值
  • 定义个鼠标变量存储这个消息
//ExMessage原型:
ExMessage m;
struct ExMessage
{
	USHORT message;					// The message identifier.
	union
	{
		// Data of the mouse message
		struct
		{
			bool ctrl		:1;		
			bool shift		:1;		
			bool lbutton	:1;		
			bool mbutton	:1;		
			bool rbutton	:1;		
			short x;				
			short y;				
			short wheel;		
		};
		// Data of the key message
		struct
		{
			BYTE vkcode;			
			BYTE scancode;			
			bool extended	:1;	
			bool prevdown	:1;		
		};
		// Data of the char message
		TCHAR ch;

		// Data of the window message
		struct
		{
			WPARAM wParam;
			LPARAM lParam;
		};
	};
};
  • 鼠标消息当前大家只需要掌握简单的属性即可
    • message:鼠标的类型
    • x,y :当前鼠标的坐标
WM_MOUSEMOVE: m.meesage==WM_MOUSEMOVE   鼠标移动
//windows message mouse move
WM_LBUTTONDOWN:  左键按下
WM_RBUTTONDOWN: 右键按下
WM_LBUTTONUP:    左键弹起
WM_RBUTTONUP:    右键弹起
  • 获取鼠标消息
peekmessage(&m,EM_MOUSE)
//返回值: 0 表示不存在鼠标消息

处理两个鼠标消息

  • 例如:画图工具的画线过程: 鼠标左键点击+鼠标移动
//头文件
#include<easyx.h>
#include<assert.h>
#include<conio.h>
#include<stdbool.h>

typedef struct Point
{
	int x;
	int y;
}Point;

typedef struct Brush
{
	int size;//画笔大小
	COLORREF color;//画笔颜色
	bool isdown;//是否按下
	Point begin;//画笔开始位置
}Brush;
//初始化画笔
Brush* creatbrush(int size, COLORREF color)
{
	Brush* brush = (Brush*)malloc(sizeof(Brush));
	assert(brush);
	brush->size = size;
	brush->color = color;
	brush->isdown = false;
	return brush;
}
//绘制线条
void drawline(Brush*brush,ExMessage m)
{
	//左键按下
	if (m.message == WM_LBUTTONDOWN)
	{
		brush->isdown = true;
		brush->begin.x = m.x;
		brush->begin.y = m.y;
	}
	//左键抬起
	if (m.message == WM_LBUTTONUP)
	{
		brush->isdown = false;
	}
	//如果鼠标左键被按下且鼠标移动就开始画线
	if (brush->isdown == true && m.message == WM_MOUSEMOVE)
	{
		setlinestyle(PS_ENDCAP_ROUND, brush->size);
		setlinecolor(brush->color);
		line(brush->begin.x, brush->begin.y, m.x, m.y);
	}
	brush->begin.x = m.x;
	brush->begin.y = m.y;
	if (m.message == WM_RBUTTONDOWN)
	{
		setbkcolor(WHITE);
		cleardevice();
	}
}
//按键控制
void keystroke(Brush*brush)
{
	//按键控制线的粗细
	if (_kbhit())	//判断是否存在按键,存在按键按键操作 返回非零值表示存在
	{
		char userKey = _getch();  //阻塞函数
		switch (userKey)
		{
		case '+'://线条加粗
			brush->size++;
			break;
		case '-':/线条变细
			if (brush->size > 0)
				brush->size--;
			break;
		case '1'://线条颜色变为红色
			brush->color = LIGHTRED;
			break;
		case '2'://线条颜色变为绿色
			brush->color = LIGHTGREEN;
			break;
		case '3'://线条颜色变为蓝色
			brush->color = LIGHTBLUE;
			break;
		case '\r':
			exit(0);//结束绘图
			break;
		}
	}
}
//主函数
int main()
{
	initgraph(520, 520);//初始化窗口大小
	setbkcolor(WHITE);//设置背景为白色
	cleardevice();
	Brush* brush=creatbrush(5, BLACK);
	ExMessage m;
	while (1)
	{
		peekmessage(&m, EM_MOUSE);
		drawline(brush, m);//绘制线条
		keystroke(brush);//设置颜色和画笔粗细
	}
	closegraph();
	return 0;
}

鼠标应用之封装按钮

  • 学会抽象数据
  • 学会封装函数
  • 学会分析逻辑问题
//头文件
#include <graphics.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
struct Button 
{
	int x;//按钮左上角横坐标
	int y;//按钮左上角纵坐标
	int w;//按钮的宽
	int h;//按钮的高
	COLORREF curColor;		//按钮当前显示颜色
	COLORREF inColor;		//鼠标在按钮里面的颜色
	COLORREF outColor;		//鼠标不在按钮中的颜色
	char *text;				//按键内文字内容
};
//1.创建按钮
struct Button* createButton(int x, int y, int w, int h, const char* str, 
	COLORREF inColor, COLORREF outColor) 
{
	struct Button* pB = (struct Button*)malloc(sizeof(struct Button));
	assert(pB);
	pB->x = x;
	pB->y = y;
	pB->w = w;
	pB->h = h;
	pB->inColor = inColor;
	pB->outColor = outColor;
	pB->curColor = pB->outColor;
	int textLength = strlen(str) + 1;
	pB->text = (char*)malloc(sizeof(char) * textLength);
	assert(pB->text);
	strcpy_s(pB->text, textLength, str);
	return pB;
}
//2.画按钮
void drawButton(struct Button* pB) 
{
	//1.按钮就是一个矩形,画一个矩形
	setlinecolor(BLACK);
	setfillcolor(pB->curColor);
	fillrectangle(pB->x, pB->y, pB->x + pB->w, pB->y + pB->h);
	//2.画文字
	settextcolor(BLACK);		//设置文字颜色
	setbkmode(TRANSPARENT);		//去掉文字背景
	settextstyle(15, 0, "楷体");
	//文字如何居中
	//textwidth(str),textheight(str);
	int textw = textwidth(pB->text);		//获取文字宽度
	int texth = textheight(pB->text);		//获取文字高度
	int xx = pB->x + (pB->w - textw) / 2;
	int yy = pB->y + (pB->h - texth) / 2;
	outtextxy(xx, yy, pB->text);
}
//3.判断鼠标是否在按钮中
bool isInButton(struct Button* pB, ExMessage m) 
{
	if (m.x > pB->x && m.x<pB->x + pB->w
		&& m.y>pB->y && m.y < pB->y + pB->h) 
	{
		pB->curColor = pB->inColor;
		return true;
	}
	pB->curColor = pB->outColor;
	return false;
}
//4.鼠标点击
bool isClickButton(struct Button* pB, ExMessage m) 
{
	if (isInButton(pB, m) && m.message == WM_LBUTTONDOWN) 
	{
		return true;
	}
	return false;
}
//主函数
int main()
{
	initgraph(400, 400);//创建窗口
	struct Button* play = createButton(0, 0, 60, 20, "play", RGB(236, 244, 255), RGB(204, 213, 240));//创建按钮
	BeginBatchDraw();//批量绘图
	ExMessage m;
	mciSendString("open ./music/1.mp3", NULL, 0, NULL);
	while (1) 
	{
		drawButton(play);//绘制按钮
		peekmessage(&m, EM_MOUSE);
		if (isClickButton(play, m))
		{
			mciSendString("play ./music/1.mp3 repeat", NULL, 0, NULL);
		}
		FlushBatchDraw();
	}
	EndBatchDraw();
	closegraph();
	return 0;
}
举报

相关推荐

0 条评论