MFC中怎么显示图像,我要疯了!

VC++6.0,,,MFCAppWizard,,,你懂的,,正题:我想在一个单文档程序中显示一张电脑里的图片,,有谁能告诉我一步一步的具体步骤?最好从建立程序开始,每一步... VC++6.0,,,MFC AppWizard,,,你懂的,,
正题:我想在一个单文档程序中显示一张电脑里的图片,,有谁能告诉我一步一步的具体步骤?最好从建立程序开始,每一步怎么操作,,最好把代码写出来,,最好不要带太多的专业术语,接触这个MFC时间不长啊,,不胜感激,,随便任意一种方法也可以啊,,我真的要疯了, ,,我真的要疯了,,,
我很无语了,,我自己已经解决了,还是不要看书,,真的很难找到专门给一点都不会的人准备的书,,看了打击你的自信,,为了方便其他刚学mfc的人,,说一下我的方法,,所谓的双缓冲,,,,不过只能显示bmp图像
首先按4楼的,,,然后可以给那个图片起一个ID名字,,,(左边的工作区,右键,,属性,ID),,找到OnDraw()函数(如果不是OnDraw(),里面也没有pDC这个参数的话,,可以写下CDC *pDC=GetDC();)
然后在里面写下面的话:
CRect rcClient;
GetClientRect(&rcClient);

CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap memBmp;
memBmp.CreateCompatibleBitmap(pDC,rcClient.Width(),rcClient.Height());
memDC.SelectObject(&memBmp);
memDC.FillSolidRect(0,0,rcClient.Width(),rcClient.Height(),RGB(255,255,255));

CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP);//IDB_BITMAP是你起的图片的ID
dc.SelectObject(&bmp);

memDC.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&dc,0,0,SRCAND);

pDC->BitBlt(0,0,rcClient.Width(),rcClient.Height(),&memDC,0,0,SRCCOPY);
展开
 我来答
百度网友8e174c7
2010-07-28 · 超过28用户采纳过TA的回答
知道答主
回答量:56
采纳率:0%
帮助的人:61.6万
展开全部
1、 启动Visual C++6.0,生成一个单文档视图结构的应用程序,视图类的基类为CscrollView,同时将该程序命名为"Myimgapp";

2、 在应用程序的项目代码中添加"CPicture"类;工具栏上添加图像显示比例的按钮,具体参加代码部分;

3、 使用资源编辑器向程序中添加Jepg格式的图像资源;

4、 添加代码,编译运行程序。

三、程序代码

/////////////////// Picture object--encapsulates IPicture

#pragma once

#include

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(this)->m_hr = m_spIPicture->get_Width(&cx);

ASSERT(SUCCEEDED(m_hr));

const_cast(this)->m_hr = m_spIPicture->get_Height(&cy);

ASSERT(SUCCEEDED(m_hr));

}

void Free()

{

if (m_spIPicture) {

m_spIPicture.Release();

}

}

protected:

CComQIPtrm_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);

}
yueyuwangzi
推荐于2016-09-27
知道答主
回答量:10
采纳率:0%
帮助的人:0
展开全部
资源文件->Dialog打开对话框->标题栏Insert->Resource->Bitmap->Import->文件类型所有文件->选择电脑上的Bitmap图片,单击确定
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
515088896
2010-07-29
知道答主
回答量:24
采纳率:0%
帮助的人:8.9万
展开全部
一楼可以的,最好自己先那本书照着做做吧。VC++深入详解不错,其他的自己也可以去找找。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式