【C语言玩转圣诞节】100行代码画出圣诞水晶球

我爱你,不是因为你是一个怎样的人,而是因为我喜欢与你在一起时的感觉。

嗨!这里是狐狸~~

今天是2021年12月23号,后天就是圣诞节了,再过一个星期就是2022年了,最近总是感觉伤感,有些事情就是比想象中来的快一些,希望大家都可以把握2021年最后的时间,不留遗憾吧,后天圣诞节,今天再教大家一个圣诞项目吧,圣诞水晶球,今天这个呢代码不多,但难度会有点,因为这个涉及桌面,就是可以在桌面实现,希望大家可以认真看,认真学吧。

同样,先给大家看效果吧

 效果还是很不错的,再加上一个音乐,女朋友看完就马上同意你了,嘿嘿,这里就不再是简单的窗口了,难度有一些,但也不大,毕竟代码也不多,好了,认真学,认真的尝试哦!

项目代码

这是我第一次只用一个代码段,因为代码确实不多,我觉得分段就没必要了,而且其中必要的地方,我也加了详细的注释,应该还好,难就难在之前没有用Windows库,这次用了,效果看起来就会舒服很多,怎么样,学会了赶快拿给心中的那个人看吧!

#include <stdio.h>
#include <windows.h>
#include <graphics.h>
#include <commctrl.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

int main()
{
	keybd_event(VK_LWIN, 0, 0, 0);
	keybd_event('D', 0, 0, 0);
	keybd_event('D', 0, 2, 0);
	keybd_event(VK_LWIN, 0, 2, 0);
	
	Sleep(3000);
	int CX = GetSystemMetrics(SM_CXSCREEN);
	int CY = GetSystemMetrics(SM_CYSCREEN);

	HWND hwnd = initgraph(500,500); //创建一个窗口hwnd
	HRGN Ellip = CreateEllipticRgn(10, 32, 100+10, 100+32);//设置一个逻辑的图形rect
	SetWindowRgn(hwnd, Ellip, true);  //设置窗口的区域
	SetForegroundWindow(hwnd);
	MoveWindow(hwnd, CX / 2 - 120, CY / 2, 500, 500, true);

	mciSendString("open res/music.mp3", 0, 0, 0);    //打开
	mciSendString("play res/music.mp3 repeat", 0, 0, 0);    //播放
	bool isplay = true;  //音乐是否正在播放

	int sence = 3;  //场景
	int frame = 0;  //帧数
	IMAGE img[30];
	int maxframe[] = { 20, 7, 4, 15, 10}; //每一种场景的图片的张数
	char picpath[30] = { 0 };  //字符串 图片的路径
	for (int i = 0; i < maxframe[sence]; i++)
	{
		sprintf(picpath, "res/%02d-%02d.bmp", sence, i);
		loadimage(&img[i], picpath, 100, 100);
	}

	/*---------------------------------------------------------*/
	//获取到桌面图标的窗口
	HWND zmWnd = FindWindow("Progman","Program Manager");  //桌面窗口
	HWND bzWnd = FindWindowEx(zmWnd, 0, "SHELLDLL_DefView", NULL);  //壁纸窗口
	HWND tbWnd = FindWindowEx(bzWnd, 0, "SysListView32", "FolderView");  //图标窗口

	HWND workHwnd = NULL;
	while (tbWnd == NULL)//必须存在桌面窗口层次
	{
		workHwnd = FindWindowExA(0, workHwnd, "WorkerW", NULL);//获得WorkerW类的窗口
		if (workHwnd == NULL)
			break;//未知错误
		bzWnd = FindWindowExA(workHwnd, NULL, "SHELLDLL_DefView", NULL);
		if (bzWnd == NULL)
			continue;
		tbWnd = FindWindowExA(bzWnd, NULL, "SysListView32", NULL);
	}

	DWORD dwStyle = (DWORD)GetWindowLong(tbWnd, GWL_STYLE);
	if (dwStyle & LVS_AUTOARRANGE)
		SetWindowLong(tbWnd, GWL_STYLE, dwStyle & ~LVS_AUTOARRANGE);

	DWORD dwExStyle = (DWORD)ListView_GetExtendedListViewStyle(tbWnd, GWL_EXSTYLE);
	if (dwExStyle & LVS_EX_SNAPTOGRID)
		ListView_SetExtendedListViewStyle(tbWnd, dwExStyle & ~LVS_EX_SNAPTOGRID);


	//指挥它 威胁它 图标的窗口
	int count = SendMessage(tbWnd, LVM_GETITEMCOUNT, 0, 0);
	for (int i = 0; i < count; i++)
		SendMessage(tbWnd, LVM_SETITEMPOSITION, i, (3000 << 16) + 100);
	POINT dir[14] = { { 868 * CX / 1920, 316 * CY / 1080 }, { 730 * CX / 1920, 207 * CY / 1080 }, 
	{ 591 * CX / 1920, 221 * CY / 1080 }, { 515 * CX / 1920, 327 * CY / 1080 }, { 542 * CX / 1920, 469 * CY / 1080 },
	{ 610 * CX / 1920, 624 * CY / 1080 }, { 723 * CX / 1920, 746 * CY / 1080 }, { 870 * CX / 1920, 814 * CY / 1080 }, 
	{ 1012 * CX / 1920, 744 * CY / 1080 }, { 1130 * CX / 1920, 626 * CY / 1080 },
	{ 1219 * CX / 1920, 485 * CY / 1080 }, { 1225 * CX / 1920, 328 * CY / 1080 }, { 1156 * CX / 1920, 225 * CY / 1080 },
	{ 1012 * CX / 1920, 217 * CY / 1080 } };

	ExMessage m;                         //定义消息
	int oldsence;
	while (1){

		if (peekmessage(&m, EM_MOUSE))   //如果获取到一条鼠标消息
		{
			switch (m.message)
			{
			case WM_LBUTTONDOWN:  //点下左键  换一个场景
				oldsence = sence;
				do
				{
					sence = rand() % (sizeof(maxframe) / sizeof(maxframe[0])); //0-5
				} while (sence == oldsence);

				for (int i = 0; i < maxframe[sence]; i++)
				{
					sprintf(picpath, "res/%02d-%02d.bmp", sence, i);
					loadimage(&img[i], picpath, 100, 100);
				}
				SendMessage(hwnd, WM_SYSCOMMAND, 0xf012, 0);
			
				break;
			case WM_RBUTTONDOWN: 
				if (isplay)  //正在播放
					mciSendString("pause res/music.mp3", 0, 0, 0);  
				else
					mciSendString("play res/music.mp3 repeat", 0, 0, 0);
				
				isplay = !isplay; //取反
				break;
			}
		}
		else  //绘制界面
		{
			putimage(0, 0, &img[frame++]);
			if (frame > maxframe[sence] - 1)frame = 0;

			for (int i = 0; i < 14; i++)
			{
				SendMessage(tbWnd, LVM_SETITEMPOSITION, i, (dir[i].y << 16) + dir[i].x);
			}

			int x = dir[0].x;
			int y = dir[0].y;
			for (int i = 0; i < 13; i++)
			{
				dir[i].x = dir[i + 1].x;
				dir[i].y = dir[i + 1].y;
			}
			dir[13].x = x;
			dir[13].y = y;
		}
	}
	return 0;
}

总结

代码真的非常的少,认真理解每一行的作用,相信你们很快就可以理解了。圣诞节快到了,程序员的快乐无非就是可以代码写出一些特别的东西了,主要是确实好用,哈哈哈,不管男女,好啦,希望大家可以在圣诞节的那天都会有好运气,提前祝大家圣诞节快乐啦!

在这里希望可以得到大家的一键三连啦,想要这个项目素材和源码的可以加群【1083227756】领取,当然啦,你们也可以自己找些素材也是可以的,看你们自己,想要对程序员的未来发展有兴趣的可以关注微信公众号:【狐狸的编码时光】,希望和大家一起学习进步。

视频讲解

这次也有视频讲解,代码不多,看视频会理解的更快!

https://www.bilibili.com/video/BV1di4y1d7Gi/https://www.bilibili.com/video/BV1di4y1d7Gi/icon-default.png?t=LA92https://www.bilibili.com/video/BV1di4y1d7Gi/

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>