WinCE使用DirectDraw实现页面下掉的特效

#include <ddraw.h>
#pragma comment(lib, "ddraw.lib")

//画一个要掉下来的动态页面之前把loop设为TRUE

BOOL loop = TRUE;

static LPDIRECTDRAW lpDD = NULL;
static LPDIRECTDRAWCLIPPER lpClipper = NULL;
static LPDIRECTDRAWSURFACE lpBmpSurface = NULL;
static LPDIRECTDRAWSURFACE lpPrimarySurface = NULL;

//初始化Direct Draw

void CBluetoothDlg::InitializeDirectDraw()
{
   HRESULT            hRes;
   DDSURFACEDESC    ddDesc;

   if ( lpDD != NULL )
       return;

   hRes = DirectDrawCreate( NULL, &lpDD, NULL );
   if ( hRes != DD_OK )
       return;

   hRes = lpDD->SetCooperativeLevel(NULL, DDSCL_NORMAL);
   if ( hRes != DD_OK )
       return;

   hRes = lpDD->CreateClipper(0, &lpClipper, NULL);
   if ( hRes != DD_OK )
       return;

   ZeroMemory(&ddDesc, sizeof(ddDesc));
   ddDesc.dwSize = sizeof(ddDesc);
   ddDesc.dwFlags = DDSD_CAPS;
   ddDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
   hRes = lpDD->CreateSurface(&ddDesc, &lpPrimarySurface, NULL);
   if ( hRes != DD_OK )
       return;

   lpPrimarySurface->SetClipper(lpClipper);

   lpClipper->SetHWnd(0, m_hWnd);
}

//加载页面(页面图像改变时调用)

BOOL CBluetoothDlg::LoadPicture()
{
 BOOL bRet = FALSE;
 HDC hdc = NULL;
 DDSURFACEDESC ddsDesc;
 
 if ( lpDD == NULL )
  return bRet;
 
 if ( lpBmpSurface != NULL )
 {
  lpBmpSurface->Release();
  lpBmpSurface = NULL;
 }

 ZeroMemory(&ddsDesc, sizeof(ddsDesc));
 ddsDesc.dwSize = sizeof(DDSURFACEDESC);
 ddsDesc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
 ddsDesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
 ddsDesc.dwWidth = 480;
 ddsDesc.dwHeight = 272;

 if ( lpDD->CreateSurface(&ddsDesc, &lpBmpSurface, NULL) != DD_OK )
 {
  return bRet;
 }

 lpBmpSurface->GetDC(&hdc);
 if(Page[CPage::stackPage.GetTop()].Draw(hdc))
  bRet = TRUE;
 
 lpBmpSurface->ReleaseDC(hdc);
 return bRet;
}

//显示页面

void CBluetoothDlg::ShowPicture()
{
   if ( lpPrimarySurface == NULL )
       return;
   if ( lpBmpSurface == NULL )
       return;

   // if there is a screensaver, when resume from it, 
   // the surface will lost, then the program should restore the surface
   if ( lpBmpSurface->IsLost() == DDERR_SURFACELOST )
   {
       if ( lpBmpSurface->Restore() != DD_OK )
           return;
   }

   if ( lpPrimarySurface->IsLost() == DDERR_SURFACELOST )
   {
       if ( lpPrimarySurface->Restore() != DD_OK )
           return;
   }
   if(loop)
   {
    RECT rect = {0,0,480,0};
    RECT rect2 = {0,272,480,272};
    for(int i=0;i<68;i++)
    {
     rect.bottom+=4;
     rect2.top-=4;
   lpPrimarySurface->Blt(&rect, lpBmpSurface, &rect2, DDBLT_WAIT, NULL);
    }
    loop = FALSE;
   }
   else
   {
    lpPrimarySurface->Blt(NULL, lpBmpSurface, NULL, DDBLT_WAIT, NULL);
   }
}

//释放

void CBluetoothDlg::InvalidateDirectDraw()
{
   if ( lpPrimarySurface != NULL )
   {
       lpPrimarySurface->Release();
       lpPrimarySurface = NULL;
   }

   if ( lpClipper != NULL)
   {
       lpClipper->Release();
       lpClipper = NULL;
   }

   if ( lpBmpSurface != NULL )
   {
       lpBmpSurface->Release();
       lpBmpSurface = NULL;
   }

   if ( lpDD != NULL )
   {
       lpDD->Release();
       lpDD = NULL;
   }
}

猜你喜欢

转载自blog.csdn.net/llxxhm/article/details/81905244
今日推荐