1 / 160

第六章 绘图与打印

第六章 绘图与打印. 在 Windows 应用程序中,无论被 显示输出的数据 的形式是 图 形 、 图象 还是 文本 ,也不管输出设备是 显示器 、 打印机 、 内存 还是其他输出设备,都是通过 图形设备接口 ( GDI ) 以 图形 的 形式来处理的。也就是 Windows 程序通过调用 GDI 函数,请求 进行各类 绘制 操作; GDI 通过相应的 设备驱动 程序,完成各类 绘图 操作在特定 硬件 设备(如 显示器 、 打印机 )上的 显示输出 。 GDI 提供的 绘制 操作有三种类型:.

lara
Download Presentation

第六章 绘图与打印

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第六章 绘图与打印

  2. 在 Windows 应用程序中,无论被显示输出的数据的形式是图 形、图象还是文本,也不管输出设备是显示器、打印机、内存 还是其他输出设备,都是通过图形设备接口(GDI) 以图形的 形式来处理的。也就是 Windows程序通过调用 GDI 函数,请求 进行各类绘制操作;GDI 通过相应的设备驱动程序,完成各类 绘图操作在特定硬件设备(如显示器、打印机)上的显示输出。 GDI 提供的绘制操作有三种类型:

  3. ·矢量图形绘制: 用于创建和操作用矢量描述的图形,例如·矢量图形绘制: 用于创建和操作用矢量描述的图形,例如 点、直线、曲线等几何图形。这类图形的数据量小,存储需 求低、易于几何变换和传输,但显示速度慢。 ·光栅图形绘制: 用于对以点阵形式存储的位图图形进行操 作,包括各种位图和图标的输出。这类图形输出的优点是显 示速度快,因为它是直接从内存到显存的拷贝操作。但缺点 是数据量大,存储需求高,不利于几何变换和传输。 ·文本输出:用于以图形方式进行各种文本输出操作。按图形 方式输出文本也给文本输出带来很大的灵活性。用户可以通 过调用各种 GDI 函数,实现各种文本输出效果,包括加粗、 斜体、设置颜色等。还可以通过参数来调整组成文字的直线 的长度和曲线的形状,从而实现对字体的自由缩放操作。

  4. MFC 为了支持上述的绘制任务,提供了一套简单的机制。在 MFC 应用程序中绘图操作都是通过使用设备环境类 CDC(也称 为设备上下文类)对象和基本“绘图工具” —— CGdiObject(图形 设备接口对象)类的派生类(画笔类 CPen、画刷类 CBrush 等) 对象来实现的。

  5. 6.1 MFC 绘图需要哪些要素 MFC 绘图要素主要由设备环境和图形设备接口对象两大类族 组成。 6.1.1 设备环境 Windows 通过设备环境结构 Device Context描述了一个虚拟于 物理输出设备的绘图设备环境(提供绘制操作的条件、状态和 基本绘图操作)。任何绘图操作都必须在此结构描述的虚拟设 备环境中进行。MFC 为绘图环境提供了建立在设备环境结构 Device Context之上的一系列 C++ 类,其中基类为 CDC,它与 Windows 平台提供的设备环境相关联,并将设备环境句柄和通过 该句柄实现的绘制操作封装起来。

  6. CDC CClientDC CPaintDC CWindowDC CMateFileDC ·MFC 类:CDC 及 CDC 的派生类。 ·作用:提供绘制操作的环境,每个 CWnd及其派生类对象都 通过内含的 CDC 对象成员提供一个设备环境。 ·组成:用于绘制操作的一组描述设备环境的属性,一套用于 创建、选取所需要的绘图工具和进行各种绘图操作、以及其他支持绘图实现的成员函数。 MFC 中设备环境类的派生层次如下: CObject

  7. 1 CDC类 The CDC class defines a class of device-context objects. The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window. Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes. It also provides member functions for getting and setting drawing attributes, mapping, working with the viewport, working with the window extent, converting coordinates, working withregions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working withfonts, using printer escapes, scrolling, and playing metafiles.

  8. To use a CDC object, construct it, and then call its member functions that parallel Windows functions that use device contexts. For specific uses, the Microsoft Foundation Class Library provides several classes derived from CDC . CPaintDCencapsulates calls to BeginPaint and EndPaint. CClientDC manages a display context associated with a window’s client area. CWindowDC manages a display context associated with an entire window, including its frame and controls. CMetaFileDC associates a device context with a metafile. CDC contains two device contexts, m_hDC and m_hAttribDC, which, on creation of a CDC object, refer tothe same device. CDC directs all output GDI calls to m_hDC and most attribute GDI calls to m_hAttribDC. (An example of an attribute call is GetTextColor, while SetTextColor is an output call.)

  9. For example, the framework uses these two device contexts to implement a CMetaFileDC object that will send output to a metafile while reading attributes from a physical device. Print preview is implemented in the framework in a similar fashion. You can also use the two device contexts in a similar way in your application-specific code.

  10. 2 CClietDC 类 The CClientDC class is derived from CDC and takes care of calling the Windows functions GetDC at construction time andReleaseDC at destruction time. This means that the device context associated with a CClientDC object is the client area of a window. 3 CPaintDC 类 The CPaintDC class is a device-context class derived from CDC. It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint at destruction time. A CPaintDCobject can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function.

  11. 4 CWindowDC 类 The CWindowDC class is derived from CDC. It calls the Windows functions GetWindowDC at construction time and ReleaseDC at destruction time. This means that a CWindowDC object accesses the entire screen area of a CWnd (both client and nonclient areas). 5 CMetaFileDC 类 A Windows metafile contains a sequence of graphics device interface (GDI) commands that you can replay to create a desired image or text. To implement a Windows metafile, first create a CMetaFileDC object. Invoke the CMetaFileDC constructor, then call the Create member function, which creates a Windows metafile device context and attaches it to the CMetaFileDC object.

  12. Next send the CMetaFileDC object the sequence of CDC GDI commands that you intend for it to replay. Only those GDI commands that create output, such as MoveTo and LineTo, can be used. After you have sent the desired commands to the metafile, call the Close member function, which closes the metafile device contexts and returns a metafile handle. Then dispose of the CMetaFileDC object. CDC::PlayMetaFile can then use the metafile handleto play the metafile repeatedly. The metafile can also be manipulated by Windows functions such as CopyMetaFile, which copies a metafile to disk. When the metafile is no longer needed, delete it from memory with the DeleteMetaFile Windows function. You can also implement the CMetaFileDC object so that it can handle both output calls and attribute GDI calls such as GetTextExtent. Such a metafile is more flexible and can more easily reusegeneral GDI code, which often consists of a mix of output and attribute calls.

  13. The CMetaFileDC class inherits two device contexts, m_hDC and m_hAttribDC, from CDC. The m_hDC device context handles all CDC GDI output calls and the m_hAttribDC device context handles all CDC GDI attribute calls. Normally, these two device contexts refer to the same device. In the case of CMetaFileDC, the attribute DC is set to NULL by default. Create a second device context that points to the screen, a printer, or device other than a metafile, then call the SetAttribDC member function to associate the new device context with m_hAttribDC. GDI calls for information will now be directed to the new m_hAttribDC. Output GDI calls will go to m_hDC, which represents the metafile.

  14. CGdiObject CPen CBrush CFont CPallete CBitmap CRgn 6.1.2 基本绘图工具(图形设备接口对象类) ·MFC 类: CGdiObject 的派生类 ·作用: 提供所绘制图形所需要的属性:线宽、线型、填 充颜色、填充图案、字体等。 ·组成: 画笔、画刷、字体、位图、调色板等。 绘图工具的 MFC 类的派生层次如下: CObject

  15. CGdiObject 是一个抽象类,所以它不能直接定义对象,但可以 定义该类的指针,用于指向设备环境中选取的由它派生的各种 绘图工具类对象。 1 画笔类 CPen The CPen class encapsulates a Windows graphics device interface (GDI) pen. 2 刷子类 CBrush The CBrush class encapsulates a Windows graphics device interface (GDI) brush. To use a CBrush object, construct a CBrush object and pass it to any CDC member function that requires a brush. Brushes can be solid, hatched, or patterned.

  16. 3 字体类 CFont The CFont class encapsulates a Windows graphics device interface (GDI) font and provides member functions for manipulating the font. To use a CFont object, construct a CFont object and attach a Windows font to it with CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndiret, and then use the object’s member functions to manipulate the font. The CreatePointFont and CreatePointFontIndirect functions are often easier to use than CreateFont or CreateFontIndirect since they do the conversion for the height of the font from a point size to logical units automatically.

  17. 4 位图类 CBitmap 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 object’s member functions.

  18. 5 调色板类 CPalette The CPalette class encapsulates a Windows color palette. A palette provides an interfacebetweenan application and a color output device (such as a display device). The interface allows the application to take full advantage of the color capabilities of the output device without severely interfering with the colors displayed by other applications. Windows uses the application’slogical palette (a list of needed colors) and the system palette (which defines available colors) to determine the colors used. A CPalette object provides member functions for manipulating the palette referred to by the object. Construct a CPalette object and use its member functions to create the actual palette, a graphics device interface (GDI) object, and to manipulate its entries and other properties.

  19. 6. 区域类 CRgn The CRgn class encapsulates a Windows graphics device interface (GDI) region. A region is an elliptical or polygonalarea within a window. To use regions, you use the member functions of class CRgn with the clipping functions defined as members of class CDC.

  20. 6.2 获取和释放设备环境对象 设备环境本身与具体的物理设备无关,但它可以定向于以下 任何一类设备,实现该设备的操作: ·视频设备(Display)Supports drawing operations on a video display. ·打印设备(Printer)Supports drawing operations on a printer or plotter. ·内存设备(Memory)Supports drawing operations on a bitmap. ·设备的信息(Information)Supports the retrieval of device data.

  21. 6.2.1 获取和释放窗口客户区设备环境 获取:CDC *pDC = pWnd->GetDC(); 释放:pWnd->ReleaseDC( pDC ); 其中,pWnd 是 pDC 指向的设备环境所属的窗口对象指针。 6.2.2 获取和释放整个窗口设备环境 获取:CDC *pDC = pWnd->GetWindowDC(); 释放:pWnd->ReleaseDC( pDC ); 其中,pWnd 是 pDC指向设备环境所属的窗口对象指针。

  22. 6.2.3 获取整个屏幕的设备环境 获取:CDC *pDC = CDC::FromHandle( ::GetDC( NULL ) ); 释放:pDC->DeleteDC(); 注意:由于 DeleteDC 的作用是析构 FromHandle 创建的临时 CDC 对象,因此在调用 DeleteDC 之前必须释放已经被选入 Device Context中使用的 GDI Object。

  23. 6.2.4 创建窗口客户区设备环境对象 CClientDCdc( this ); 参数 this 必须指向一个窗口类对象,因为在 CClientDC 的构造函 数中调用了API 函数: ::GetDC( this->m_hWnd ); 获取窗口的客户区设备环境句柄,使之与 dc相关联。同时将 this->m_hWnd和所获取的设备环境句柄分别保存在 CClientDC的 HWND 属性成员 m_hWnd和 HDC 属性成员 m_hDC中。 dc析构时调用 API 函数: ::ReleaseDC( m_hWnd, m_hDC ); 自动释放由 ::GetDC 获取的设备环境。

  24. 6.2.5 构造窗口绘图设备环境对象(响应 WM_PAINT) CPaintDCdc( this ); 参数 this 必须指向一个窗口类对象,因为在 CPaintDC 的构造函 数中自动调用了窗口类对象的成员函数 BeginPaint: this->BeginPain( &m_ps ); 从而获取了指向窗口的绘图设备环境的指针,使之与 dc 相关联。 其中参数 m_ps是 CPaintDC 类的 PAINTSTRUCT 属性成员,用于 包含应用程序的绘图信息。BeginPain 的调用还将 this 所指向的 窗口的句柄保存在 CPaintDC 类的 HWND 属性成员 m_hWnd中。 dc析构时自动调用窗口类对象的成员函数 EndPaint: FromHandle( m_hWnd )->EndPaint ( & m_ps ); 从而使由 BeginPaint 获取的绘图设备环境结束使用并撤消。

  25. 6.2.6 构造整个窗口的设备环境对象 CWindowDCdc( this ); 参数this 必须指向一个窗口类对象,因为在 CWindowDC的构造 函数中自动调用了 API函数: ::GetWindowDC( this->m_hWnd ); 从而获取整个窗口的设备环境,使之与 dc 相关联。同时将 this-> m_hWnd和所获取的设备环境句柄分别保存在 CWindowDC 的属 性成员 m_hWnd和 m_hDC中。 dc 析构时自动调用 API 函数: ::ReleaseDC( m_hWnd, m_hDC ); 自动释放由 ::GetWindowDC 获取的设备环境。

  26. 6.3 创建绘图工具对象 6.3.1 创建 CPen 对象 1 直接法 ·被调用的 CPen 类构造函数的原型: CPen( int nPenStyle, int nWidth, COLORREF crColor ); throw( CResourceException ); 参数: 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.

  27. PS_DASHDOT    Creates a pen with alternating 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. 更多的风格查询 MSDN 中的有关内容。 nWidth画笔(线)宽度。 crColor画笔(线)颜色,用宏RGB表示。 ·创建方法: CPenMypen( PS_SOLID, 2, RGB( 255, 0,0 ) ); 直接创建画笔的另一个函数原型为: CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL ); throw( CResourceException );

  28. 2二步法 ⑴ 根据参数直接创建 ·被调用的 CPen 的缺省构造函数和画笔创建成员函数原型: CPen( ); BOOL CreatePen( int nPenStyle, int nWidth, COLORREF crColor ); 参数:与前面带参数的构造函数中传递的参数相同。 BOOL CreatePenIndirect( LPLOGPEN lpLogPen ); 参数: lpLogPen指向一个描述逻辑画笔的结构 LOGPEN,定义如下: typedef struct tagLOGPEN {  /* lgpn */ UINT     lopnStyle; POINT    lopnWidth; COLORREF lopnColor; } LOGPEN; 显然结构中的成员含义顺序为画笔(线)的风格、宽度和颜色。

  29. ·创建方法: CPen pen; pen.CreatePen( PS_SOLID, 2, RGB( 255, 0, 0 )); 或 CPen pen; LOGPEN logPen; logPen.lopnStyle = PS_SOLID; logPen.lopnWidth = 2; logPen.lopnColor = RGB( 255, 0, 0 ); pen.CreatePenIndirect( &logPen );

  30. 3 从 Windows 画笔句柄(HPEN)创建一个 CPen 对象 ·被调用的 CPen 的成员函数的原型: static CPen* PASCAL FromHandle( HPEN hPen ); 参数: hPen Windows画笔句柄。 注意,如果未使参数指定的画笔句柄与一个用户创建的 CPen 对象相关联,则 FromHandle 返回的指针指向一个临时创建的 CPen 对象,则这个临时创建的 CPen 对象在下一个空闲时间 到来时会被自动撤消,换句话说,这个临时创建的 CPen 对象 只在一个 Windows 消息被处理期间有效。

  31. 4 在绘图场境 DC 中选用库存画笔 ·被调用的 CDC 的成员函数 SelectStockObject 的原型: virtual CGdiObject* SelectStockObject( int nIndex ); 参数: nIndex库存 GDI 对象的索引。可以选择的库存 GDI 对象如下: BLACK_BRUSH    Black brush. DKGRAY_BRUSH    Dark gray brush. GRAY_BRUSH    Gray brush. HOLLOW_BRUSH    Hollow brush. LTGRAY_BRUSH   Light gray brush. NULL_BRUSH    Null brush. WHITE_BRUSH    White brush. BLACK_PEN    Black pen.

  32. NULL_PEN    Null pen. WHITE_PEN    White pen. ANSI_FIXED_FONT    ANSI fixed system font. ANSI_VAR_FONT    ANSI variable system font. DEVICE_DEFAULT_FONT   Device-dependent font. OEM_FIXED_FONT    OEM-dependent fixed font. 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.

  33. SYSTEM_FIXED_FONT The fixed-width system font 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. ·选用方法: … pDC->SelectStockObject(BLACK_PEN);

  34. 6.3.2 创建 CBrush 对象 1 直接法 ⑴ 创建实心刷子 ·被调用的 CBrush 类构造函数的原型: CBrush( COLORREF crColor ); throw( CResourceException ); 参数: crColor所创建刷子的颜色。 ·创建实例: CBrush br ( RGB( 255, 0, 0 ));

  35. ⑵ 创建带阴影的刷子 ·被调用的 CBrush 类构造函数的原型: CBrush( int nIndex, COLORREF crColor ); throw( CResourceException ); 参数: nIndex阴影线模式的索引值。可使用的阴影线模式如下: HS_BDIAGONAL    Downward hatch (left to right) at 45 degrees HS_CROSS    Horizontal and vertical crosshatch HS_DIAGCROSS    Crosshatch at 45 degrees HS_FDIAGONAL    Upward hatch (left to right) at 45 degrees HS_HORIZONTAL    Horizontal hatch HS_VERTICAL    Vertical hatch

  36. ·创建实例: CBrush br( HS_DIAGCROSS, RGB( 255, 0, 0 )); ⑶ 创建位图(自定义图案)刷子 ·被调用的 CBrush类构造函数的原型: CBrush( CBitmap* pBitmap ); throw( CResourceException ); 参数: pBitmap指向一个 8X8 的位图对象。祥见 MSDN 中的相关信息。 ·创建实例: CBitmap bmp; bmp.LoadBitmap( IDB_MYBITMAP ); CBrush br(&bmp);

  37. 2 二步法 ⑴ 创建实心刷子 ·被调用的 CBrush类成员函数的原型: BOOL CreateSolidBrush( COLORREF crColor ); ·创建方法: CBrush br; br.CreateSolidBrush( RGB( 255, 0, 0 ));

  38. ⑵ 创建带阴影的刷子 ·被调用的 CBrush类成员函数的原型: BOOL CreateHatchBrush( int nIndex, COLORREF crColor ); 参数: nIndex阴影线模式的索引值。 ·创建方法: CBrush br; br.CreateHatchBrush( HS_BDIAGONAL, RGB( 255, 0, 0 ));

  39. ⑶ 创建位图(自定义图案)刷子 ·被调用的 CBrush类成员函数的原型: BOOL CreatePatternBrush( CBitmap* pBitmap ); 参数: pBitmap指向一个 8X8 的位图对象。祥见 MSDN 中的相关信息。 ·创建方法: CBitmap bmp; // 定义一个位图对象 bmp.LoadBitmap( IDB_MYBITMAP ); // 装载位图资源与位图对象关联 CBrush br; // 定义一个画刷对象 br.CreatePaletteBrush( &bmp ); // 创建位图图案的画刷

  40. 3 从 Windows 刷子(句柄)创建一个 CBrush对象 ·被调用的 CBrush的成员函数的原型: static CBrush* PASCAL FromHandle( HBRUSH hBrush ); 参数: hBrush Windows刷子句柄。 注意,如果未使参数指定的刷子句柄与一个用户创建的 CBrush对象相关联,则 FromHandle返回的指针指向一个临时 创建的 CBrush对象,这个临时创建的 CBrush对象在下一次空 闲时间到来时会被自动撤消,换句话说,这个临时创建的 CBrush 对象只在一个 Windows 消息被处理期间有效。 4 在绘图场境 DC 中选用库存刷子 pDC->SelectStockObject( WHITE_BRUSH );

  41. 6.3.3 创建 CFont 对象 字体对象的创建不能采用直接法。 1 二步法 ⑴ 使用字体参数直接创建 ·被调用的 CFont 的成员函数 CreateFont 的原型:

  42. BOOL CreateFont ( int nHeight /* 字体高 */,int nWidth /*字体宽 */, int nEscapement, // 字与水平基线的夹角,单位为0.1度 int nOrientation,// 字旋转度,逆时针为正,单位为0.1度 int nWeight,// 字的粗细 BYTE bItalic,// 字是否为斜体 BYTE bUnderline,// 字是否有下划线 BYTE cStrikeOut,// 字是否有删除线 BYTE nCharSet,// 字符集 BYTE nOutPrecision,// 输出精度 BYTE nClipPrecision,// 裁剪精度 BYTE nQuality,// 逻辑字体与输出设备提供的实际精度 BYTE nPitchAndFamily,// 字体间距和字体族 LPCTSTR lpszFacename// 字体名 );

  43. ·创建方法: CFont Myfont; Myfont.CreateFont( 16 /*字高 */, 0 /*字宽 */, 0 /* 基线夹角 */, 0 /* 旋转度 */, FW_SEMIBOLD /* 字粗细 */, 0 /* 斜体标志 */, 0 /* 下划线标志 */, 0 /*删除线标志 */, ANSI_CHARSET, // 字符集 OUT_DEFAULT_PRECIS, // 输出精度 CLIP_DEFAULT_PRECIS, // 裁剪精度 DEFAULT_QUANLITY, // 实际精度 DEFAULT_PITCH|FF_DONTCARE, // 字体间距 “Courier New” // 字体名 );

  44. ⑵ 使用逻辑字体创建 ·被调用的 CFont 的成员函数 CreateFontIndirect 的原型: BOOL CreateFontIndirect( const LOGFONT* lpLogFont ); 参数: lpLogFont指向逻辑字体LOGFONT 结构变量。该结构如下: typedef struct tagLOGFONT { LONG lfHeight; // 字高 LONG lfWidth; // 字宽 LONG lfEscapement; // 基线夹角 LONG lfOrientation;// 旋转度 LONG lfWeight; // 字粗细 BYTE lfItalic; // 斜体标志 BYTE lfUnderline; // 下划线标志 BYTE lfStrikeOut; // 删除线标志

  45. BYTE lfCharSet; // 字符集 BYTE lfOutPrecision; // 输出精度 BYTE lfClipPrecision; // 裁剪精度 BYTE lfQuality; // 实际精度 BYTE lfPitchAndFamily; // 字体间距 TCHAR lfFaceName[LF_FACESIZE]; // 字体名 } LOGFONT; 显然结构成员的含义与函数 CreateFont 对应的参数相同。 ·创建方法: LOGFONT logFont; logFont.lfHeight = 16; logFont.lfWidth = 0; logFont.lfEscapement = 0;

  46. logFont.lfOrientation = 0; logFont.lfWeight = FW_SEMIBOLD; logFont.lfItalic = 0; logFont.lfUnderline = 0; logFont.lfStrikeOut = 0; logFont.lfCharSet = ANSI_CHARSET; logFont.lfOutPrecision = OUT_DEFAULT_PRECIS; logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; logFont.lfQuality = DEFAULT_QUALITY; logFont.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE; strcopy( logFont.lfFaceName, “Courier New” ); CFont font; font.CreateFontIndirect( &logFont );

  47. ⑶ 另外两种创建方法 ·被调用的 CFont 的成员函数 CreateFont 的原型: BOOL CreatePointFont( int nPointSize, LPCTSTR lpszFaceName, CDC* pDC = NULL ); BOOL CreatePointFontIndirect( const LOGFONT* lpLogFont, CDC* pDC = NULL ); 2 从 Windows 字体(句柄)创建一个 CFont 对象 ·被调用的 CFont 的成员函数的原型: static CFont* PASCAL FromHandle( HFONT hFont ); 参数: hFont Windows 字体句柄。

  48. 注意,如果未使参数指定的字体句柄与一个用户创建的 CFont 对象相关联,则 FromHandle返回的指针指向一个临时创建的 CFont 对象,这个临时创建的 CFont 对象在下一次空闲时间到 来时会被自动撤消,换句话说,这个临时创建的 CFont对象只 在一个 Windows 消息被处理期间有效。 3 在绘图场境 DC 中选用库存字体 pDC->SelectStockObject( SYSTEM_FONT );

  49. 6.3.4 创建 CBitmap 类对象 1 二步法创建 CBitmap 类对象 第一步先创建一个 CBitmap对象;然后第二步调用 CBitmap 类的创建成员函数创建与 CBitmap 对象相关联的 Windows位图 Bitmap。 CBitmap 的创建成员函数有四个,原型如下: BOOL CreateBitmap( int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits ); BOOL CreateBitmapIndirect( LPBITMAP lpBitmap ); BOOL CreateCompatibleBitmap( CDC* pDC, int nWidth, int nHeight ); BOOL CreateDiscardableBitmap( CDC* pDC, int nWidth, int nHeight ); 这些函数的参数含义和调用方法参见 MSDN 的有关内容。

  50. 2 从 Windows 位图(句柄)创建一个 CBitmap对象 ·被调用的 CBitmap 的成员函数的原型: static CBitmap* PASCAL FromHandle( HBITMAP hBitmap ); 参数: hBitmap Windows 位图句柄。 注意,如果参数指定的位图句柄未与一个用户创建的 CBitmap 对象相关联,则 FromHandle返回的指针指向一个临时创建的 CBitmap对象,这个临时创建的 CBitmap 对象在下一次空闲时 间到来时会被自动撤消,换句话说,这个临时创建的 CBitmap 对象只在一个 Windows 消息被处理期间有效。

More Related