我已经用IPicture接口能在对话框的控件上加在资源文件,但不知怎样利用CPICTURE类函数读入并利用CFileDialog读入图形文件 .cpictue.cpp
#include "StdAfx.h"
#include "Picture.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif////////////////////////////////////////////////////////////////#include "StdAfx.h"
#include "PictCtrl.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif////////////////////////////////////////////////////////////////
// CPictureCtrl is like a static bitmap, but displays any kind of
// image -- BMP, JPG, or GIF.
//IMPLEMENT_DYNAMIC(CPictureCtrl, CStatic)
BEGIN_MESSAGE_MAP(CPictureCtrl, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()CPictureCtrl::CPictureCtrl(){

}CPictureCtrl::~CPictureCtrl()
{
}//////////////////
// Created: load picture with same ID as me, if there is one. In theory, this
// should not be required because PreSubclassWindow is called whether the
// control is created directly or subclassed from a dialog--but for some odd
// reason unbeknowst to me, GetDlgCtrlID always returns 0 in OnCreate. Go
// figure.
//
//////////////////
// Subclassed: load picture with same ID as me, if there is one.
//
void CPictureCtrl::PreSubclassWindow()
{
int nID = GetDlgCtrlID();
if (nID > 0 && !m_pict) {
LoadImage(nID);
}
CString s;}//////////////////
// Paint the picture -- override static stuff and do my own thing.
// Call CPicture to to the work. 
//
void CPictureCtrl::OnPaint()
{
CPaintDC dc(this);
if (m_pict) {
CRect rcClient;
GetClientRect(&rcClient);
CRect rcImage(CPoint(0,0),m_pict.GetImageSize());
CRect rc;
rc.IntersectRect(&rcImage, &rcClient);
m_pict.Render(&dc, rc);
}
}//////////////////
// If picture is smaller than client area, paint extra with background color.
// CPicture implementation
//CPicture::CPicture()
{
}CPicture::~CPicture()
{
}//////////////////
// Load from resource. Looks for "IMAGE" type.
//
BOOL CPicture::Load(UINT nIDRes)
{
// 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); return bRet;
}//////////////////
// Load from path name.
//
BOOL CPicture::Load(LPCTSTR pszPathName)
{
CFile file;
if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite))
return FALSE;
BOOL bRet = Load(file);
file.Close();
return bRet;
}//////////////////
// Load from CFile
//
BOOL CPicture::Load(CFile& file)
{
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;
}
cstatic类的派生类cpicturectrl.h#include "picture.h"
#pragma once//////////////////
// Class to encapsulate IPicture. This does not wrap all IPicture methods,
// only the ones I needed to implement ImgView -- feel free to add the others
// yourself.
//
class CPictureCtrl : public CStatic {
public:
CPictureCtrl();
~CPictureCtrl();
    BOOL SubclassDlgItem(UINT nID, CWnd* pParent, LPCTSTR lpszLink=NULL) {
return CStatic::SubclassDlgItem(nID, pParent);
} // brainless wrappers call CPicture
BOOL LoadImage(UINT nIDRes) {
Invalidate();
return m_pict.Load(nIDRes);
}
BOOL LoadImage(LPCTSTR pszPathName) {
Invalidate();
return m_pict.Load(pszPathName);
}
BOOL LoadImage(CArchive& ar) {
Invalidate();
return m_pict.Load(ar);
}
BOOL LoadImage(IStream* pstm) {
Invalidate();
return m_pict.Load(pstm);
} CSize GetImageSize() {
return m_pict.GetImageSize();
} const CPicture* GetPicture() {
return &m_pict;
}protected:
CPicture m_pict; // picture
virtual void PreSubclassWindow(); // message handlers
afx_msg void OnPaint();
DECLARE_DYNAMIC(CPictureCtrl)
DECLARE_MESSAGE_MAP()
};