简体   繁体   English

初学者在MFC C ++中,为什么设备上下文需要创建一个旧的Font / Bitmap / etc指针然后在结尾处选择Object()呢?

[英]Beginner In MFC C++, why does the device context need to create an old Font/Bitmap/etc pointer and then selectObject() it at the end?

Case in point: 例证:

void CMainWindow::OnPaint ()
{
    CRect rect;
    GetClientRect (&rect);

    CPaintDC dc (this);
    dc.SetViewportOrg (rect.Width () / 2, rect.Height () / 2);
    dc.SetBkMode (TRANSPARENT);

    for (int i=0; i<3600; i+=150) {
        LOGFONT lf;
        ::ZeroMemory (&lf, sizeof (lf));
        lf.lfHeight = 160;
        lf.lfWeight = FW_BOLD;
        lf.lfEscapement = i;
        lf.lfOrientation = i;
        ::lstrcpy (lf.lfFaceName, _T ("Arial"));

        CFont font;
        font.CreatePointFontIndirect (&lf);

        CFont* pOldFont = dc.SelectObject (&font);
        dc.TextOut (0, 0, CString (_T ("          Hello, MFC")));

        //WHY THIS LINE?
        dc.SelectObject (pOldFont);
    }
}

The code prints " Hello, MFC" in a circle around the origin (which is moved to the center of the window). 代码在原点周围的圆圈中打印“Hello,MFC”(移动到窗口的中心)。

产量

Why is that CFont pointer created and then the dc selects it as the font? 为什么创建CFont指针然后dc选择它作为字体? Is that just good programming practice or does this app actually need it? 这只是很好的编程习惯还是这个应用确实需要它?

I've seen similar code on the web doing this with Bitmaps and other device context objects. 我在网上看到类似的代码用Bitmaps和其他设备上下文对象做这件事。 What's the purpose? 目的是什么?

When I remove that last line of the code, nothing changes. 当我删除代码的最后一行时,没有任何变化。 Thanks in advance for the help. 先谢谢您的帮助。

Device Context : 设备背景

A device context is a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output. 设备上下文是一种结构,它定义一组图形对象及其相关属性,以及影响输出的图形模式。 The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations. 图形对象包括用于画线的笔,用于绘画和填充的画笔,用于复制或滚动屏幕部分的位图,用于定义可用颜色集的调色板,用于剪裁和其他操作的区域以及用于绘画和绘画操作。

At any time, there is exactly one graphic object selected into a device context. 在任何时候,只有一个图形对象被选择到设备上下文中。 Since the system stores a set of default objects into a device context when it is created, an application must retain that state, when the device context is handed back to the system for cleanup. 由于系统在创建时将一组默认对象存储到设备上下文中,因此当设备上下文被交还给系统进行清理时,应用程序必须保留该状态。 That is what 那是什么

dc.SelectObject (pOldFont);

is responsible for. 为...负责。

This requirement is documented under SelectObject : SelectObject下记录了此要求:

This function returns the previously selected object of the specified type. 此函数返回先前选择的指定类型的对象。 An application should always replace a new object with the original, default object after it has finished drawing with the new object. 在使用新对象完成绘制后,应用程序应始终使用原始的默认对象替换新对象。


Note: This is not related to MFC, but rather the Windows GDI. 注意:这与MFC无关,而是与Windows GDI有关。 MFC merely implements an automatic resource management wrapper. MFC仅实现自动资源管理包装器。 State management still requires explicit code. 状态管理仍然需要明确的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM