数字雨

2 篇文章 0 订阅
#include <windows.h>
#include <ctime>
#include <cstdlib>
#define DEF_TIMER1 65536
#define DEF_TIMER1_TIME 100

LRESULT CALLBACK cb(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
char createChar();
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmd)
{

	WNDCLASSEX wd;
	HWND hWnd;
	MSG mSg;
	wd.cbClsExtra = 0;
	wd.cbSize = sizeof(wd);
	wd.cbWndExtra = 0;
	wd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wd.hCursor = NULL;
	wd.hIcon = NULL;
	wd.hIconSm = NULL;
	wd.hInstance = hInstance;

	wd.lpfnWndProc = cb;

	wd.lpszClassName = "NumberRain";
	wd.lpszMenuName = "Wa";
	wd.style = CS_HREDRAW | CS_VREDRAW;
	if (RegisterClassEx(&wd) == 0)
	{
		return -1;
	}

	hWnd = CreateWindow("NumberRain", NULL, WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL);

	if (hWnd == NULL)
	{
		return -1;
	}

	ShowWindow(hWnd, nCmd);
	ShowCursor(NULL);

	while (GetMessage(&mSg, hWnd, 0, 0))
	{
		TranslateMessage(&mSg);
		DispatchMessage(&mSg);
	}

	return 0;
}

const int numCount = 50;
int numPos[numCount] = { 0 };
char numRain[numCount][10];

LRESULT CALLBACK cb(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
	case WM_DESTROY:
		KillTimer(hWnd, DEF_TIMER1);
		PostQuitMessage(0);
		break;
	case WM_CREATE:
	{
		int i;
		SetTimer(hWnd, DEF_TIMER1, DEF_TIMER1_TIME, NULL);
		srand(unsigned(time));
		for (i = 0; i < numCount; ++i)
		{
			numPos[i] = rand() % GetSystemMetrics(SM_CYSCREEN);
		}
		break;
	}	
	case WM_TIMER:
	{
		HDC hDc = GetDC(hWnd);
		HDC hMemoryDc = CreateCompatibleDC(hDc);
		HBITMAP hBitMap = CreateCompatibleBitmap(hDc, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
		SelectObject(hMemoryDc, hBitMap);
		int i, j;
		for (i = 0; i < numCount; ++i)
		{
			for (j = 0; j < 10; ++j)
			{
				numRain[i][j] = createChar();
			}
		}
		HFONT hFont = CreateFont(15, 10, 0, 0, FW_DEMIBOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_SWISS, NULL);
		SelectObject(hMemoryDc, hFont);
		SetBkMode(hMemoryDc, TRANSPARENT);
		
		PatBlt(hDc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), BLACKNESS);
		for (i = 0; i < numCount; ++i)
		{
			for (j = 0; j < 10; ++j)
			{
				TextOut(hMemoryDc, i * 30, numPos[i] + 15 * j, &numRain[i][j], 1);
				SetTextColor(hMemoryDc, RGB(0, 255 - 25 * (j + 1), 0));

			}
		}
		BitBlt(hDc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hMemoryDc, 0, 0, SRCCOPY);
		for (i = 0; i < numCount; ++i)
		{
			numPos[i] += 15;
		}
		for (i = 0; i < numCount; ++i)
		{
			if (numPos[i] >= GetSystemMetrics(SM_CYSCREEN))
				numPos[i] = 0;
		}
		DeleteObject(hFont);
		DeleteObject(hBitMap);
		DeleteDC(hMemoryDc);
		DeleteDC(hDc);
	}
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_ESCAPE:
			SendMessage(hWnd, WM_DESTROY, 0, 0);
		default:
			break;
		}
	default:
		break;
	}

	return DefWindowProc(hWnd, nMsg, wParam, lParam);
}

char createChar()
{
	int num = -1;
	while (1)
	{
		num = rand() % 123;
		if ((num >= '0' &&num <= '9') || (num >= 'A'&&num <= 'Z') || (num >= 'a'&&num <= 'z'))
			break;
	}
	return num;
}

会全屏动态显示如下效果:

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值