温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言怎么绘制圣诞水晶球

发布时间:2021-12-27 13:53:24 来源:亿速云 阅读:169 作者:iii 栏目:开发技术

本篇内容介绍了“C语言怎么绘制圣诞水晶球”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

先给大家看效果

C语言怎么绘制圣诞水晶球

项目代码

这是我第一次只用一个代码段,因为代码确实不多,我觉得分段就没必要了,而且其中必要的地方,我也加了详细的注释,应该还好,难就难在之前没有用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;
}

“C语言怎么绘制圣诞水晶球”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI