Share via


Função BeginBufferedAnimation (uxtheme.h)

Inicia uma operação de animação em buffer. A animação consiste em um esmaecimento cruzado entre o conteúdo de dois buffers durante um período de tempo especificado.

Sintaxe

HANIMATIONBUFFER BeginBufferedAnimation(
        HWND               hwnd,
        HDC                hdcTarget,
        const RECT         *prcTarget,
        BP_BUFFERFORMAT    dwFormat,
  [in]  BP_PAINTPARAMS     *pPaintParams,
  [in]  BP_ANIMATIONPARAMS *pAnimationParams,
  [out] HDC                *phdcFrom,
  [out] HDC                *phdcTo
);

Parâmetros

hwnd

Tipo: HWND

Um identificador para a janela na qual as animações são reproduzidas.

hdcTarget

Tipo: HDC

Um identificador do DC de destino no qual o buffer é animado.

prcTarget

Tipo: const RECT*

Um ponteiro para uma estrutura que especifica a área do DC de destino na qual desenhar.

dwFormat

Tipo: BP_BUFFERFORMAT

O formato do buffer.

[in] pPaintParams

Tipo: BP_PAINTPARAMS*

Um ponteiro para uma estrutura que define os parâmetros de operação de pintura. Esse valor pode ser NULL.

[in] pAnimationParams

Tipo: BP_ANIMATIONPARAMS*

Um ponteiro para uma estrutura que define os parâmetros de operação de animação.

[out] phdcFrom

Tipo: HDC*

Quando essa função retorna, esse valor aponta para o identificador do DC em que o aplicativo deve pintar o estado inicial da animação, se não FOR NULL.

[out] phdcTo

Tipo: HDC*

Quando essa função retorna, esse valor aponta para o identificador do DC em que o aplicativo deve pintar o estado final da animação, se não FOR NULL.

Retornar valor

Tipo: HANIMATIONBUFFER

Um identificador para a animação de pintura em buffer.

Comentários

BeginBufferedAnimation cuidará do desenho dos quadros intermediários entre esses dois estados gerando várias mensagens WM_PAINT .

BeginBufferedAnimation inicia um temporizador que gera WM_PAINT mensagens nas quais BufferedPaintRenderAnimation deve ser chamado. Durante essas mensagens, BufferedPaintRenderAnimation retornará TRUE quando pintar um quadro intermediário, para indicar que o aplicativo não tem mais pintura para fazer.

Se a duração da animação for zero, somente phdcTo será retornado e phdcFrom será definido como NULL. Nesse caso, o aplicativo deve pintar o estado final usando phdcTo para obter o comportamento semelhante a BeginBufferedPaint.

Exemplos

O exemplo de código a seguir mostra como usar essa função.

#include <windows.h>
#include <tchar.h>
#include <uxtheme.h>

#pragma comment(lib, "uxtheme.lib")

#define WNDCLASSNAME L"BufferedPaintSample_WndClass"
#define ANIMATION_DURATION 500

bool g_fCurrentState = true;
bool g_fNewState = true;

void StartAnimation(HWND hWnd)
{
    g_fNewState = !g_fCurrentState;
    InvalidateRect(hWnd, NULL, TRUE);
}

void Paint(HWND hWnd, HDC hdc, bool state)
{
    RECT rc;
    GetClientRect(hWnd, &rc);
    FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
    LPCTSTR pszIconId = state ? IDI_APPLICATION : IDI_ERROR;
    HICON hIcon = LoadIcon(NULL, pszIconId);
    if (hIcon)
    {
        DrawIcon(hdc, 10, 10, hIcon);
        DestroyIcon(hIcon);
    }
}

void OnPaint(HWND hWnd)
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);
    if (hdc)
    {
        // See if this paint was generated by a soft-fade animation
        if (!BufferedPaintRenderAnimation(hWnd, hdc))
        {
            BP_ANIMATIONPARAMS animParams;
            ZeroMemory(&animParams, sizeof(animParams));
            animParams.cbSize = sizeof(BP_ANIMATIONPARAMS);
            animParams.style = BPAS_LINEAR;
            
            // Check if animation is needed. If not set dwDuration to 0
            animParams.dwDuration = (g_fCurrentState != g_fNewState ? ANIMATION_DURATION : 0);

            RECT rc;
            GetClientRect(hWnd, &rc);

            HDC hdcFrom, hdcTo;
            HANIMATIONBUFFER hbpAnimation = BeginBufferedAnimation(hWnd, hdc, &rc,
                BPBF_COMPATIBLEBITMAP, NULL, &animParams, &hdcFrom, &hdcTo);
            if (hbpAnimation)
            {
                if (hdcFrom)
                {
                    Paint(hWnd, hdcFrom, g_fCurrentState);
                }
                if (hdcTo)
                {
                    Paint(hWnd, hdcTo, g_fNewState);
                }

                g_fCurrentState = g_fNewState;
                EndBufferedAnimation(hbpAnimation, TRUE);
            }
            else
            {
                Paint(hWnd, hdc, g_fCurrentState);
            }
        }
        
        EndPaint(hWnd, &ps);
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_LBUTTONDOWN:
        StartAnimation(hWnd);
        break;
    case WM_PAINT:
        OnPaint(hWnd);
        break;
    case WM_SIZE:
        BufferedPaintStopAllAnimations(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI _tWinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR     lpCmdLine,
     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    if (SUCCEEDED(BufferedPaintInit()))
    {
        WNDCLASSEX wcex;
        ZeroMemory(&wcex, sizeof(wcex));
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_HREDRAW|CS_VREDRAW;
        wcex.lpfnWndProc = WndProc;
        wcex.hInstance = hInstance;
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wcex.lpszClassName = WNDCLASSNAME;
        RegisterClassEx(&wcex);

        HWND hWnd = CreateWindow(WNDCLASSNAME, L"Buffered Paint Sample", 
            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 
            CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

        if (hWnd)
        {
            ShowWindow(hWnd, nCmdShow);
            UpdateWindow(hWnd);

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

        BufferedPaintUnInit();
    }

    return 0;
}

void BufferedPaint(HDC hdc, const RECT *prcPaint)
{
    BP_PAINTPARAMS paintParams = {0};
    paintParams.cbSize = sizeof(paintParams);

    HDC hdcBuffer;
    HPAINTBUFFER hBufferedPaint = BeginBufferedPaint(hdc, prcPaint, 
        BPBF_COMPATIBLEBITMAP, &paintParams, &hdcBuffer);

    if (hBufferedPaint)
    {
        // Application specific painting code
        AppPaint(hdcBuffer, prcPaint);
        EndBufferedPaint(hBufferedPaint, TRUE);
    }
    else
    {
        // Error occurred, default to unbuffered painting
        AppPaint(hdc, prcPaint);
    }
}

Requisitos

Requisito Valor
Cliente mínimo com suporte Windows Vista [somente aplicativos da área de trabalho]
Servidor mínimo com suporte Windows Server 2008 [somente aplicativos da área de trabalho]
Plataforma de Destino Windows
Cabeçalho uxtheme.h
DLL UxTheme.dll