Share via


BeginBufferedAnimation, fonction (uxtheme.h)

Commence une opération d’animation mise en mémoire tampon. L’animation se compose d’un fondu croisé entre le contenu de deux mémoires tampons sur une période spécifiée.

Syntaxe

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
);

Paramètres

hwnd

Type : HWND

Handle de la fenêtre dans laquelle les animations sont lues.

hdcTarget

Type : HDC

Handle du contrôleur de domaine cible sur lequel la mémoire tampon est animée.

prcTarget

Type : const RECT*

Pointeur vers une structure qui spécifie la zone du contrôleur de domaine cible dans laquelle dessiner.

dwFormat

Type : BP_BUFFERFORMAT

Format de la mémoire tampon.

[in] pPaintParams

Type : BP_PAINTPARAMS*

Pointeur vers une structure qui définit les paramètres de l’opération de peinture. Cette valeur peut être NULL.

[in] pAnimationParams

Type : BP_ANIMATIONPARAMS*

Pointeur vers une structure qui définit les paramètres de l’opération d’animation.

[out] phdcFrom

Type : HDC*

Lorsque cette fonction retourne, cette valeur pointe vers le handle du contrôleur de domaine où l’application doit peindre l’état initial de l’animation, sinon NULL.

[out] phdcTo

Type : HDC*

Lorsque cette fonction retourne, cette valeur pointe vers le handle du contrôleur de domaine où l’application doit peindre l’état final de l’animation, si ce n’est pas NULL.

Valeur retournée

Type : HANIMATIONBUFFER

Poignée de l’animation de peinture mise en mémoire tampon.

Remarques

BeginBufferedAnimation s’occupe de dessiner les cadres intermédiaires entre ces deux états en générant plusieurs messages WM_PAINT .

BeginBufferedAnimation démarre un minuteur qui génère WM_PAINT messages sur lesquels BufferedPaintRenderAnimation doit être appelé. Pendant ces messages, BufferedPaintRenderAnimation retourne TRUE lorsqu’il peint un cadre intermédiaire, pour indiquer que l’application n’a plus de peinture à effectuer.

Si la durée de l’animation est égale à zéro, seul phdcTo est retourné et phdcFrom est défini sur NULL. Dans ce cas, l’application doit peindre l’état final à l’aide de phdcTo pour obtenir le comportement similaire à BeginBufferedPaint.

Exemples

L’exemple de code suivant montre comment utiliser cette fonction.

#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);
    }
}

Configuration requise

Condition requise Valeur
Client minimal pris en charge Windows Vista [applications de bureau uniquement]
Serveur minimal pris en charge Windows Server 2008 [applications de bureau uniquement]
Plateforme cible Windows
En-tête uxtheme.h
DLL UxTheme.dll