请教一个加载BMP文件的问题
写如以下代码可以运行,但在打开时出错,问题出在哪?// p61View.cpp : implementation of the CP61View class
//
#include "stdafx.h"
#include "p61.h"
#include "p61Doc.h"
#include "p61View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CP61View
IMPLEMENT_DYNCREATE(CP61View, CScrollView)
BEGIN_MESSAGE_MAP(CP61View, CScrollView)
//{{AFX_MSG_MAP(CP61View)
ON_COMMAND(ID_DRAW_AOTOFIT, OnDrawAotofit)
ON_COMMAND(ID_DRAW_SOURCESIZE, OnDrawSourcesize)
ON_COMMAND(ID_DRAW_ZOOMIN, OnDrawZoomin)
ON_COMMAND(ID_DRAW_ZOOMOUT, OnDrawZoomout)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CP61View construction/destruction
CP61View::CP61View()
{
// TODO: add construction code here
m_bFileOpen=false;
}
CP61View::~CP61View()
{
}
BOOL CP61View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CP61View drawing
void CP61View::OnDraw(CDC* pDC)
{
CP61Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(!m_bFileOpen)
{
return;
}
CRect r;
GetClientRect(&r);
long scrW,scrH,desW,desH;
BITMAP BMap;
bitmap.GetBitmap(&BMap);
scrW=BMap.bmWidth;
scrH=BMap.bmHeight;
switch(m_bZoom)
{
case ZOOMFIT:
desW=r.Width();
desH=r.Height();
break;
case ZOOMSCR:
desW=scrW;
desW=scrH;
break;
case ZOOMOUT:
desW=scrW*2;
desH=scrH*2;
break;
case ZOOMIN:
desW=(long)(scrW/2);
desH=(long)(scrW/2);
break;
}
CSize sizeTotal;
sizeTotal.cx=desW;
sizeTotal.cy=desH;
SetScrollSizes(MM_TEXT,sizeTotal);
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&bitmap);
pDC->StretchBlt(0,0,desW,desH,&dcMem,0,0,scrW,scrH,SRCCOPY);
}
void CP61View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CP61View printing
BOOL CP61View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CP61View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CP61View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CP61View diagnostics
#ifdef _DEBUG
void CP61View::AssertValid() const
{
CScrollView::AssertValid();
}
void CP61View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CP61Doc* CP61View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CP61Doc)));
return (CP61Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CP61View message handlers
void CP61View::OnDrawAotofit()
{
// TODO: Add your command handler code here
m_bZoom=ZOOMFIT;
Invalidate();
}
void CP61View::OnDrawSourcesize()
{
// TODO: Add your command handler code here
m_bZoom=ZOOMSCR;
Invalidate();
}
void CP61View::OnDrawZoomin()
{
// TODO: Add your command handler code here
m_bZoom=ZOOMIN;
Invalidate();
}
void CP61View::OnDrawZoomout()
{
// TODO: Add your command handler code here
m_bZoom=ZOOMOUT;
Invalidate();
}
BOOL CP61View::GetBmp(LPCTSTR lpszSourceName, CBitmap &bitmap)
{
HBITMAP hbmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),lpszSourceName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hbmp==NULL)
return false;
bitmap.Detach();
bitmap.Attach(hbmp);
return true;
}
BOOL CP61View::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CScrollView::OnEraseBkgnd(pDC);
}
void CP61View::OnFileOpen()
{
// TODO: Add your command handler code here
CString Filepathname;
CFileDialog dlg(TRUE,_T("BMP"),_T("BMP"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件()*.BMP|*.BMP|"));
if(dlg.DoModal())
{
Filepathname=dlg.GetPathName();
if(Filepathname!="*.BMP")
{
if(!GetBmp(Filepathname,bitmap))
MessageBox("打开文件失败","提示",MB_ICONHAND|MB_OK);
else
{
m_bFileOpen=true;
m_bZoom=ZOOMSCR;
Invalidate();
}
}
}
}