您的位置:首页 > 其它

一些MFC绘图相关

2010-11-08 12:31 92 查看
最近项目需要用到MFC绘图,生成一个动态绘图的界面。所以查询了下。

MFC绘图,首先要用到一个CDC类,所有的绘图操作都封装在了这个类中,下面是MSDN中对此类的描述。

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 with regions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working with fonts, using printer escapes, scrolling, and playing metafiles.

To use a CDC object, construct it, and then call its member functions that parallel Windows functions that use device contexts.

里面提到了个名词—— device-context ,翻译过来叫做设备上下文,MSDN对它的描述是这样的。

A device context is a Windows data structure containing information about the drawing attributes of a device such as a display or a printer. All drawing calls are made through a device-context object, which encapsulates the Windows APIs for drawing lines, shapes, and text. Device contexts allow device-independent drawing in Windows. Device contexts can be used to draw to the screen, to the printer, or to a metafile.

DC包含四种,分别是一下四个,各有所用。

CPaintDC objects encapsulate the common idiom of Windows, calling the BeginPaint function, then drawing in the device context, then calling the EndPaint function. The CPaintDC constructor calls BeginPaint for you, and the destructor calls EndPaint. The simplified process is to create the CDC object, draw, and destroy the CDC object. In the framework, much of even this process is automated. In particular, your
OnDraw
function is passed a CPaintDC already prepared (via OnPrepareDC), and you simply draw into it. It is destroyed by the framework and the underlying device context is released to Windows upon return from the call to your
OnDraw
function.

CClientDC objects encapsulate working with a device context that represents only the client area of a window. The CClientDC constructor calls the GetDC function, and the destructor calls the ReleaseDC function.

CWindowDC objects encapsulate a device context that represents the whole window, including its frame.

CMetaFileDC objects encapsulate drawing into a Windows metafile. In contrast to the CPaintDC passed to
OnDraw
, you must in this case call OnPrepareDC yourself.

另外在画图时往往要用到个叫做内存设备上下文,——

内存设备上下文环境是仅在内存中存在的设备上下文环境,当内存设备上下文环境被创建时,它的显示界面是标准的一个单色像素宽和一个单色像素高,在一个应用程序可以使用内存设备上下文环境进行绘图操作之前,它必须选择一个高和宽都正确的位图到设备上下文环境中,这可以通过使用CreateCompatibleBitmap函数指定高、宽和色彩组合以满足函数调用的需要。

要用到CreateCompatibleDC()函数,

该函数创建一个与指定设备兼容的内存设备上下文环境(DC)。通过GetDc()获取的HDC直接与相关设备沟通,而本函数创建的DC,则是与内存中的一个表面相关联。

初学者可能对这两东西还是如同雾里看花一样,看得不真切,看看老程序员怎么说:

你首先明白DC的含义,Windows不允许程序员直接访问硬件,它对屏幕的操作是通过环境设备,也就是DC来完成的。屏幕上的没一个窗口
都对应一个DC,可以把DC想象成一个视频缓冲区,对这这个缓冲区的操作,会表现在这个缓冲区对应的屏幕窗口上。


在窗口的DC之外,可以建立自己的DC,就是说它不对应窗口,这个方法就是CreateCompatibleDC,这个DC就是一个内存缓冲区,通过
这个DC你可以把和它兼容的窗口DC保存到这个DC中,就是说你可以通过它在不同的DC之间拷贝数据。例如:你先在这个DC中建立好数据,
然后在拷贝到窗口的DC就是完成了这个窗口的刷新。
下面是一个代码段:不长

hDeskTop = GetDesktopWindow();

hDeskTopDC = GetDC(hDeskTop);//桌面窗口DC

hMemDC = CreateCompatibleDC(hDeskTopDC);//这建立的就是与桌面窗口兼容的DC。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: