demo制作

读取BMP文件的demo制作:


1、 所需文件:dibapi.h

                           dibapi.cpp

                           myfile.cpp

2、工程类型选择多文档,MFC standard,其他默认

3、取消打开软件的默认空白标签页

         项目.cpp——》在ProcessShellCommand调用之前加 cmdInfo.m_nShellCommand =CCommandLineInfo::FileNothing 。

4、在文档类头文件中添加#include "dibapi.h"

   // Attributes
public:

   HDIB GetHDIB() const
{ return m_hDIB; }
CPalette* GetDocPalette() const
{ return m_palDIB; }
CSize GetDocSize() const
{ return m_sizeDoc; }

// Operations
public:
void ReplaceHDIB(HDIB hDIB);
void InitDIBData();

重载

virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

protected:
HDIB m_hDIB;
CPalette* m_palDIB;
CSize m_sizeDoc;

5、在OnDraw函数中绘图,添加

CDEMO1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;


HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB);        // Size of DIB - y
::GlobalUnlock((HGLOBAL) hDIB);
CRect rcDIB;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
CRect rcDest;
if (pDC->IsPrinting())   // printer DC
{
// get size of printer page (in pixels)
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// get printer pixels per inch
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);


//
// Best Fit case -- create a rectangle which preserves
// the DIB's aspect ratio, and fills the page horizontally.
//
// The formula in the "->bottom" field below calculates the Y
// position of the printed bitmap, based on the size of the
// bitmap, the width of the page, and the relative size of
// a printed pixel (cyInch / cxInch).
//
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
/ ((double)cxDIB * cxInch));
rcDest.right = cxPage;


// 计算打印图像位置(垂直居中)
int temp = cyPage - (rcDest.bottom - rcDest.top);
rcDest.bottom += temp/2;
rcDest.top += temp/2;
}
else   // not printer DC
{
rcDest = rcDIB;
}
::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}


6、文档类中添加:

BOOL Ctest1Doc::OnOpenDocument(LPCTSTR lpszPathName) 
{
CFile file;
CFileException fe;
if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
{
ReportSaveLoadException(lpszPathName, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}


DeleteContents();
BeginWaitCursor();


// replace calls to Serialize with ReadDIBFile function
TRY
{
m_hDIB = ::ReadDIBFile(file);
}
CATCH (CFileException, eLoad)
{
file.Abort(); // will not throw an exception
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eLoad,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB = NULL;
return FALSE;
}
END_CATCH


InitDIBData();
EndWaitCursor();


if (m_hDIB == NULL)
{
// may not be DIB format
CString strMsg;
strMsg = "不支持此类型文件!";


// 提示出错
::MessageBox(NULL, strMsg, _T("系统提示"), MB_ICONINFORMATION | MB_OK);


// 返回FALSE
return FALSE;
}
SetPathName(lpszPathName);
SetModifiedFlag(FALSE);     // start off with unmodified
return TRUE;
}


void Ctest1Doc::InitDIBData()
{
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDIB == NULL)
{
return;
}
// Set up document size
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
{
::GlobalUnlock((HGLOBAL) m_hDIB);
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
CString strMsg=_T("this dib is too big");
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
return;
}
m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
// Create copy of palette
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
// we must be really low on memory
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
return;
}
if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
{
// DIB may not have a palette
delete m_palDIB;
m_palDIB = NULL;
return;
}
}


BOOL Ctest1Doc::OnSaveDocument(LPCTSTR lpszPathName) 
{
CFile file;
CFileException fe;


if (!file.Open(lpszPathName, CFile::modeCreate |
CFile::modeReadWrite | CFile::shareExclusive, &fe))
{
ReportSaveLoadException(lpszPathName, &fe,
TRUE, AFX_IDP_INVALID_FILENAME);
return FALSE;
}


// replace calls to Serialize with SaveDIB function
BOOL bSuccess = FALSE;
TRY
{
BeginWaitCursor();
bSuccess = ::SaveDIB(m_hDIB, file);
file.Close();
}
CATCH (CException, eSave)
{
file.Abort(); // will not throw an exception
EndWaitCursor();
ReportSaveLoadException(lpszPathName, eSave,
TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
return FALSE;
}
END_CATCH


EndWaitCursor();
SetModifiedFlag(FALSE);     // back to unmodified


if (!bSuccess)
{
// may be other-style DIB (load supported but not save)
//  or other problem in SaveDIB
CString strMsg=_T("cantnot save this dib");
MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
}


return bSuccess;
}

7、在这里初始化,否则会有野指针

CDEMO1Doc::CDEMO1Doc()
{
// TODO: add one-time construction code here
m_hDIB = NULL;
m_palDIB = NULL;
m_sizeDoc = CSize(1,1);//用于显示滚动条,此变量的值在InitDIBData中得到。

}


8、程序中的CMyView是从CView继承来的,我现在想使它有滚动条,那就得从CScrollView继承

有这几个地方需要改动
1。IMPLEMENT_DYNCREATE(CDemo1View, CScrollView)
2。BEGIN_MESSAGE_MAP(CDemo1View, CScrollView)
3。所有COMMAND宏
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
4。重载的基类函数
         return CScrollView::PreCreateWindow(cs);

5。最后就是类的声明

class CDemo1View : public CScrollView


在OnInitUpdate中定义视图区域大小(先在头文件中声明此函数)

void CXXView::OnInitialUpdate() 
{
CScrollView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CSize sizeTotal(1024,680);
CSize sizePage(1024,680);
CSize sizeLine(128,70);
SetScrollSizes(MM_TEXT,sizeTotal,sizePage,sizeLine);//MM_LOENGLISH
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值