#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;
}
会全屏动态显示如下效果: