在MFC程序中显示JPG/GIF图像


  在“E:/兴辉俊武/vc++学习笔记/动画图片操作/JPG图像加载及超连接”定义了所需的类
  CPictureCtrl 使你可以在任何对话框或窗口中把图像作为子窗口显示。
  例如: 关于对话框中加载IDC_MYIMAGE图像
  class CAboutDialog : public CDialog {
  protected:
   CPictureCtrl m_wndPict;
   virtual BOOL OnInitDialog();
  };
  BOOL CAboutDialog::OnInitDialog()
  {
   m_wndPict.SubclassDlgItem(IDC_MYIMAGE,this);// IDC_MYIMAGE和变量m_wndPict建立联系
   return CDialog::OnInitDialog();
  } 
  在文档/视图中加载JPG/GIF图像
  在doc.h中
  #pragma once
  #include "Picture.h"
  protected:
   CPicture m_pict;
  public:
   CPicture* GetPicture() {
   return &m_pict;
  doc.cpp
  #include <afxdisp.h>
  #include <afxpriv2.h>
  void CPictureDoc::Serialize(CArchive& ar)//打开图片在文档中显示
  {
   if (ar.IsLoading()) {
   VERIFY(m_pict.Load(ar));
   } else { }
  }
  BOOL CPictureDoc::OnNewDocument()/*为了使程序更实用,CPictureDoc::OnNewDocument从程序资源数据加载了一幅图像。为了显示这幅图像,CPictureView::OnDraw要调用CPicture::Render。这样程序一启动便会显示一幅默认的图像。*/
  {
   m_pict.Load(IDR_MAINFRAME);
   return TRUE;
  }
  void CPictureView::OnDraw(CDC* pDC)
  {
   CPictureDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   CPicture* ppic = pDoc->GetPicture();
   ASSERT(ppic);
   if (*ppic) {
   CRect rc;
   GetImageRect(rc);// GetImageRect是CPictureView类的一个成员函数
   ppic->Render(pDC,rc);
   }
  }
 
原文地址:https://www.cnblogs.com/rainbowzc/p/2422165.html