kehuadong

windows rb_tree动画


#define UNICODE
#include <windows.h>
#include <windowsx.h>

#include <stdbool.h>
#include <stdio.h>

typedef struct ball_t ball_t;

struct ball_t
{
    int src_x;
    int src_y;

    int target_x;
    int target_y;
};

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 480;
const int BALL_DIAMETER = 30;

HDC hdc;
HDC hdcMem;
HBRUSH hbrBlack;

bool bSliderActive;
RECT rcSlider = {20, 440, 780, 470};
int m_slider_percent;

ball_t ball_1 = {0, 0, 100, 100};

void draw_ball(HDC hdc, ball_t* ball, int percent)
{
    int x = ball->src_x + (ball->target_x - ball->src_x) * percent / 100;
    int y = ball->src_y + (ball->target_y - ball->src_y) * percent / 100;
    Ellipse(hdc, x,  y, x + BALL_DIAMETER, y + BALL_DIAMETER);
}

void do_paint(HDC hdc)
{
    draw_ball(hdc, &ball_1, m_slider_percent);

    Rectangle(hdc, 20, 440, 780, 470);
    if (m_slider_percent)
    {
        RECT rc = {23, 443, 23+(777-23)*m_slider_percent/100, 467};
        FillRect(hdc, &rc, hbrBlack);
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_CREATE:
        {
            hdc = GetDC(hWnd);
            
            hdcMem = CreateCompatibleDC(hdc);
            HBITMAP hbmp = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
            SelectObject(hdcMem, hbmp);

            hbrBlack = CreateSolidBrush(RGB(0, 0, 0));

            SetTimer(hWnd, 0, 16, NULL);
            break;
        }

        case WM_TIMER:
        {
            static DWORD last_tick = 0;
            if (last_tick == 0)
            {
                last_tick = GetTickCount();
            }

            DWORD now_tick = GetTickCount();
            while (now_tick - last_tick >= 16)
            {
                last_tick += 16;
                RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
                FillRect(hdcMem, &rc, (HBRUSH)(COLOR_WINDOW+1));
                do_paint(hdcMem);
                BitBlt(hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);
            }
            break;
        }

        case WM_LBUTTONDOWN:
        {
            POINT pt =  {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
            if (PtInRect(&rcSlider, pt))
            {
                bSliderActive = true;
            }
            break;
        }

        case WM_LBUTTONUP:
            bSliderActive = false;
            break;

        case WM_MOUSEMOVE:
        {
            if (bSliderActive)
            {
                int x = GET_X_LPARAM(lParam);
                if (x <= rcSlider.left)
                {
                    m_slider_percent = 0;
                }
                else if (x >= rcSlider.right)
                {
                    m_slider_percent = 100;
                }
                else
                {
                    m_slider_percent = (x - rcSlider.left) * 100 / (rcSlider.right-rcSlider.left);
                }
            }
            break;
        }

        case WM_DESTROY:
            ReleaseDC(hWnd, hdc);
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

int main()
{
    WNDCLASS wc = {0};
    wc.lpszClassName = L"RbTreeAnimation";
    wc.lpfnWndProc = WndProc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wc);

    RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW&~WS_THICKFRAME, FALSE);

    HWND hWnd = CreateWindow(wc.lpszClassName, L"RbTreeAnimation", WS_OVERLAPPEDWINDOW&~WS_THICKFRAME,
        CW_USEDEFAULT, CW_USEDEFAULT, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, wc.hInstance, NULL);
    UpdateWindow(hWnd);
    ShowWindow(hWnd, SW_SHOWDEFAULT);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

 

posted on 2024-09-24 19:53  kehuadong  阅读(3)  评论(0编辑  收藏  举报

导航