雪花的代码

作者在 2011-01-19 19:51:25 发布以下内容

#include <windows.h>
#include <stdlib.h>

#define         WINCX       300
#define         WINCY       400
#define         UNICODE
#define         _UNICODE

#define         ID_TIME     1


typedef struct tagSNOW
{
    POINT       pos;
    int         r;
    int         xSpeed;
    int         ySpeed;
}
SNOW, *PSNOW;

#pragma comment (linker, "/subsystem:windows")

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    TCHAR                       szAppName[] = TEXT ("clsDesktopSnowing");
    TCHAR                       szTitle[] = TEXT ("Desktop Snowing (Crazyp)");
    HWND                        hWnd;
    MSG                         msg;
    WNDCLASS                    wc;

    int                         screen_x, screen_y;
    int                         x, y;

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

    if ( !RegisterClass (&wc) )
    {
        MessageBox (NULL, TEXT ("RegisterClass Error"), TEXT ("ERROR"), MB_OK | MB_ICONINFORMATION);
        return -1;
    }

    screen_x = GetSystemMetrics (SM_CXSCREEN);
    screen_y = GetSystemMetrics (SM_CYSCREEN);
    x = (screen_x - WINCX) / 2;
    y = (screen_y - WINCY) / 2;


    hWnd = CreateWindow (
        szAppName,
        szTitle,
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        0, 0,
        screen_x, screen_y,
        NULL, NULL, hInstance, NULL);

    if ( !hWnd )
    {
        MessageBox (NULL, TEXT ("CreateWindow Error"), TEXT ("ERROR"), MB_OK | MB_ICONINFORMATION);
        return -2;
    }

    SetWindowPos (hWnd, HWND_TOPMOST, 0, 0, screen_x, screen_y, SWP_NOMOVE);
    //ShowWindow (hWnd, SW_SHOW);
    //UpdateWindow (hWnd);

    while ( GetMessage (&msg, NULL, 0, 0) > 0 )
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int          iScrWidth, iScrHeight;
    static HDC          hScrDC;
    static HDC          hSaveDC;
    static HDC          hMemDC;
    static HBITMAP      hSaveBmp;
    static HBITMAP      hBmp;
    static SNOW         snows[100];
    static RECT         rtScr;

    switch ( message )
    {
    case WM_CREATE:
        {
            int         i;
            iScrWidth = GetSystemMetrics (SM_CXSCREEN);
            iScrHeight = GetSystemMetrics (SM_CYSCREEN);
            hScrDC = CreateDC ("DISPLAY", NULL, NULL, NULL);
            hSaveDC = CreateCompatibleDC (hScrDC);
            hMemDC = CreateCompatibleDC (hScrDC);
            hSaveBmp = CreateCompatibleBitmap (hScrDC, iScrWidth, iScrHeight);
            hBmp = CreateCompatibleBitmap (hScrDC, iScrWidth, iScrHeight);
            SelectObject (hSaveDC, hSaveBmp);
            SelectObject (hMemDC, hBmp);
            BitBlt (hSaveDC, 0, 0, iScrWidth, iScrHeight, hScrDC, 0, 0, SRCCOPY);

            SetRect (&rtScr, 0, 0, iScrWidth, iScrHeight);
            srand ((unsigned) GetTickCount ());
            for (i = 0; i < 100; ++i)
            {
                snows[i].pos.x = rand () % iScrWidth;
                snows[i].pos.y = rand () % (iScrHeight / 3);
                snows[i].r = rand () % 5 + 1;
                snows[i].xSpeed = rand () % 3 -1;
                snows[i].ySpeed = rand () % 5 + 1;
            }
            ShowCursor (FALSE);

            SetTimer (hWnd, ID_TIME, 10, NULL);
        }
        break;
    case WM_TIMER:
        {
            int         i;
            for (i = 0; i < 100; ++i)
            {
                snows[i].pos.x += snows[i].xSpeed;
                snows[i].pos.y += snows[i].ySpeed;
                if ( !PtInRect (&rtScr, snows[i].pos) )
                {
                    snows[i].pos.x = rand () % iScrWidth;
                    snows[i].pos.y = rand () % (iScrHeight / 3);
                }
            }

            BitBlt (hMemDC, 0, 0, iScrWidth, iScrHeight, hSaveDC, 0, 0, SRCCOPY);
            for (i = 0; i < 100; ++i)
            {
                SelectObject (hMemDC, GetStockObject (NULL_PEN));
                Ellipse (hMemDC, snows[i].pos.x, snows[i].pos.y, snows[i].pos.x + 2 * snows[i].r, snows[i].pos.y + 2 * snows[i].r);
            }
            BitBlt (hScrDC, 0, 0, iScrWidth, iScrHeight, hMemDC, 0, 0, SRCCOPY);
        }
        break;
    case WM_LBUTTONDOWN:
        {
            SendMessage (hWnd, WM_CLOSE, 0, 0);
        }
        break;
    case WM_DESTROY:
        {
            ShowCursor (TRUE);
            KillTimer (hWnd, ID_TIME);
            DeleteObject (hBmp);
            DeleteObject (hSaveBmp);
            DeleteDC (hMemDC);
            DeleteDC (hSaveDC);
            DeleteDC (hScrDC);
            InvalidateRect (NULL, NULL, TRUE);
            PostQuitMessage (0);
        }
        break;
    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }

    return 0;
}


默认分类 | 阅读 880 次
文章评论,共0条
游客请输入验证码
浏览2905次
文章分类
文章归档
最新评论