|
本帖最后由 Rainyboy 于 2010-11-18 20:57 编辑
给一个很久以前学习和使用过的类吧,是继承MFC框架中CBitMap类实现的一个CBitMapEx(MFC框架中的类都是这样,以C开头)类:头文件:BitMapEx.h
- // BitmapEx.h: interface for the CBitmapEx class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_BITMAPEX_H__5E661765_FB4A_4623_BC9F_261366D967C3__INCLUDED_)
- #define AFX_BITMAPEX_H__5E661765_FB4A_4623_BC9F_261366D967C3__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- class CBitmapEx : public CBitmap
- {
- public:
- CBitmapEx();
- virtual ~CBitmapEx();
- BOOL LoadImage(LPCTSTR szFilename);
- BOOL LoadBitmap(LPCTSTR szFilename);
- void Draw(CDC* pDC,double init_x,double init_y);
- };
- #endif // !defined(AFX_BITMAPEX_H__5E661765_FB4A_4623_BC9F_261366D967C3__INCLUDED_)
复制代码 .cpp文件BitMapEx.cpp
- // BitmapEx.cpp: implementation of the CBitmapEx class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "ImageEx.h"
- #include "BitmapEx.h"
- #include <olectl.h>
- #include <ole2.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CBitmapEx::CBitmapEx()
- {
- }
- CBitmapEx::~CBitmapEx()
- {
- DeleteObject();
- }
- BOOL CBitmapEx::LoadBitmap(LPCTSTR szFilename)
- {
- ASSERT(szFilename);
- DeleteObject();
- HBITMAP hBitmap = NULL;
- hBitmap = (HBITMAP)::LoadImage(NULL, szFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
- return Attach(hBitmap);
- }
- BOOL CBitmapEx::LoadImage(LPCTSTR szFilename)
- {
- // Use IPicture stuff to use JPG / GIF files
- IPicture* p;
- IStream* s;
- HGLOBAL hG;
- void* pp;
- FILE* fp;
- // Read file in memory
- fp = fopen(szFilename,"rb");
- if (!fp)
- return FALSE;
- fseek(fp,0,SEEK_END);
- int len = ftell(fp);
- fseek(fp,0,SEEK_SET);
- hG = GlobalAlloc(GPTR,len);
- if (!hG)
- {
- fclose(fp);
- return FALSE;
- }
- pp = (void*)hG;
- fread(pp,1,len,fp);
- fclose(fp);
- // Create an IStream so IPicture can
- if (CreateStreamOnHGlobal(hG,false,&s) != S_OK )
- {
- GlobalFree(hG);
- return FALSE;
- }
- OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
- if (!p)
- {
- s->Release();
- GlobalFree(hG);
- return FALSE;
- }
- s->Release();
- GlobalFree(hG);
- HBITMAP hB = 0;
- p->get_Handle((unsigned int*)&hB); // 返回GDI句柄的指针
- // Copy the image. Necessary, because upon p's release,
- // the handle is destroyed.
- HBITMAP hBitmap = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG);
- p->Release();
- Detach();
- return Attach(hBitmap);
- }
- void CBitmapEx::Draw(CDC* pDC,double init_x,double init_y)
- {
- HBITMAP hBitmap = (HBITMAP)*this;
- if( hBitmap )
- {
- CDC bitmapDC;
- if (bitmapDC.CreateCompatibleDC(pDC))
- {
- BITMAP bm;
- GetBitmap(&bm);
- CBitmap* pOldBmp = bitmapDC.SelectObject(this);
- pDC->BitBlt(init_x,init_y,bm.bmWidth,bm.bmHeight,&bitmapDC,0,0,SRCCOPY);
- bitmapDC.SelectObject(pOldBmp);
- }
- }
- }
复制代码
实际中如何使用?
1,在欲显示图像的窗口声明私有变量:- CBitmapEx m_BitmapEx_11;
- CBitmapEx m_BitmapEx_12;
复制代码 2,想要载入图像的时候(多半对应于某个事件(菜单项的单击、按钮的单击)响应函数):
- void CImageExView::OnReadImage()
- {
- CString strFileName11("11.bmp");
- CString strFileName12("12.bmp");
- m_BitmapEx_11.LoadImage(strFileName11);
- BITMAP bm11;
- m_BitmapEx_11.GetBitmap(&bm11);
- m_BitmapEx_12.LoadImage(strFileName12);
- BITMAP bm12;
- m_BitmapEx_12.GetBitmap(&bm12);
- CSize sz(bm11.bmWidth + bm12.bmWidth, bm11.bmHeight + bm12.bmHeight);
- SetScrollSizes(MM_TEXT, sz);
- Invalidate();
-
- }
复制代码 3,当然,要想Invalidate()强制重绘的时候也重绘咱们的图像,得改写相应窗口的OnDraw函数,看这里:
- void CImageExView::OnDraw(CDC* pDC)
- {
- CImageExDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- m_BitmapEx_11.Draw(pDC,0,0);
- BITMAP bm11;
- m_BitmapEx_11.GetBitmap(&bm11);
- m_BitmapEx_12.Draw(pDC,bm11.bmWidth,0);
- }
复制代码
个人感觉挺好用的一个类,但愿能对你有用。
|
评分
-
1
查看全部评分
-
|