CString strPathName = FileDlg.GetPathName(); AfxGetApp()-OpenDocumentFile( strPathName ); nIndex = (int) FileDlg.m_ofn.nFilterIndex; if( !ReadImgToDoc() ) { AfxMessageBox(无法载入图像文件!); return; } } } 在CDipDoc类的消息映射函数OnFileOpen()中, 除了要调用一个CDipDoc类的公有成员函数ReadImgToDoc()外(该函数将在后面进行说明), 其他与CDipApp类消息映射函数OnFileOpen()基本一样。 注意:在此再一次使用了数组szFilter,但它并不在“DipDoc.h”和“DipDoc.cpp”两个文件中声明,因此需要在“DipDoc.cpp”头部添加如下外部声明信息。 // DipDoc.cpp : implementation of the CDipDoc class // #include stdafx.h #include Dip.h #include DipDoc.h #include MainFrm.h #include ChildFrm.h #include DipView.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[]= - -FILE- -; #endif ? extern char szFilter[]; 在文件“DipDoc.cpp”中同时还包含了“MainFrm.h”、 “ChildFrm.h”和“DipView.h”三个头文件, 因为CDipDoc类的成员函数必须使用它们链接指定的代码。 成员函数ReadImgToDoc()负责读入图像对象文件, 其声明与实现文件如下。 在“DipDoc.h”文件添加如下代码: //函数声明 public: BOOL ReadImgToDoc(); 在“DipDoc.cpp”文件添加代码: //函数实现 BOOL CDipDoc:: ReadImgToDoc() { CString strPathName = GetPathName(); //设置等待光标 BeginWaitCursor(); m_pDibObject = new CDibObject( strPathName.GetBuffer(3) ); //取消等待光标 EndWaitCursor(); ? //读入图像文件失败 if( m_pDibObject == NULL ) { AfxMessageBox(无法创建图像类对象!); //返回FALSE return(FALSE); } //读入图像文件成功, 设置相应变量 m_bImageLoaded = TRUE; //返回TRUE return(TRUE); } 在ReadImgToDoc()成员函数中,首先设置等待光标,然后,以文件打开对话框返回的文件名为参数调用CDibObject类的构造函数创建CDibObject对象m_pDibObject,从而将图像文件中保存的图像数据读入内存,并将文档类的m_bImageLoaded设为TRUE。 (3) ID_FILE_SAVE_AS消息处理函数代码如下: void CDipDoc:: OnFileSaveAs() { // TODO: Add your command handler code here static int nIndex = 1; CFileDialog DialogSaveAs(FALSE, NULL, m_pDibObject-GetImageName(), OFN_HIDEREADONLY, szFilter ); DialogSaveAs.m_ofn.nFilterIndex = (DWORD) nIndex; if( DialogSaveAs.DoModal() == IDOK ) { CMainFrame *pMainFrame = ( CMainFrame *)
原创力文档

文档评论(0)