在MFC应用程序中显示JPG/GIF图像文件
2010-06-15 20:40:11 来源:WEB开发网三、程序代码
/////////////////// Picture object--encapsulates IPicture
#pragma once
#include <atlbase.h>
class CPicture {
public:
CPicture();
~CPicture();
// Load frm various sosurces
BOOL Load(UINT nIDRes);
BOOL Load(LPCTSTR pszPathName);
BOOL Load(CFile& file);
BOOL Load(CArchive& ar);
BOOL Load(IStream* pstm);
// render to device context
BOOL Render(CDC* pDC, CRect rc=CRect(0,0,0,0),
LPCRECT prcMFBounds=NULL) const;
CSize GetImageSize(CDC* pDC=NULL) const;
operator IPicture*() {
return m_spIPicture;
}
void GetHIMETRICSize(OLE_XSIZE_HIMETRIC& cx, OLE_YSIZE_HIMETRIC& cy) const
{
cx = cy = 0;
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Width(&cx);
ASSERT(SUCCEEDED(m_hr));
const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Height(&cy);
ASSERT(SUCCEEDED(m_hr));
}
void Free()
{
if (m_spIPicture) {
m_spIPicture.Release();
}
}
protected:
CComQIPtr<IPicture>m_spIPicture; // ATL smart pointer to IPicture
HRESULT m_hr; // last error code
};
/////////////////////////////////////////////////////////////// CPicture implementation
#include "StdAfx.h"
#include "Picture.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CPicture::CPicture()
{}
CPicture::~CPicture()
{}
BOOL CPicture::Load(UINT nIDRes) // Load from resource. Looks for "IMAGE" type.
{
// find resource in resource file
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(nIDRes),"IMAGE"); // type
if (!hRsrc)
return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;
// create memory file and load it
CMemFile file(lpRsrc, len);
BOOL bRet = Load(file);
FreeResource(hRsrc);
GlobalFree(lpRsrc);
return bRet;
}
BOOL CPicture::Load(LPCTSTR pszPathName) // Load from path name.
{
CFile file;
if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite))
return FALSE;
BOOL bRet = Load(file);
file.Close();
return bRet;
}
BOOL CPicture::Load(CFile& file) // Load from CFile
{
CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
return Load(ar);
}
////// Load from archive--create stream and load from stream.
BOOL CPicture::Load(CArchive& ar)
{
CArchiveStream arcstream(&ar);
return Load((IStream*)&arcstream);
}
//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
BOOL CPicture::Load(IStream* pstm)
{
Free();
HRESULT hr = OleLoadPicture(pstm, 0, FALSE,IID_IPicture, (void**)&m_spIPicture);
ASSERT(SUCCEEDED(hr) && m_spIPicture);
return TRUE;
}
//////////////////
// Render to device context. Covert to HIMETRIC for IPicture.
BOOL CPicture::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const
{
ASSERT(pDC);
if (rc.IsRectNull()) {
CSize sz = GetImageSize(pDC);
rc.right = sz.cx;
rc.bottom = sz.cy;
}
long hmWidth,hmHeight; // HIMETRIC units
GetHIMETRICSize(hmWidth, hmHeight);
m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(),0, hmHeight, hmWidth, -hmHeight, prcMFBounds);
return TRUE;
}
//////////////////
// Get image size in pixels. Converts from HIMETRIC to device coords.
CSize CPicture::GetImageSize(CDC* pDC) const
{
if (!m_spIPicture)
return CSize(0,0);
LONG hmWidth, hmHeight; // HIMETRIC units
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
CSize sz(hmWidth,hmHeight);
if (pDC==NULL) {
CWindowDC dc(NULL);
dc.HIMETRICtoDP(&sz); // convert to pixels
} else {
pDC->HIMETRICtoDP(&sz);
}
return sz;
}
///////////////////////////////////// Picture view is a typical scroll view.
#include "Doc.h"
class CPictureView : public CScrollView {
public:
virtual ~CPictureView();
CPictureDoc* GetDocument() { return (CPictureDoc*)m_pDocument; }
protected:
BOOL m_rcImage; // rect to display image in
UINT m_iHowScale; // how to scale image
CPictureView();
void GetImageRect(CRect& rc);
void SetScrollSizes();
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual void OnInitialUpdate(); // called first time after construct
// command/message handlers
afx_msg void OnViewScale(UINT nID);
afx_msg void OnUpdateViewScale(CCmdUI* pCmdUI);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSize(UINT nType, int cx, int cy);
DECLARE_DYNCREATE(CPictureView)
DECLARE_MESSAGE_MAP()
};
////////////////////////////////////////////////////////////// CPictureView
#include "StdAfx.h"
#include "View.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CPictureView, CScrollView)
BEGIN_MESSAGE_MAP(CPictureView, CScrollView)
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_COMMAND_RANGE(ID_VIEW_TOFIT, ID_VIEW100, OnViewScale)
ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_TOFIT, ID_VIEW100, OnUpdateViewScale)
END_MESSAGE_MAP()
CPictureView::CPictureView()
{
m_iHowScale = ID_VIEW_TOFIT;
}
CPictureView::~CPictureView()
{}
void CPictureView::OnInitialUpdate()
{
SetScrollSizes();
}
//////////////////// Set scroll sizes based on picture. Page size = client hieight/width;
// line size = 1/10 of this.
void CPictureView::SetScrollSizes()
{
CRect rcClient;
GetClientRect(&rcClient);
CRect rcImage;
GetImageRect(rcImage);
CSize szTotal = rcImage.Size();
CSize szPage = rcClient.Size();
CSize szLine = szPage;
szLine.cx /= 10;
szLine.cy /= 10;
CScrollView::SetScrollSizes(MM_TEXT, szTotal, szPage, szLine);
Invalidate();
}
//////////////////// View was sized: readjust scroll sizes if I'm in "zoom to fit" mode
void CPictureView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
if (m_iHowScale==ID_VIEW_TOFIT) {
SetScrollSizes();
}
}
//////////////////
// Erase the background. This is required in case the image is smaller than
// the client area, to paint the extra background. Use clipping to avoid flicker.
BOOL CPictureView::OnEraseBkgnd(CDC* pDC)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// get client rectangle
CRect rcClient;
GetClientRect(&rcClient);
CRect rc = rcClient;
// get image rectangle
CRect rcImage;
GetImageRect(rcImage);
rc = rcImage;
CPoint pt = pDC->GetViewportOrg();
CSize sz = GetTotalSize();
// create clipping region
CRgn clipRgn;
clipRgn.CreateRectRgnIndirect(&rcClient);
pDC->SelectClipRgn(&clipRgn);
pDC->ExcludeClipRect(&rcImage);
CBrush brush(RGB(0,0,0)); // black
pDC->FillRect(&rcClient, &brush);
pDC->SelectClipRgn(NULL);
return TRUE;
}
//////////////////
// Draw the picture -- call CPicture to do it.
void CPictureView::OnDraw(CDC* pDC)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPicture* ppic = pDoc->GetPicture();
ASSERT(ppic);
if (*ppic) {
CRect rc;
GetImageRect(rc);
ppic->Render(pDC,rc);
}
}
//////////////////
// Get image rectangle, scaled for current zoom factor.
void CPictureView::GetImageRect(CRect& rc)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPicture* ppic = pDoc->GetPicture();
ASSERT(ppic);
if (!ppic || !*ppic) {
rc.SetRect(0,0,0,0);
} else if (m_iHowScale==ID_VIEW_TOFIT) {
GetClientRect(&rc);
} else {
CSize sz = ppic->GetImageSize();
switch (m_iHowScale) {
case ID_VIEW25:
sz.cx >>= 2;
sz.cy >>= 2;
break;
case ID_VIEW33:
sz.cx /= 3;
sz.cy /= 3;
break;
case ID_VIEW50:
sz.cx >>= 1;
sz.cy >>= 1;
break;
case ID_VIEW75:
sz.cx = (sz.cx * 3)/4;
sz.cy = (sz.cy * 3)/4;
break;
}
rc.SetRect(0,0,sz.cx,sz.cy);
}
}
//////////////////
// Handle zoom command.
void CPictureView::OnViewScale(UINT nID)
{
if (m_iHowScale != nID) {
m_iHowScale = nID;
ScrollToPosition(CPoint(0,0));
OnInitialUpdate();
}
}
//////////// Update zoom menu -- check the whichever zoom factor I'm at now.
void CPictureView::OnUpdateViewScale(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(pCmdUI->m_nID == m_iHowScale);
}
四、小结
本实例通过Cpicutre类现了JEPG图像的显示,读者可以将该类直接用在多媒体应用程序的开发中。另外,MFC提供了一个现成的类--CPictureHolder,这个类的功能几乎与CPicture完全一样,读者朋友可以在afxctl.h文件中找到它的定义。
更多精彩
赞助商链接