1.首先下载DLL&DIP文件(内容为图1)。
2.将ImageLoad.dll拷贝到应用程序所在的目录、 Windows目录、 Windows\System目录或Windows\System32目录。
3.创建新的MFC【exe】工程,并命名为Dip。(图2) 在第六步将基类改为CScrollView(图3) 4.将图 1中文件复制到工程目录下。
5.选择VC++集成开发环境的Project\Add to project\Files…菜单项,系统打开如图4所示的 “Insert Files into Project” 对话框,将图1中的文件选中并点击确认添加到工程中。
6.工具栏→project→设置→连接→对象/库模块→输入ImageLoad.lib添加库文件。(图5)
7.因为应用程序类CDipApp带有自己的“文件/新建”和“文件/打开”菜单,在此为这两个菜单项添加消息映射处理函数。使用MFC ClassWizard分别为ID_FILE_NEW和ID_FILE_OPEN两个菜单命令消息添加“OnFileNew”和“OnFileOpen”,操作如下图6所示。 选择resourceview→menu→IDR DIPTYPE→文件(点击右键)→建立类向导→右上角class name处选择CDipApp→左侧Object IDs处选择ID_FILE_NEW和ID_FILE_OPEN→右侧messages处选择command→选择add添加→确定
8.选择fileview→source files→在Dip.cpp文件头部添加如下代码:(图7) char szFilter[]=“位图文件(.BMP)|.BMP|图形交换格式文件(.GIF)|.GIF|PCX文件(.PCX)|.PCX|TGA文件(.TGA)|.TGA|JPEG文件(.JPG)|.JPG|标记图像文件(.TIF)|.TIF|所有支持图片|*.BMP, *.GIF, .PCX, .TGA, .JPG, .TIF|所有文件(.)|.||”;
9.添加类向导之后Dip.cpp文件最下方会增加两个函数,其中CDip::OnFileNew函数不编写任何代码,用于防止自动建立新文件。成员函数CDip:: OnFileOpen负责打开一个图像文件, 其代码如下:(图8) void CDipApp::OnFileOpen() { // TODO: Add your command handler code here static int nIndex = 1; CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); FileDlg.m_ofn.nFilterIndex = (DWORD) nIndex; if( FileDlg.DoModal() == IDOK ) { CString PathName = FileDlg.GetPathName(); PathName.MakeUpper(); OpenDocumentFile( PathName ); nIndex = (int) FileDlg.m_ofn.nFilterIndex; } }
10.文档类CDipDoc负责维护文档的具体数据。因此,选择fileview→header files→ 在CDipDoc.h中声明两个公有型的成员变量:(图9) public: BOOL m_bImageLoaded; CDibObject *m_pDibObject;
11.另外,声明的指针型变量还应在类的构造函数和析构函数中正确的初始化和释放。需选择fileview→source files→ 在CDipDoc.cpp中更改构造函数和析构函数,代码如下:(注:类中自带构造函数与析构函数,不用另外添加,否则会出错)图10 CDipDoc::CDipDoc() { // TODO: add onetime construction code here m_pDibObject = NULL; m_bImageLoaded = FALSE; }
CDipDoc:: ~CDipDoc() { if(m_pDibObject != NULL) { delete m_pDibObject; m_pDibObject = NULL; } }
12.“StdAfx.h”文件中添加如下代码:(注:必须放在第二个endif下方,否则会出错)图11 #include “DibObject.h”
13.为CDipDoc文档类映射消息处理函数,使用MFC ClassWizard分别为ID_FILE_NEW、 ID_FILE_OPEN、 ID_FILE_SAVE AS菜单命令消息添加消息映射处理函数OnFileNew()、 OnFileOpen()和OnFileSaveAs()函数。操作类似上面对CDipApp的操作。(图12) 14.同样对DipDoc.cpp改写函数,代码如下:(图13,14)(注意区分三个函数) void CDipDoc::OnFileNew() { // TODO: Add your command handler code here
}
void CDipDoc:: OnFileOpen() { // TODO: Add your command handler code here static int nIndex = 1; CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); FileDlg.m_ofn.nFilterIndex = (DWORD) nIndex; if( FileDlg.DoModal() == IDOK ) { CString strPathName = FileDlg.GetPathName(); AfxGetApp()->OpenDocumentFile( strPathName ); nIndex = (int) FileDlg.m_ofn.nFilterIndex; if(!ReadImgToDoc()) { AfxMessageBox(“无法载入图像文件!”); return; } } }
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 *)AfxGetMainWnd(); CChildFrame *pChildFrame = (CChildFrame * ) pMainFrame->MDIGetActive(); CDipView *pDipView = ( CDipView * )pChildFrame->GetActiveView(); nIndex = (int) DialogSaveAs.m_ofn.nFilterIndex; if( nIndex == 5 ) { if( m_pDibObject->GetNumBits() != 24 ) { AfxMessageBox(“24位真彩色图像才能存为JPEG格式!”); return; } } if( m_pDibObject != NULL ) { CString strPathName = DialogSaveAs.GetPathName(); int nFindIndex = strPathName.Find("."); if( nFindIndex != -1) strPathName = strPathName.Left( nFindIndex ); strPathName += CDibObject:: szExtensions[nIndex -1]; m_pDibObject->Save( strPathName ); CString strFileName = DialogSaveAs.GetFileName(); nFindIndex = strFileName.Find("."); if ( nFindIndex != -1 ) strFileName = strFileName.Left( nFindIndex ); strFileName += CDibObject:: szExtensions[nIndex -1]; pChildFrame->SetWindowText( strFileName ); SetPathName( strPathName ); if( nIndex == 5 ) { m_pDibObject->Load( strPathName ); pDipView->InvalidateRect( NULL, FALSE ); pDipView->UpdateWindow(); } } } } 15.在DipDoc.cpp头部添加程序,代码如下:(图15)(忘了是哪一段就都粘贴进来了) #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[]; 16. 在“DipDoc.h”文件添加如下代码:(图16) //函数声明 public: BOOL ReadImgToDoc(); 17.在“DipDoc.cpp”文件添加代码:(图13)(注:本函数为文件自带函数,请在原本函数位置添加) //函数实现 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); }
18.具体的图像绘制任务是在CDipView视类成员函数CDipView:: OnDraw中完成的,选择CDipView.cpp,其代码如下:(图17) void CDipView:: OnDraw(CDC* pDC) { CDipDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if( !pDoc->m_bImageLoaded) { pDoc->ReadImgToDoc(); } //滚动窗口 CSize sizeTotal; sizeTotal.cx = pDoc->m_pDibObject->GetWidth(); sizeTotal.cy = pDoc->m_pDibObject->GetHeight(); SetScrollSizes (MM_TEXT, sizeTotal); //获取客户区尺寸 OnPrepareDC(pDC); CRect Rect; GetClientRect( &Rect ); //获取图像宽度及高度 int nImageWidth, nImageHeight; nImageWidth = pDoc->m_pDibObject->GetWidth(); nImageHeight = pDoc->m_pDibObject->GetHeight(); //当图像实际尺寸小于窗口尺寸时, 将图像放在客户区中间 int nX, nY; if( nImageWidth < Rect.Width() ) nX = (Rect.Width() - nImageWidth ) / 2; else nX = 0; if( nImageHeight < Rect.Height() ) nY = (Rect.Height() -nImageHeight ) / 2; else nY = 0; pDoc->m_pDibObject->Draw(pDC, nX, nY); } 19.全部配置完成后编译全部文件并运行即可:(图18) 注意:1.本篇博客并非全部原创,部分操作及代码参考百度文库中的vc6配置操作ppt,侵删! 2.配置所需文件网上有大量资源,在此不提供下载链接。 3.配置中出现问题或文章中有错误的地方欢迎各位在评论留言。 4.本篇博客为实验报告作业,禁止转载。