用DirectDraw封装的位图动画类 分类: VC++ 2013-07-22 16:31 365人阅读 评论(0) 收藏
头文件
- #pragma on
ce - #include <vector>
- using namespace std;
- #include <DShow.h>
- #include <uuids.h>
- #define DXIMAGE_WIDTH 1024
- #define DXIMAGE_HEIGHT 768
- class CDXImage
- {
- public:
- CDXImage(void);
- ~CDXImage(void);
- //
- public:
- // 文件中载入位图
- BOOL LoadImageFromFile(char *pFileName, int cx, int cy);
- // 载入图像
- void LoadDXImage(char* pBits, int cx, int cy);
- // 显示图像
- void Display(CDC *pDC);
- // 初始化DirectDraw
- void InitDraw(HWND hWnd);
- // 恢复丢失的图面
- void RestoreAll(HWND hWnd);
- protected:
- // 释放对象
- void ReleaseObject();
- // DX objects
- private:
- IDirectDraw7 *m_lpDD; // DirectDraw Object
- IDirectDrawSurface7 *m_lpDDSPrimary; // Primary Surface Object
- IDirectDrawSurface7 *m_lppDDSBackBuffer; // BackBuffer Surface Object
- // Image Object
- private:
- vector<HBITMAP> m_vpBMPs; // BMP Objects Array
- HWND m_hWnd;
- };
源文件
- #include "StdAfx.h"
- #include "DXImage.h"
- CDXImage::CDXImage(void)
- {
- // 成员变量初始化
- this->m_lpDD = NULL;
- this->m_lpDDSPrimary = NULL;
- this->m_lppDDSBackBuffer = NULL;
- this->m_hWnd = NULL;
- }
- CDXImage::~CDXImage(void)
- {
- ReleaseObject();
- }
- /************************************************************************
- * 函数名称:LoadDXImage
- * 函数功能:从DSP端接收图像数据
- * 函数参数:
- * char *pBits : 图像数据
- * int cx : 图像宽度
- * int cy : 图像高度
- * 返回值:void
- ************************************************************************/
- void CDXImage::LoadDXImage(char *pBits, int cx,int cy)
- {
- }
- /************************************************************************
- * 函数名称:LoadImageFromFile
- * 函数功能:从硬盘文件中读取图像
- * 函数参数:
- * char *pFileName : 图像文件名
- * int cx : 图像宽度
- * int cy : 图像高度
- * 返回值类型:BOOL
- true : 文件载入成功
- false: 文件载入失败
- ************************************************************************/
- BOOL CDXImage::LoadImageFromFile(char *pFileName, int cx, int cy)
- {
- HBITMAP hbmp;
- hbmp = (HBITMAP)LoadImage(AfxGetInstanceHandle(), pFileName, IMAGE_BITMAP, cx, cy, LR_LOADFROMFILE);
- if (hbmp == NULL)
- {
- AfxMessageBox("Failed to Load Image /n Please Check the File Path!");
- return FALSE;
- }
- m_vpBMPs.push_back(hbmp);
- return TRUE;
- }
- /************************************************************************
- * 函数名称:InitDraw
- * 函数功能:初始化DirectDraw
- * 函数参数:
- * HWND hWnd : 图像显示窗口句柄
- * 返回值类型:void
- ************************************************************************/
- void CDXImage::InitDraw(HWND hWnd)
- {
- ASSERT(m_lpDD == NULL);
- ASSERT(m_lpDDSPrimary == NULL);
- ASSERT(m_lppDDSBackBuffer == NULL);
- ASSERT(m_vpBMPs.size() == 0);
- ASSERT(m_hWnd == NULL);
- m_hWnd = hWnd;
- // 创建DirectDraw对象
- HRESULT hr;
- hr = DirectDrawCreateEx(NULL, (VOID**)&m_lpDD, IID_IDirectDraw7, NULL);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Create DirectDraw Object!");
- return;
- }
- hr = m_lpDD->SetCooperativeLevel(m_hWnd, DDSCL_NORMAL);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Initialize DirectDraw");
- return;
- }
- // 创建主图面
- DDSURFACEDESC2 ddsd;
- memset(&ddsd, 0, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_CAPS;
- ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
- hr = m_lpDD->CreateSurface(&ddsd, &m_lpDDSPrimary, NULL);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Create primary surface!");
- return;
- }
- // 创建裁剪
- IDirectDrawClipper *lpClipper = NULL;
- hr = m_lpDD->CreateClipper(NULL, &lpClipper, NULL);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Create Clipper!");
- return;
- }
- hr = lpClipper->SetHWnd(0, hWnd);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Create Window Clipper!");
- return;
- }
- hr = m_lpDDSPrimary->SetClipper(lpClipper);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Set Clipper of Primary Surface!");
- return;
- }
- lpClipper->Release();
- // 创建辅助画面
- memset(&ddsd, 0, sizeof(ddsd));
- ddsd.dwSize = sizeof(ddsd);
- ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
- ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
- ddsd.dwWidth = DXIMAGE_WIDTH;
- ddsd.dwHeight = DXIMAGE_HEIGHT;
- hr = m_lpDD->CreateSurface(&ddsd, &m_lppDDSBackBuffer, NULL);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Create Assistant Surface!");
- return;
- }
- }
- /************************************************************************
- * 函数名称:Display
- * 函数功能:显示图像
- * 函数参数:
- * CDC pDC : 图像显示设备上下文
- * 返回值类型:void
- ************************************************************************/
- void CDXImage::Display(CDC *pDC = NULL)
- {
- // 绘制辅助图面
- HRESULT hr;
- DDSURFACEDESC2 desc;
- memset(&desc, 0, sizeof(desc));
- desc.dwSize = sizeof(desc);
- hr = m_lppDDSBackBuffer->GetSurfaceDesc(&desc);
- if (FAILED(hr))
- {
- AfxMessageBox("Failed to Get Assistant Surface Descriptor");
- return;
- }
- for (vector<HBITMAP>::iterator it = m_vpBMPs.begin(); it != m_vpBMPs.end(); it++)
- {
- // 获取设备DC
- HDC hDC;
- if (m_lppDDSBackBuffer->GetDC(&hDC) != DD_OK)
- {
- return;
- }
- // 保存DC
- CDC dc; //dbg
- dc.m_hDC = hDC;
- int nSavedDC = dc.SaveDC();
- CDC memDC;
- memDC.CreateCompatibleDC(&dc);
- HBITMAP hbmp = *it;
- memDC.SelectObject(hbmp);
- dc.BitBlt(0, 0, DXIMAGE_WIDTH, DXIMAGE_HEIGHT, &memDC, 0, 0, SRCCOPY);
- memDC.DeleteDC();
- DeleteObject(hbmp);
- //m_vpBMPs.erase(it);
- CRect rcClient;
- GetClientRect(m_hWnd, &rcClient);
- ClientToScreen(m_hWnd, &rcClient.TopLeft());
- ClientToScreen(m_hWnd, &rcClient.BottomRight());
- dc.RestoreDC(nSavedDC);
- dc.m_hDC = NULL;
- m_lppDDSBackBuffer->ReleaseDC(hDC);
- m_lpDDSPrimary->Blt(&rcClient, m_lppDDSBackBuffer, NULL, DDBLT_WAIT, NULL);
- }
- }
- /************************************************************************
- * 函数名称:ReleaseObject
- * 函数功能:释放DirectDraw对象
- * 函数参数:无
- * 返回值类型:void
- ************************************************************************/
- void CDXImage::ReleaseObject()
- {
- // 删除对象
- if (m_lpDDSPrimary != NULL)
- {
- m_lpDDSPrimary->Release();
- m_lpDDSPrimary = NULL;
- }
- if (m_lppDDSBackBuffer != NULL)
- {
- m_lppDDSBackBuffer->Release();
- m_lppDDSBackBuffer = NULL;
- }
- if (m_lpDD != NULL)
- {
- m_lpDD->Release();
- m_lpDD = NULL;
- }
- }
- /************************************************************************
- * 函数名称:RestoreAll
- * 函数功能:所有DirectDraw对象复位
- * 函数参数:
- HWND hWnd : 图像显示设备句柄
- * 返回值类型:void
- ************************************************************************/
- void CDXImage::RestoreAll(HWND hWnd)
- {
- HRESULT hr;
- hr = m_lpDDSPrimary->Restore();
- if (hr == DDERR_WRONGMODE)
- {
- TRACE("Dispaly model changed");
- ReleaseObject();
- m_hWnd = hWnd;
- InitDraw(m_hWnd);
- return;
- }
- else if (FAILED(hr))
- {
- return;
- }
- if (FAILED(m_lppDDSBackBuffer->Restore()))
- {
- return;
- }
- }
用法:
适用于Win32平台
一、创建一个CDXImage类对象
- CDXImage m_DXImage;
二、初始化并载入位图
- m_DXImage.InitDraw(GetSafeHwnd()); // 初始化DX图像对象
- m_DXImage.LoadImageFromFile("pics//0.bmp", 1024, 768);
- m_DXImage.LoadImageFromFile("pics//2.bmp", 1024, 768);
三、显示位图
- m_DXImage.Display(NULL);
然后可借助定时器使用。
版权声明:本文为博主原创文章,未经博主允许不得转载。