本科《c程序设计》visual_c_mfc入门教程

上传人:灯火****19 文档编号:168845441 上传时间:2022-11-12 格式:DOCX 页数:123 大小:171.68KB
收藏 版权申诉 举报 下载
本科《c程序设计》visual_c_mfc入门教程_第1页
第1页 / 共123页
本科《c程序设计》visual_c_mfc入门教程_第2页
第2页 / 共123页
本科《c程序设计》visual_c_mfc入门教程_第3页
第3页 / 共123页
资源描述:

《本科《c程序设计》visual_c_mfc入门教程》由会员分享,可在线阅读,更多相关《本科《c程序设计》visual_c_mfc入门教程(123页珍藏版)》请在装配图网上搜索。

1、Visual C+/MFC 入门教程Visual C+/MFC 入门教程-1 -VC开发指南-5-1.1 如何学好VC-51.2 理解 Windows消息机制-6 -1.3 利用Visual C+/MFC开发Windows程序的优势-7 -1.4 利用MFC进行开发的通用方法介绍-91.5 MFC中常用类,宏,函数介绍-10-2.1 和GUI有关的各种对象-11-2.3 使用点,刷子,笔进行绘图-18-2.4 在窗口中绘制设备相关位图,图标,设备无关位图-20-2.5 使用各种映射方式-23-2.6 多边形和剪贴区域-24-3.1 文档视图框架窗口间的关系和消息传送规律-25-3.2 接收用户

2、输入-25-3.3 使用菜单-29-3.4 文档,视,框架之间相互作用-32-3.5 利用序列化进行文件读写-33 -3.6 MFC中所提供的各种视类介绍-354.1 Button- 36 -4.5 List Box/Check List Box- 39 -4.6 Combo Box- 41 -4.7 Tree Ctrl- 42 -4.8 List Ctrl-44 -4.9 Tab Ctrl-47 -4.10 A Tool Bar- 48 -4.11 Dialog Bar- 50 -4.12 利用 AppWizard 创建并使用 ToolBar StatusBar Dialog Bar- 51

3、 -4.13 General Window 55 5.1 使用资源编辑器编辑对话框-57-5.2 创建有模式对话框-59-(1)如何通过代码获得应用程序主窗口的 指针?-61(2) 确定应用程序的路径-61 -(3)如何在程序中获得其他程序的图标?- 62 -(4)获得各种目录信息-63(5) 女U |可 自 SelectObject(&penl);选择对象进 DC drawWithPeni.(CPen*)pDC-SelectObject(&pen2);选择对象进 DCdrawWithPen2.pen 1 .DeleteObject。;/再次创建前先销毁pen 1 .CreatePen(PS_

4、SOLID2RGB(0,0,0);再次创建对象(CPen*)pDCSelectObject(&penl);选择对象进 DCdrawWithPeni.pDCSelectObject(pOldPen); 恢复此外系统中还拥有一些库存GUI对象,你可以利用CDC:SelectStockObject(SelectStockObject( int nlndex)选入这些对象,它们包括些固定颜色的刷子,画笔和一些基本字体。BLACK_BRUSH Black brush.DKGRAY.BRUSH Dark gray brush.GRAY.BRUSH Gray brush.HOLLOW_BRUSH Hollo

5、w brush.LTGRAY.BRUSH Light gray brush.NULL_BRUSH Null brush.WHITE_BRUSH White brush.BLACK_PEN Black pen.NULL.PEN Null pen.WHITE_PEN White pen.ANSLFIXED_FONT ANSI fixed system font.ANSI_VAR_FONT ANSI variable system font.DEVICE_DEFAULT_FONT Device-dependent font.OEM_FIXED_FONT OEM-dependent fixed fon

6、t.SYSTEM_FONT The system font. By default, Windows uses the system font to draw menus, dialog-box controls, and other text. In Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.SYSTEM_FIXED_FONT The fixed-width system fon

7、t used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.DEFAULT_PALETTE Default color palette. This palette consists of the 20 static colors in the system palette.这些对象留在DC中是安全的,所以你可以利用选入库存对象来作为恢复DC中GUI对象。大家可能都注意到了绘图时都需要一个DC对象,DC (Device Con

8、text设备环境)对象是个抽 象的作图环境,可能是对应屏幕,也可能是对应打印机或其它。这个环境是设备无关的,所以你 在对不同的设备输出时只需要使用不同的设备环境就行了,而作图方式可以完全不变。这也就是 Windows耀眼的一点设备无关性。如同你将对一幅画使用照相机或复印机将会产生不同的输出, 而不需要对画进行任何调整。DC的使用会穿插在本章中进行介绍。2.2 在窗口中输出文字在这里我假定读者己经利用Applicationwizard生成了一个SD!界面的程序代码。接下来的你只 需要在CView派生类的OnDraw成员函数中加入绘图代码就可以了。在这里我需要解释一下 OnDraw函数的作用,On

9、Draw函数会在窗口需要重绘时自动被调用,传入的参数CDC* pDC对 应的就是DC环境。使用OnDraw的优点就在于在你使用打印功能的时候传入OnDraw的DC环 境将会是打印机绘图环境,使用打印预览时传入的是个称为CPreviewDC的绘图环境,所以你 只需要一份代码就可以完成窗口/打印预览/打印机绘图三重功能。利用Windows的设备无关性和 乂$为打印预览所编写的上千行代码你可以很容易的完成一个具有所见即所得的软件。输出文字一般使用 CDC:BOOL TextOut( int x, int y, const CString& str )和 CDC:int DrawText( const

10、 CString& str, LPRECT IpRect, UINT nFormat)两个函数,对 TextOut 来讲只能输出 单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文字,并且可以规定对齐方式 和使用何种风格。nFormat可以是多种以下标记的组合(利用位或操作)以达到选择输出风格的 目的。DT_BOTTOM 底部对齐 Specifies bottom-justified text. This value must be combined with DT.SINGLELINE.DT_CALCRECT 计算指定文字时所需要矩形尺寸 Determines the wid

11、th and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by IpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that

12、it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.DT.CENTER 中部对齐 Centers text horizontally.DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits

13、in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified.You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash () charact

14、ers, DT_PATH_ELLI PS IS preserves as much as possible of the text after the last backslash.DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight.DT_EXTERNALLEADING Includes the font 扌且 external leading in the line height. Normally, external leading is not included i

15、n the height of a line of text.DT_LEFT 左对齐 Aligns text flush-left.DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified.Note Some uFormat flag combinations can cause the passed string to be modi

16、fied. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the stringto be modified, causing an assertion in the CString override.DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used.DT_NOPREFIX 禁止使用&前缀 Turns off processing of prefix charact

17、ers. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off.DT_PATH_EL

18、L1PS1SDT.RIGHT 右对齐 Aligns text flush-right.DT_SINGLELINE 单行输出 Specifies single line only. Carriage returns and linefeeds do not break the line.DT_TABSTOP 设置 TAB 字符所占宽度 Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters per tab is

19、 eight.DT_TOP 定部对齐 Specifies top-justified text (single line only).DT_VCENTER 中部对齐 Specifies vertically centered text (single line only).DT_WORDBREAK 每行只在单词间被折行 Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by Ip

20、Rect. A carriage return 札 inefeed sequence will also break the line.在输出文字时如果希望改变文字的颜色,你可以利用CDC:SetTextColor( COLORREF crColor ) 进行设置,如果你希望改变背景色就利用CDC:SetBkColor( COLORREF crColor ),很多时候你 可能需要透明的背景色你可以利用CDC:SetBkMode( int nBkMode )设置,可接受的参数有OPAQUE Background is filled with the current background colo

21、r before the text, hatched brush, or pen is drawn. This is the default background mode.TRANSPARENT Background is not changed before drawing.接下来讲讲如何创建字体,你可以创建的字体有两种:库存字体CDC:CreateStockObject( int nlndex )和自定义字体。在创建非库存字体时需要填充一个LOGFONT结构并使用CFont:CreateFontIndirect(const LOGFONT* IpLogFont )(可以参考文章在同一系统

22、中显示GB字符和BIG5字符),或使用 CFont:CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientation, int nWeight, BYTE bltalic, BYTE bUnderline, BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR IpszFacename )其中的参数和 LOGFONT中的分量有一定的对应关系

23、。下面分别讲解参数的意义:nHeight字体高度(逻辑单位)等于零为缺省高度,否则取绝对值并和可用的字体高度进行匹配。nWidth宽度(逻辑单位)如果为零则使用可用的横纵比进行匹配。nEscapement出口矢量与X轴间的角度nOrientation字体基线与X轴间的角度nWeight字体粗细,可取以下值Constant ValueFW_DONTCARE 0FW_THIN 100FW_EXTRALIGHT 200FW.ULTRALIGHT 200FW.LIGHT 300FW_NORMAL400FW_REGULAR 400FW_MEDIUM 500FW_SEMIBOLD 600FW_DEMIBO

24、LD 600FW_BOLD 700FW_EXTRABOLD 800FW_ULTRABOLD 800FW_BLACK 900FW_HEAVY 900bltalic是否为斜体bUnderline是否有下划线 cStrikeOut是否带删除线nCharSet指定字符集合,可取以下值Constant ValueANSI.CHARSET 0DEFAULT_CHARSET 1SYMBOL.CHARSET 2SHIFTJIS_CHARSET 128OEM.CHARSET 255nOutPrecision 输出精度OUT_CHARACTER_PRECIS OUT_STRING_PREC1SOUT_DEFAUL

25、T_PRECIS OUT_STROKE_PRECISOUT_DEVICE_PRECIS OUT_TT_PRECISOUT_RASTER_PREC1SnClipPrecision剪辑精度,可取以下值CLIP_CHARACTER_PRECIS CLIP_MASKCL1P_DEFAULT_PREC1S CL1P_STROKE_PRECISCLIP.ENCAPSULATE CLIP_TT_ALWAYSCLIP_LH_ANGLESnQuality输出质量,可取以下值DEFAULT_QUAL1TY Appearance of the font does not matter.DRAFT_QUALITY A

26、ppearance of the font is less important than when PROOF_QUALITY is used. For GDI raster fonts, scaling is enabled. Bold, italic, underline, and strikeout fonts are synthesized if necessary.PROOF_QUALITY Character quality of the font is more important than exact matching of the logical-font attribute

27、s. For GDI raster fonts, scaling is disabled and the font closest in size is chosen. Bold, italic, underline, and strikeout fonts are synthesized if necessary.nPitchAndFamily字体间的间距IpszFacename指定字体名称,为了得到系统所拥有的字体可以利用EmunFontFamiliesEx。(可以 参考文章在同一系统中显示GB字符和BIG5字符)此外可以利用CFontDialog来得到用户选择的字体的LOGFONT数据。

28、最后我讲下文本坐标的计算,利用CDC:GetTextExtent( const CString& str )可以得到字符串的 在输出时所占用的宽度和高度,这样就可以在手工输出多行文字时使用正确的行距。另外如果需 要更精确的对字体高度和宽度进行计算就需要使用CDC:GetTextMetrics( LPTEXTMETR1C IpMetrics )该函数将会填充TEXTMETRIC结构,该结构中的分量可以非常精确的描述字体的各 种属性。2.3 使用点,刷子,笔进行绘图在 Windows中画点的方法很简单,只需要调用COLORREFCDC:SetPixel( int x, int y, COLORRE

29、F crColor)就可以在指定点画上指定颜色,同时返回原来的颜色。COLORREF CDC:GetPixel( int x, int y)可以得到指定点的颜色。在Windows中应该少使用画点的函数,因为这样做的执行效率比 较低。刷子和画笔在Windows作图中是使用最多的GUI对象,本节在讲解刷子和画笔使用方法的同时 也讲述写基本作图函数。在画点或画线时系统使用当前DC中的画笔,所以在创建画笔后必须将其选入DC会在绘图时 产生效果。画笔可以通过CPen对象来产生,通过调用CPen:CreatePen( int nPenStyle, int nWidth, COLORREFcrColor)来

30、创建。其中nPenStyle指名画笔的风格,可取如下值:PS_SOLID 实线 Creates a solid pen.PS_DASH 虚线,宽度必须为 Creates a dashed pen. Valid only when the pen width is 1 or less, in device units.PS_DOT 点线,宽度必须为 Creates a dotted pen. Valid only when the pen width is 1 or less, in device units.PS_DASHDOT 点划线,宽度必须为 Creates a pen with alt

31、ernating dashes and dots. Valid only when the pen width is 1 or less, in device units.PS_DASHDOTDOT 双点划线,宽度必须为 Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units.PS_NULL空线,使用时什么也不会产生Creates a null pen.PS_ENDCAP_ROUND 结束处为圆形 End caps are round.PS_ENDCAP_SQUARE 结束处为方形 End caps are square.nWidth和crColor为线的宽度和颜色。刷子是在画封闭曲线时用来填充的颜色,例如当你画圆形或方形时系统会用当前的刷子对内部进 行填充。刷子可利用CBrush对象产生。通过以下几种函数创建刷子:BOOL CreateSolidBrush( COLORREF crColor)1创建一种固定颜色的刷子BOOL CreateHatchBrush( int n

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!