数字雨程序源码

#include 
#include 
#include 

#define FONTWIDTH 0x10
#define FONTHEIGHT 0x14
#define SCREENWIDTH ScreenX / FONTWIDTH
#define SCREENHEIGHT ScreenY / FONTHEIGHT
#define MAXLENGTH 0x20
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int ScreenX, ScreenY;

struct strnode
{
    int x, y;
    int len;
    strnode *next;
} node;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    ScreenX = GetSystemMetrics(SM_CXSCREEN);
    ScreenY = GetSystemMetrics(SM_CYSCREEN);
    static TCHAR szAppName[] = L"数字雨";
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc  = WndProc ;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH) ;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if(!RegisterClass (&wndclass))
    {
        return 0;
    }

    hwnd = CreateWindow (szAppName, NULL,
        WS_DLGFRAME | WS_THICKFRAME | WS_POPUP,
        0, 0,
        GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
        NULL, NULL, hInstance,
        NULL);
    ShowWindow (hwnd, SW_SHOWMAXIMIZED);
    UpdateWindow(hwnd);
    ShowCursor(FALSE);
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int Chess[200][200];
    static int r = 10;
    HDC hdc ;
    static int cxScreen, cyScreen;
    static HDC hdcMem;
    static HFONT hFont, font;
    static HBITMAP hBitmap;
    int iColumnCount;
    WCHAR szNumber[0x10];
    strnode *Node, *TNode;
    static RECT CCodeRect; 
    static HBRUSH Brush;
    static HPEN Pen;
    static FILE *fp;
    CHAR strBuf[0x100];
    switch (message)
    {
    case WM_CREATE:
        cxScreen = GetSystemMetrics(SM_CXSCREEN);
        cyScreen = GetSystemMetrics(SM_CYSCREEN);
        hdc = GetDC(hwnd);
        hdcMem = CreateCompatibleDC(hdc);
        hBitmap = CreateCompatibleBitmap(hdc, cxScreen, cyScreen);
        SelectObject(hdcMem, hBitmap);
        ReleaseDC(hwnd, hdc);
        hFont = CreateFont(0x18, 0x0C, 0, 0, 0x100, 0, 0, 0,
            DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
            DRAFT_QUALITY, FIXED_PITCH | FF_SWISS, TEXT("Agency FB"));
        font = CreateFont(0x08, 0x04, 0, 0, 0, 0, 0, 0,
            DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
            DRAFT_QUALITY, FIXED_PITCH | FF_SWISS, TEXT("Arial"));
        SelectObject(hdcMem, hFont);
        SetBkMode(hdcMem, TRANSPARENT);
        for (int i = 0;i < 200;i++)
        {
            for (int j = 0;j < 200;j++)
            {
                Chess[i][j] = rand() % 0x10;
            }
        }
        Node = &node;
        for (int i = 0;i < 0x10;i++)
        {
            Node->x = rand() % SCREENWIDTH;
            Node->y = rand() % SCREENHEIGHT;
            Node->len = rand() % MAXLENGTH;
            Node->next = new strnode;
            Node = Node->next;
        }
        Node->x = rand() % SCREENWIDTH;
        Node->y = rand() % SCREENHEIGHT;
        Node->next = NULL;
        CCodeRect.top = 10;
        CCodeRect.left = 10;
        CCodeRect.right = 300;
        CCodeRect.bottom = 300;
        Brush = CreateSolidBrush(RGB(0, 0, 0));
        Pen = CreatePen(PS_SOLID, 1, RGB(0, -1, 0));
        //fp = fopen("code.txt", "r");
        SetTimer (hwnd, 1, 50, NULL);
        srand ((int)GetCurrentTime());
        return 0;
    case WM_TIMER:
        for (int i = 0;i < 0x50;i++)
        {
            //每次有0x50个字符改变
            Chess[rand() % 100][rand() % 100] = rand() % 0x10;
        }
        hdc = GetDC(hwnd);
        PatBlt(hdcMem, 0, 0, cxScreen, cyScreen, BLACKNESS);
        SetTextColor(hdcMem, RGB(0, 0x80, 0));
        for (int i = 0;i < 100;i++)
        {
            for (int j = 0;j < 100;j++)
            {
                wsprintf(szNumber, L"%X", Chess[i][j]);
                TextOut(hdcMem, i * FONTWIDTH, j * FONTHEIGHT, szNumber, 1);
            }
        }
        TNode = &node;
        while (TNode)
        {
            SetTextColor(hdcMem, RGB(0, -1, 0));
            for (int i = TNode->y - 1;(i >= 0)&&(TNode->y - i < TNode->len);i--)
            {
                wsprintf(szNumber, L"%X", Chess[TNode->x][i]);
                TextOut(hdcMem, FONTWIDTH * TNode->x, i * FONTHEIGHT, szNumber, 1);
            }
            SetTextColor(hdcMem, RGB(-1, -1, -1));
            wsprintf(szNumber, L"%X", Chess[TNode->x][TNode->y]);
            TextOut(hdcMem, FONTWIDTH * TNode->x,  TNode->y * FONTHEIGHT, szNumber, 1);
            wsprintf(szNumber, L"%X", Chess[TNode->x][TNode->y + 1]);
            TextOut(hdcMem, FONTWIDTH * TNode->x,  (TNode->y + 1) * FONTHEIGHT, szNumber, 1);
            SetTextColor(hdcMem, RGB(0, 0x80, 0));
            TNode->y = TNode->y + 1;
            if (TNode->y == SCREENHEIGHT + TNode->len + 2)
            {
                TNode->x = rand() % SCREENWIDTH;
                TNode->len = rand() % MAXLENGTH;
                TNode->y = 0;
            }
            TNode = TNode->next;
        }
        SelectObject(hdcMem, hFont);
        BitBlt(hdc, 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY);
        ReleaseDC(hwnd, hdc);
        return 0;
        //  case WM_KEYDOWN:
    case WM_LBUTTONDOWN:
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

你可能感兴趣的:(SDK)