资源预览内容
第1页 / 共38页
第2页 / 共38页
第3页 / 共38页
第4页 / 共38页
第5页 / 共38页
第6页 / 共38页
第7页 / 共38页
第8页 / 共38页
第9页 / 共38页
第10页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
VC+ 入门与提高主讲教师:贾澎涛第六章 应用实例之一编写一 个具有实用功能的字处理软件本篇经典n文档和视图是怎样联系起来的?n如何为程序添加查找、替换等功能?n为什么在某些程序新创建的菜单中只输入了一个菜 单ID标识就可以实现某一种功能?n怎样为程序添加一个浮动菜单?n如何在“帮助关于”对话框中添加系统信息?n怎样为应用程序做一个每日提示(在程序启动时显 示)?n如何动态修改菜单n怎样为应用程序增加一个日历?n怎样修改应用程序标题栏上的显示文字?n怎样使应用程序以最大化窗口显示?n怎样在应用程序的状态条上显示时间?n怎样使应用程序支持拖放功能?n怎样建立一个文件打开、保存对话框?n如何为应用程序添加一个闪屏?n如何使用字体、颜色对话框?n如何处理文字输入?n如何实现应用程序对数据的读出和写入?14.如何为应用程序添加一个闪屏 ?1)打开工程项目EditSoft; (2)选择“ProjectAdd to ProjectComponents and Controls”将出现组件和控件库对话框,双击部件文 件夹(Visual C+ Components),选择Splash screen,双 击。出现插入部件确认对话框,选择“确定”,出现部 件类的实现文件和头文件的文件名对话框,保留缺省 值,选择“OK”按钮。 (3)将返回到“Components and Controls Gallery”,单击 “Close”,插入过程完成。 (4)编译、连接并运行。(5)我们发现主窗口和闪屏同时出现了,所以修改应用 类的InitInstance()函数,隐藏主窗口。pMainFrame-ShowWindow(SW_HIDE); (6)图形界面不美观。设计一张位图,在启动窗口显示 。 可以在ResourceView中删除原有的图片,插入你想 要的闪屏图片,然后将其ID改为IDS_SPLASH。或按 照以下方法加入一个真彩图片。 (7)下一步我们来实现装载24位真彩位图的,我们直接 采用读入文件的方式,所以我们将闪屏类的Create函 数改写成下面这样。BOOL CSplashWnd:Create(CWnd* pParentWnd /*= NULL*/) HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(“SnpSplash.bmp“),/图像文件名称 ,注意换成你的文件名称。 IMAGE_BITMAP,/读入位图,还可以设定为读入图标或鼠 标指针 0,/位图宽 0,/位图高 LR_CREATEDIBSECTION/创建DibSection |LR_LOADFROMFILE);/从文件读 if(hBitmap=NULL) return false; m_bitmap.Attach(hBitmap);/获得位图指针 BITMAP bm; m_bitmap.GetBitmap(return CreateEx(0, AfxRegisterWndClass(0, AfxGetApp()- LoadStandardCursor(IDC_ARROW), NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, pParentWnd-GetSafeHwnd(), NULL); (8)最后我们来完成闪屏消失,主窗口显示的部分 void CSplashWnd:HideSplashScreen() / Destroy the window, and update the mainframe. DestroyWindow(); AfxGetApp()-m_pMainWnd- ShowWindow(SW_SHOWMAXIMIZED);/恢复了我们的最大 化主窗口 AfxGetApp()-m_pMainWnd-UpdateWindow();说明nCBitmap The CBitmap class encapsulates a Windows graphics device interface (GDI) bitmap and provides member functions to manipulate the bitmap. To use a CBitmap object, construct the object, attach a bitmap handle to it with one of the initialization member functions, and then call the objects member functions.nLoadImage The LoadImage function loads an icon, cursor, or bitmap. HANDLE LoadImage( HINSTANCE hinst, / handle of the instance containing the image LPCTSTR lpszName, / name or identifier of image UINT uType, / type of image int cxDesired, / desired width int cyDesired, / desired height UINT fuLoad / load flags ); 15.如何使用字体、颜色对话框 ?与文件对话框一样,字体、颜色对话框也是 Windows通用对话框的两个重要成员,他们是从 CCommonDialog类派生出来的,通过调用 Windows的COMMDLG.DLL动态连接库实现。 在大多数的字处理软件中,字体和颜色对话框的 实现均放置在视图类中并将其结果返回给文档类 ,以便于文档对字体和颜色进行保存和维护,视 图类则通过调用GetDocument()函数或的文档 的字体和颜色。通用对话框的调用分三个步骤: (1)用MFC的封装类创建一个对话框对象,要确定 已经把参数传递给对象的构造函数以初始化对话框; (2)对象调用DoModal()成员函数,从DoModal()的返 回值标明对话框存在的方式(IDOK或IDCCANCLE) ; (3)如果DoModal()函数返回IDOK,则把公用对话框 的成员函数的返回值赋与用户。使用字体对话框n在字体对话框的操作中,用户进行的操作结 果存放在CFontDialog的数据变量m_cf中, m_cf 是一个保存字体对话框对象属性的一个结构, 在构造CFontDialog对象之后,在调用DoModal() 函数之前,可使用m_cf修改对话框的各种属性 。CFontDialogThe CFontDialog class allows you to incorporate a font- selection dialog box into your application. A CFontDialog object is a dialog box with a list of fonts that are currently installed in the system. The user can select a particular font from the list, and this selection is then reported back to the application. CFontDialog:CFontDialog ( LPLOGFONT lplfInitial = NULL, DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd* pParentWnd = NULL );CFontDialog Class Members DoModalDisplays the dialog and allows the user to make a selection. GetCurrentFontRetrieves the name of the currently selected font. GetFaceNameReturns the face name of the selected font. GetStyleNameReturns the style name of the selected font. GetSizeReturns the point size of the selected font. GetColorReturns the color of the selected font. GetWeightReturns the weight of the selected font. IsStrikeOutDetermines whether the font is displayed with strikeout. IsUnderlineDetermines whether the font is underlined. IsBoldDetermines whether the font is bold. IsItalicDetermines whether the font is italic.(1)新建一个工程EditScroll,在Step 6中将视图类的基 类改为CScollView类。 (2)在“编辑”菜单下增加子菜单:字体,ID: ID_EDIT_FONT。为其增加消息处理函数。 (3)在文档类的定义中添加如下代码:CString m_Text; /定义一个存放文本的字符串变量CFont m_Font;/定义一个字体变量 LOGFONT m_lf; /定义一个LOGFONT结构变量 CPoint m_StartPoint; /定义文字的初始显示位置(4)在字体的消息处理函数中添加如下代码:void CEditScollView:OnEditFont() CEditScollDoc *pDoc=GetDocument(); ASSERT_VALID(pDoc); CFontDialog FontDlg( /定义一个字体对话框对象 FontDlg.DoModal(); /调用字体对话框 InvalidateRect(NULL); /通知视图数据已经改变,请刷新 (5)在视图类的OnDraw()函数中编辑显示代码:/ TODO: add draw code for native data here int nOldMode=pDC-SetBkMode(TRANSPARENT); pDoc-m_Font.CreateFontIndirect( CFont *pOldFont=pDC-SelectObject( pDC-TextOut(pDoc-m_StartPoint.x,pDoc- m_StartPoint.y,pDoc-m_Text); pDC-SelectObject(pOldFont); pDoc-m_Font.DeleteObject(); pDC-SetBkMode(nOldMode);(6)在文档类的新建文件函数OnNewDocument中初始 化有关变量。然后编译连接。 / (SDI documents will reuse this document) m_Text=“同学们好!“; m_StartPoint=CPoint(0,0); m_lf.lfHeight=20; m_lf.lfWidth=0; m_lf.lfEscapement=0; m_lf.lfOrientation=0; m_lf.lfWeight=FW_NORMAL; m_lf.lfItalic=0; m_lf.
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号