如何使用 Windows API 在透明窗口上绘制动画?

发布于 2024-10-09 08:51:16 字数 6466 浏览 8 评论 0原文

我正在尝试使用 Windows API 在具有透明背景的窗口上绘制动画。问题是我无法从窗口中删除先前的绘图。

我设置了以下参数:

InvalidateRect(m_hWnd, &sClientRect, TRUE);  // we set the bErase parameter as TRUE
paintParams.dwFlags = BPPF_ERASE; // erase window content while copying backbuffer
paintParams.pBlendFunction = &m_sBlendfunc; // copy source image to backbuffer

但还是不行。您可以在附图中看到结果。我想要的动画是在屏幕上移动圆圈。相反,我得到的(如附图所示)是它们运动的伪影,因为每次绘制之前窗口都没有被清除。

请参阅下面的完整代码:

#include "DrawTest.h"

DrawTest* m_sDrawTest;

DrawTest::DrawTest()
{
 m_pAnimation = NULL;
 m_sDrawTest = NULL;
 m_nFrameindex = 0;
}

DrawTest::~DrawTest(void)
{
 if(m_pAnimation)
  delete m_pAnimation;
 m_pAnimation = NULL;
 if(m_sDrawTest)
  delete m_sDrawTest;
 m_sDrawTest = NULL;
}

int DrawTest::Init(AnimationManager* pAnimationManager,HINSTANCE hInstance,int nCmdShow)
{
 //listener
 m_sDrawTest = this;

 //get anemation (sequence of frames containing location and png's);
 m_pAnimation = pAnimationManager->GetAnimation(2);


 //set window class information
 WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc    = WndDrawTestProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)GetStockObject(HOLLOW_BRUSH);//configures the window to use transparrent brush
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = sWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

 m_hInst = hInstance; // Store instance handle in our global variable

 m_hWnd = CreateWindow(
        sWindowClass,
        sTitle,
  WS_POPUP,
        200, 200,
        1024, 600,
        NULL,
        NULL,
        hInstance,
        NULL
    );


 if (!m_hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

 SetWindowPos(m_hWnd,   // handle to window
     HWND_TOPMOST,  // top z
     0,             // ignored
     0,             // ignored
     0,             // ignored
     0,             // ignored
     SWP_NOSIZE | SWP_NOMOVE);

 // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(m_hWnd,
        nCmdShow);
    UpdateWindow(m_hWnd);

   return 1;

}



// Called by an external timer. This is the application "Next Step" proc.
void DrawTest::TimeStep(){
 PostMessage(m_hWnd, WM_PAINT, 0, 0);
}


//  WndDrawTestProc replaces the default DefWindowProc
//
//  FUNCTION: WndDrawTestProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndDrawTestProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {

 case WM_ERASEBKGND:
  return DefWindowProc(hWnd, message, wParam, lParam);
 case WM_PAINT:

  // call onNextFrame to draw current frame.
  m_sDrawTest->OnNextFrame(hWnd); 

  // ensures that the window is in top most position
  SetWindowPos(hWnd,   // handle to window
     HWND_TOPMOST,  // top z
     0,             // ignored
     0,             // ignored
     0,             // ignored
     0,             // ignored
     SWP_NOSIZE | SWP_NOMOVE);

        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}



/* 
DrawTest::OnNextFrame
Called by WndDrawTestProc when receving WM_PAINT message 
Configures the drawing canvas and calles DrawTest::Draw(HDC hBBDC) to do the actual drawing
*/
void DrawTest::OnNextFrame(HWND hUILoopWnd)
{
 if(m_nFrameindex > m_pAnimation->GetNumOfFrames() - 1)
  return;
 // defines paint area
 RECT sClientRect;
 GetClientRect(hUILoopWnd, &sClientRect);
 InvalidateRect(m_hWnd, &sClientRect, TRUE);  // we set the bErase parameter as TRUE


 //blending structure 
 m_sBlendfunc.BlendOp= AC_SRC_OVER;
 m_sBlendfunc.BlendFlags = 0;
 m_sBlendfunc.SourceConstantAlpha = 255;
 m_sBlendfunc.AlphaFormat = AC_SRC_ALPHA;


 HDC hdc;
 PAINTSTRUCT ps;

 hdc = BeginPaint(hUILoopWnd, &ps);

 GetClientRect(hUILoopWnd, &sClientRect);
 BP_PAINTPARAMS paintParams =  { sizeof(BP_PAINTPARAMS) };
 paintParams.dwFlags = BPPF_ERASE; // erase window content while copying backbuffer
 paintParams.pBlendFunction = &m_sBlendfunc; // how to copy source image to backbuffer
 HDC hBBDC;
 HPAINTBUFFER hPBuffer;


 paintParams.cbSize = sizeof(paintParams);

 hPBuffer = BeginBufferedPaint(hdc, &sClientRect, BPBF_COMPATIBLEBITMAP, &paintParams, &hBBDC);

 //draw animation
 Draw(hBBDC);

 m_nFrameindex ++;


 EndBufferedPaint(hPBuffer, TRUE);
 EndPaint(hUILoopWnd, &ps);
}


/*
Draw
Paint the animation frame on the backbuffer
*/

void DrawTest::Draw(HDC hBBDC)
{ 
 HDC hdcScreen = GetDC(NULL);
 HDC hdcMem = CreateCompatibleDC(hdcScreen);

 bool test = false;  

 HGDIOBJ hbmpOld = SelectObject(hdcMem, m_pAnimation->m_pFramesArray[m_nFrameindex]->hBmp);
 HBITMAP ptemp = CreateCompatibleBitmap(hdcMem,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth,
  m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight);

 DeleteObject(ptemp);

 test = AlphaBlend(hBBDC,m_pAnimation->m_pFramesArray[m_nFrameindex]->nPtX,m_pAnimation->m_pFramesArray[m_nFrameindex]->nPtY 
  ,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth, m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight,
   hdcMem,0,0,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth,m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight,m_sBlendfunc);

 DWORD  test10 = GetLastError();

 SelectObject(hdcMem, hbmpOld); //placing the old object back
 test = DeleteDC(hdcMem);  // after CreateCompatibleDC
 test = ReleaseDC(NULL, hdcScreen); // after GetDC
}

这是 5 帧后的结果: 替代文字

I'm trying to draw an animation on a window with a transparent background using Windows API. The problem is that I can't delete the previous drawing from the window.

I set the following parameters:

InvalidateRect(m_hWnd, &sClientRect, TRUE);  // we set the bErase parameter as TRUE
paintParams.dwFlags = BPPF_ERASE; // erase window content while copying backbuffer
paintParams.pBlendFunction = &m_sBlendfunc; // copy source image to backbuffer

But it still doesn't work. You can see the result in the attached image. The animation I wanted is moving the circles across the screen. What I get instead (as shown in the attached image) is artifacts of their motion because the window is not cleared before each draw.

See my full code below:

#include "DrawTest.h"

DrawTest* m_sDrawTest;

DrawTest::DrawTest()
{
 m_pAnimation = NULL;
 m_sDrawTest = NULL;
 m_nFrameindex = 0;
}

DrawTest::~DrawTest(void)
{
 if(m_pAnimation)
  delete m_pAnimation;
 m_pAnimation = NULL;
 if(m_sDrawTest)
  delete m_sDrawTest;
 m_sDrawTest = NULL;
}

int DrawTest::Init(AnimationManager* pAnimationManager,HINSTANCE hInstance,int nCmdShow)
{
 //listener
 m_sDrawTest = this;

 //get anemation (sequence of frames containing location and png's);
 m_pAnimation = pAnimationManager->GetAnimation(2);


 //set window class information
 WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc    = WndDrawTestProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)GetStockObject(HOLLOW_BRUSH);//configures the window to use transparrent brush
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = sWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

 m_hInst = hInstance; // Store instance handle in our global variable

 m_hWnd = CreateWindow(
        sWindowClass,
        sTitle,
  WS_POPUP,
        200, 200,
        1024, 600,
        NULL,
        NULL,
        hInstance,
        NULL
    );


 if (!m_hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

 SetWindowPos(m_hWnd,   // handle to window
     HWND_TOPMOST,  // top z
     0,             // ignored
     0,             // ignored
     0,             // ignored
     0,             // ignored
     SWP_NOSIZE | SWP_NOMOVE);

 // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(m_hWnd,
        nCmdShow);
    UpdateWindow(m_hWnd);

   return 1;

}



// Called by an external timer. This is the application "Next Step" proc.
void DrawTest::TimeStep(){
 PostMessage(m_hWnd, WM_PAINT, 0, 0);
}


//  WndDrawTestProc replaces the default DefWindowProc
//
//  FUNCTION: WndDrawTestProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndDrawTestProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {

 case WM_ERASEBKGND:
  return DefWindowProc(hWnd, message, wParam, lParam);
 case WM_PAINT:

  // call onNextFrame to draw current frame.
  m_sDrawTest->OnNextFrame(hWnd); 

  // ensures that the window is in top most position
  SetWindowPos(hWnd,   // handle to window
     HWND_TOPMOST,  // top z
     0,             // ignored
     0,             // ignored
     0,             // ignored
     0,             // ignored
     SWP_NOSIZE | SWP_NOMOVE);

        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}



/* 
DrawTest::OnNextFrame
Called by WndDrawTestProc when receving WM_PAINT message 
Configures the drawing canvas and calles DrawTest::Draw(HDC hBBDC) to do the actual drawing
*/
void DrawTest::OnNextFrame(HWND hUILoopWnd)
{
 if(m_nFrameindex > m_pAnimation->GetNumOfFrames() - 1)
  return;
 // defines paint area
 RECT sClientRect;
 GetClientRect(hUILoopWnd, &sClientRect);
 InvalidateRect(m_hWnd, &sClientRect, TRUE);  // we set the bErase parameter as TRUE


 //blending structure 
 m_sBlendfunc.BlendOp= AC_SRC_OVER;
 m_sBlendfunc.BlendFlags = 0;
 m_sBlendfunc.SourceConstantAlpha = 255;
 m_sBlendfunc.AlphaFormat = AC_SRC_ALPHA;


 HDC hdc;
 PAINTSTRUCT ps;

 hdc = BeginPaint(hUILoopWnd, &ps);

 GetClientRect(hUILoopWnd, &sClientRect);
 BP_PAINTPARAMS paintParams =  { sizeof(BP_PAINTPARAMS) };
 paintParams.dwFlags = BPPF_ERASE; // erase window content while copying backbuffer
 paintParams.pBlendFunction = &m_sBlendfunc; // how to copy source image to backbuffer
 HDC hBBDC;
 HPAINTBUFFER hPBuffer;


 paintParams.cbSize = sizeof(paintParams);

 hPBuffer = BeginBufferedPaint(hdc, &sClientRect, BPBF_COMPATIBLEBITMAP, &paintParams, &hBBDC);

 //draw animation
 Draw(hBBDC);

 m_nFrameindex ++;


 EndBufferedPaint(hPBuffer, TRUE);
 EndPaint(hUILoopWnd, &ps);
}


/*
Draw
Paint the animation frame on the backbuffer
*/

void DrawTest::Draw(HDC hBBDC)
{ 
 HDC hdcScreen = GetDC(NULL);
 HDC hdcMem = CreateCompatibleDC(hdcScreen);

 bool test = false;  

 HGDIOBJ hbmpOld = SelectObject(hdcMem, m_pAnimation->m_pFramesArray[m_nFrameindex]->hBmp);
 HBITMAP ptemp = CreateCompatibleBitmap(hdcMem,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth,
  m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight);

 DeleteObject(ptemp);

 test = AlphaBlend(hBBDC,m_pAnimation->m_pFramesArray[m_nFrameindex]->nPtX,m_pAnimation->m_pFramesArray[m_nFrameindex]->nPtY 
  ,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth, m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight,
   hdcMem,0,0,m_pAnimation->m_pFramesArray[m_nFrameindex]->nWidth,m_pAnimation->m_pFramesArray[m_nFrameindex]->nHeight,m_sBlendfunc);

 DWORD  test10 = GetLastError();

 SelectObject(hdcMem, hbmpOld); //placing the old object back
 test = DeleteDC(hdcMem);  // after CreateCompatibleDC
 test = ReleaseDC(NULL, hdcScreen); // after GetDC
}

This is the result after 5 frames:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-10-16 08:51:16
// Called by an external timer. This is the application "Next Step" proc.
void DrawTest::TimeStep(){
 PostMessage(m_hWnd, WM_PAINT, 0, 0);
}

不,这并不能让它经历完整的油漆周期。其中需要包含 WM_ERASEBKGND 来解决您的问题。请改用 InvalidateRect()。您现在还可以像您应该的那样在 WM_PAINT 处理程序中调用 BeginPaint()。

// Called by an external timer. This is the application "Next Step" proc.
void DrawTest::TimeStep(){
 PostMessage(m_hWnd, WM_PAINT, 0, 0);
}

Nope, that doesn't make it go through its full paint cycle. Which needs to include WM_ERASEBKGND to solve your problem. Use InvalidateRect() instead. You can now also call BeginPaint() in the WM_PAINT handler, like you should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文