教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

VC++技术内幕(windows编程篇)(4)

来源:网络收集 时间:2026-07-12
导读: //Constructs a CClientDC object that accesses the client area of the CWnd pointed to by pWnd. The constructor calls the Windows function GetDC. CWindowDC( CWnd* pWnd ); //Constructs a CWindowDC objec

//Constructs a CClientDC object that accesses the client area of the CWnd pointed to by pWnd. The constructor calls the Windows function GetDC.

CWindowDC( CWnd* pWnd );

//Constructs a CWindowDC object that accesses the entire screen area (both client and nonclient) of the CWnd object pointed to by pWnd. The constructor calls the Windows function GetWindowDC.

如果构造CClientDC对象,则设备环境的映射区域限于客户区域,不能在客户区域外绘图。原点(0,0)在客户区左上角。

《 如果创建CWindowDC对象,则设备环境的映射区域为整个窗口(包括标题栏、状态栏、窗口边框等)。原点(0,0)在整个窗口的左上角。》

注意:

1)视图窗口没有非客户区域. 2)视图窗口覆盖在框架窗口之上。

3)在《》中的内容,是我根据测试所理解的。翻译原文中映射区域是整个显示屏幕,原点(0,0)在整个屏幕的左上角。这显然是不对的。

5,在创建了一个CDC对象后,一定要注意在完成任务后将其删除。(如果用CClientDC 或 CWindowDC来在堆栈中构造一个DC对象,则不需要我们显式的删除它,CClientDC或CWindowDC对象会在它的生命周期结束的时候自动调用析构函数来完成删除工作)

例如:

CDC *pDC=GetDC(); ...

RealeaseDC(pDC); 说明:

CDC* GetDC( );//CWnd::GetDC 获得与当前窗口相关联的CDC对象指针(映射窗口客户区域)

int ReleaseDC( CDC* pDC );//CWnd::ReleaseDC 释放与当前窗口相关联的设备环境

注意:

在MFC程序中,千万不能自己添加代码删除作为参数以指针形式传递给OnDraw(CDC* pDC)函数的CDC对象,应用程序会自动控制它的删除。

6,当利用CDC对象绘图的时候,所绘制的图形都要依赖于设备环境的状态:如画笔、画刷、字体等GDI绘图对象 和 当前映射模式的选择等。

可以使用CDC成员函数了设置所需要的饿设备环境状态。如:SelectObject()函数可将GDI对象随时选入设备环境中。

7,CPaintDC类:当需要重写OnPaint函数的时候,就需要使用CPaintDC类。默认的OnPaint函数会使用已经设置好的设备环境来调用OnDraw函数

注意:CPaintDC类的构造函数和析构函数所完成的工作都是针对显示用的。 例:

void CMyView::OnPaint() {

CPaintDC dc(this); OnPrepareDC(&dc);

dc.TextOut(0,0,\OnDraw(&dc); } 说明:

1)CPaintDC( CWnd* pWnd );

//CPaintDC::CPaintDC Constructs a CPaintDC object, prepares the application window for painting, and stores the PAINTSTRUCT structure in the m_ps member variable.

2)CPaintDC类的构造函数自动调用BeginPaint,而它的析构函数会自动调用EndPaint.

3)CDC* BeginPaint( LPPAINTSTRUCT lpPaint );

//CWnd::BeginPaint Prepares CWnd for painting and fills a PAINTSTRUCT data structure with information about the painting.

void EndPaint( LPPAINTSTRUCT lpPaint );

//CWnd::EndPaint Marks the end of painting in the given window. The EndPaint member function is required for each call to the BeginPaint member function, but only after painting is complete.

问题:

1)在View类中一旦定义了OnPaint()函数,则WM_PAINT消息由OnPaint函数来响应。OnDraw函数不再被调用。原因?

8,GDI对象:

Class Windows handle type CPen HPEN CBrush HBRUSH CFont HFONT CBitmap HBITMAP CPalette HPALETTE CRgn HRGN 说明:

Each graphic-object class in the class library has a constructor that allows you to create graphic objects of that class, which you must then initialize with the appropriate create function, such as CreatePen.

Each graphic-object class in the class library has a cast operator that will cast an MFC object to the associated Windows handle. The resulting handle is valid until the associated object detaches it. Use the object‘s Detach member function to detach the handle.

9,CGdiObject类有一个虚析构函数,在其派生类的析构函数将与C++对象相关联的WINDOWS GDI对象删除掉。如果构造了一个CGdiObject派生类对象,则在退出程序之前,必须将其删除掉。

利用CDC类的SelectObject成员函数把自己的GDI对象选进DC的同时,保存原来的GDI对象,当任务完成后,恢复原来的GDI对象,这样就可以将自己的GDI对象的删除掉。

如:

GDIStyle *poldGdiObject=pDC->SelectObject(&newGdiObject); ...//完成任务

pDC->SelectObject(poldGdiObject);

10,Windows对任何企图删除库存GDI对象的行为都不予理会。可以利用SelectObject函数把库存GDI对象选入,从而删除当前GDI对象。

原因:

This function SelectObject may return a pointer to a temporary object. This temporary object is only valid during the processing of one Windows message.

如:

pDC->SelectObject(&newGdiObject); ...

pDC->SelectStockObject(StockCGdiObject);//如:BLACK_BRUSH

11,对于显示设备环境,在消息处理函数内部所进行GDI选择在函数退出后不再有效,因此在进入其他处理函数的时候,每次都必须重新设置设备环境。 Windows句柄是唯一能够持久存在的GDI标识。可以使用GDI对象指针调用GetSafeHandle函数可以获得它Windows句柄,再利用GDI对象类的FromHandle函数将句柄转化为对应的GDI对象。

如:

CFont *pOldFont=pDC->SelectObject(&newFont);

m_hOldFont=(HFONT)pOldFont->GetSafeHandle();//m_hOldFont为数据成员,保存先前字体对象句柄

...

pDC->SelectObject(CFont::FromHandle(m_hOldFont));//可在其他函数中,恢复m_hOldFont句柄对应的字体对象。

说明:

1)static CFont* PASCAL FromHandle( HFONT hFont );

//CFont::FromHandle Returns a pointer to a CFont object when given an HFONT handle to a Windows GDI font object. If a CFont object is not already attached to the handle, a temporary CFont object is created and attached.

2)CGdiObject::GetSafeHandle();

//Return A HANDLE to the attached Windows GDI object; otherwise NULL if no object is attached.

12,每一种Windows颜色都是 通过8位的红(R)、绿(G)、蓝(B)的值祝贺来表示的。 …… 此处隐藏:2212字,全部文档内容请下载后查看。喜欢就下载吧 ……

VC++技术内幕(windows编程篇)(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/592946.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)