Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525313
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-01-24 20:22:35

我想大家对位图这一章节里的动画小球印象深刻吧, 确实是一个非常不错的程序, 我修改了一下,作者写得有点复杂了,看看:
源代码:


#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 PSTR szCmdLine,
                 int iCmdShow) {
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    static TCHAR szAppName[] = TEXT("XZZ");
    
    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 = GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = szAppName;
    wndclass.lpszClassName = szAppName;
    
    if(!RegisterClass(&wndclass)) {
        MessageBox(NULL, TEXT("Register failure..."),
            szAppName, MB_ICONERROR);
        return 0;
    }
    
    hwnd = CreateWindow(szAppName,
        szAppName,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);
    
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while(GetMessage(&msg, NULL, 0, 0)) {
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam) {
    static int cxBitmap, cyBitmap, cxClient, cyClient, sign = 1,
        sign1 = 1;
    static HBITMAP hBitmap;
    static HDC hdcMem;
    HDC hdc;
    PAINTSTRUCT ps;
    HBRUSH hBrush;
    static POINT pos;

    switch(message) {
    case WM_CREATE:
        hdc = GetDC(hwnd);
        hdcMem = CreateCompatibleDC(hdc);
        // 创建一个宽与高是128像素的矩形位图

        hBitmap = CreateCompatibleBitmap(hdc, 128, 128);
        SelectObject(hdcMem, hBitmap);
        // 用白色填充整个位图区域,我发现默认颜色是黑色。

        Rectangle(hdcMem, -1, -1, 130, 130);
        hBrush = CreateSolidBrush(RGB(0, 255, 0));
        SelectObject(hdcMem, hBrush);
        // 在位图区域中央画一个圆形,半径为32

        Ellipse(hdcMem, 32, 32, 96, 96);
        SetTimer(hwnd, 1, 50, NULL);
        DeleteObject(hBrush);
        return 0;
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        pos.x = cxClient / 2;
        pos.y = cyClient / 2;
        return 0;
    case WM_TIMER:
        hdc = GetDC(hwnd);
        // 使位图中心与客户区域中心重叠在一起

        BitBlt(hdc, pos.x - 64, pos.y - 64, 128,
            128, hdcMem, 0, 0, SRCCOPY);
        pos.x += 16 * sign;
        pos.y += 16 * sign1;
        if(pos.x + 32 >= cxClient || pos.x - 32 <= 0)
            sign = -sign;
        if(pos.y + 32 >= cyClient || pos.y - 32 <= 0)
            sign1 = -sign1;
        return 0;
    case WM_DESTROY:
        if(hBitmap)
            DeleteObject(hBitmap);
        DeleteDC(hdcMem);
        KillTimer(hwnd, 1);
        PostQuitMessage(0);
        return 0;

    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}


阅读(830) | 评论(0) | 转发(0) |
0

上一篇:小的画图程序

下一篇:字符集和Unicode

给主人留下些什么吧!~~