VC中显示图片1

在VC中显示BMP图片自己封装了一个类实现:

使用时首先在预显示图片的form相关的类中申明一个CBMPView 类型的类变量,然后使用

 m_MBMPView.LoadBMPFile("d://zp.bmp");
 CClientDC dc(this);
 m_MBMPView.ShowBMP(&dc, 100, 200);
调用,如果要保证重绘的话,需在onpain()中调用m_MBMPView.ShowBMP(&dc)

class CBMPView 
{
public:
 bool ShowBMP(CClientDC * dc);
 bool ShowBMP(CClientDC * dc, int left, int top);
 bool LoadBMPFile(CString FileName);
 CBMPView();
 virtual ~CBMPView();

private:
 int m_top;
 int m_left;
 int m_height;
 int m_width;
 HBITMAP m_bit;
};

CBMPView::CBMPView()
{
 m_bit = NULL;
}

CBMPView::~CBMPView()
{
 if ( m_bit != NULL ) DeleteObject(m_bit);
}

bool CBMPView::LoadBMPFile(CString FileName)
{
 if (m_bit != NULL) DeleteObject(m_bit);
 m_bit=(HBITMAP)LoadImage(AfxGetInstanceHandle(),
     FileName,
     IMAGE_BITMAP,
     0,
     0,
     LR_LOADFROMFILE|LR_CREATEDIBSECTION
    );
 if(m_bit==NULL) return FALSE;
 DIBSECTION ds;
 BITMAPINFOHEADER &bm = ds.dsBmih;
 GetObject(m_bit,sizeof(ds),&ds);
 m_width = bm.biWidth;
 m_height = bm.biHeight;
 return TRUE;
}

bool CBMPView::ShowBMP(CClientDC *dc, int left, int top)
{
 if (m_bit == NULL) return false;
 HDC MemDC =CreateCompatibleDC(0);
 SelectObject(MemDC,m_bit);

 m_left = left;
 m_top = top;
 BitBlt(dc->m_hDC,m_left,m_top,m_width,m_height,MemDC,0,0,SRCCOPY);
 DeleteDC(MemDC);
 return true;
}

bool CBMPView::ShowBMP(CClientDC *dc)
{
 if (m_bit == NULL) return false;
 HDC MemDC =CreateCompatibleDC(0);
 SelectObject(MemDC,m_bit);

 BitBlt(dc->m_hDC,m_left,m_top,m_width,m_height,MemDC,0,0,SRCCOPY);
 DeleteDC(MemDC);
 return true;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页