在网上查了一下,中文的写的都太简略了,英文的还看不懂,所以只好到此来求教了~~~~~~

解决方案 »

  1.   


    #ifndef Canvas_H
    #define Canvas_H#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#include <AfxWin.h>
    #include <Vfw.h>
    #include <AfxMt.h>class CCanvas : public CStatic
    {
    public:
    CCanvas(); void Clear();
    void SetBitmapSize(int nWidth, int nHeight);
    bool Draw(unsigned char *pBitmap, unsigned long nLength);// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCanvas)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CCanvas(); // Generated message map functions
    protected:
    //{{AFX_MSG(CCanvas)
    afx_msg void OnPaint();
    //}}AFX_MSG DECLARE_MESSAGE_MAP()private:
    void InitCanvas(int nWidth, int nHeight); unsigned char *m_pBitmapBuffer;
    HDRAWDIB m_hDrawDIB;
    BITMAPINFOHEADER m_BitmapInfoHeader; CCriticalSection m_CriticalSection;
    };#endif
      

  2.   


    #include "Canvas.h"
    #include "LocalLock.h"#pragma comment(lib, "Vfw32")#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endiftypedef CLocalLock<CCriticalSection> CLocalCSLock;const unsigned long BackGroundColor = 192;CCanvas::CCanvas()
    : m_hDrawDIB(NULL), 
    m_pBitmapBuffer(NULL)
    {
    memset(&m_BitmapInfoHeader, 0, sizeof(m_BitmapInfoHeader)); m_hDrawDIB = ::DrawDibOpen();
    InitCanvas(0, 0);
    }CCanvas::~CCanvas()
    {
    ::DrawDibClose(m_hDrawDIB);
    m_hDrawDIB = NULL; if (m_pBitmapBuffer != NULL) {
    delete [] m_pBitmapBuffer;
    m_pBitmapBuffer = NULL;
    }
    }BEGIN_MESSAGE_MAP(CCanvas, CStatic)
    //{{AFX_MSG_MAP(CCanvas)
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CCanvas message handlersvoid CCanvas::OnPaint()
    {
    CPaintDC PaintDC(this);
    HDC hDC = PaintDC.GetSafeHdc();
    CRect rect; bool bRet = false;
    if (m_pBitmapBuffer == NULL) {
    goto OnPaintExit;
    } if (hDC == NULL) {
    goto OnPaintExit;
    } GetClientRect(&rect); bRet = (::DrawDibDraw(m_hDrawDIB, hDC, 0, 0, rect.Width(), rect.Height(), &m_BitmapInfoHeader, m_pBitmapBuffer, 0, 0, m_BitmapInfoHeader.biWidth, m_BitmapInfoHeader.biHeight, 0) == TRUE);OnPaintExit:
    if (!bRet) {
    CStatic::OnPaint();
    }
    }bool CCanvas::Draw(unsigned char *pBitmap, unsigned long nLength)
    {
    if (pBitmap == NULL || nLength == 0) {
    return false;
    } if (m_pBitmapBuffer == NULL || nLength != m_BitmapInfoHeader.biSizeImage) {
    return false;
    } CLocalCSLock localLock(&m_CriticalSection);
    memcpy(m_pBitmapBuffer, pBitmap, m_BitmapInfoHeader.biSizeImage); Invalidate();
    UpdateWindow();
    return true;
    }void CCanvas::InitCanvas(int nWidth, int nHeight)
    {
    if (nWidth < 0 || nHeight < 0) {
    return;
    } CLocalCSLock localLock(&m_CriticalSection);
    if (m_pBitmapBuffer != NULL) {
    delete [] m_pBitmapBuffer;
    m_pBitmapBuffer = NULL;
    } m_BitmapInfoHeader.biSize = sizeof(m_BitmapInfoHeader);
    m_BitmapInfoHeader.biWidth = nWidth;
    m_BitmapInfoHeader.biHeight = nHeight;
    m_BitmapInfoHeader.biPlanes = 1;
    m_BitmapInfoHeader.biBitCount = 24;
    m_BitmapInfoHeader.biCompression = 0;
    m_BitmapInfoHeader.biSizeImage = m_BitmapInfoHeader.biWidth * m_BitmapInfoHeader.biHeight * m_BitmapInfoHeader.biBitCount / 8;
    m_BitmapInfoHeader.biXPelsPerMeter = 0;
    m_BitmapInfoHeader.biYPelsPerMeter = 0;
    m_BitmapInfoHeader.biClrUsed = 0;
    m_BitmapInfoHeader.biClrImportant = 0; if (m_BitmapInfoHeader.biSizeImage > 0) {
    m_pBitmapBuffer = new unsigned char[m_BitmapInfoHeader.biSizeImage];
    memset(m_pBitmapBuffer, BackGroundColor, m_BitmapInfoHeader.biSizeImage);
    }
    }void CCanvas::SetBitmapSize(int nWidth, int nHeight)
    {
    if (nWidth < 0 || nHeight < 0) {
    return;
    } InitCanvas(nWidth, nHeight);
    }void CCanvas::Clear()
    {
    if (m_pBitmapBuffer == NULL) {
    return;
    } memset(m_pBitmapBuffer, BackGroundColor, m_BitmapInfoHeader.biSizeImage);
    Draw(m_pBitmapBuffer, m_BitmapInfoHeader.biSizeImage);
    }
      

  3.   


    #ifndef LocalLock_H
    #define LocalLock_Htemplate <class Type>
    class CLocalLock
    {
    public:
        CLocalLock(Type *pObject) : m_pObject(pObject) { m_pObject->Lock(); }
        ~CLocalLock() { m_pObject->Unlock(); }private:
        Type *m_pObject;
    };#endif