How are GDI objects selected and destroyed by SelectObject function(SelectObject 函数如何选择和销毁 GDI 对象)
问题描述
由于我是 Visual C++ 的新手,这可能是一个与选择 GDI 对象相关的非常基本的问题.
As I am new to Visual C++, this might be a very basic question related to selecting a GDI object.
以下代码段绘制了一个没有边框的浅灰色圆圈.
The following code snippet draws a light grey circle with no border.
cPen pen(PS_NULL, 0, (RGB(0,0,0)));
dc.SelectObject(& pen);
CBrush brush (RGB (192,192,192));
dc.SelectObject (&brush);
dc.Ellipse(0,0, 100,100);
我从代码片段中了解到的是,首先创建了一个 Pen 对象,它是一个 NULL Pen,它会使边框消失,然后画笔创建一个灰色圆圈,但是 dc代码>如果它已经在使用画笔,请使用笔?这有点令人困惑.
All I understand from the code snippet is first an Object of Pen is created, and its a NULL Pen which would make the border disappear, the brush then creates a Circle of grey color, but how does dc
use pen if it is already using brush? this is a bit confusing.
两次使用 dc.SelectObject()
有什么帮助?如果使用实心画笔对象创建一个灰色的圆圈,创建笔对象有什么帮助,如果它在创建画笔对象时被销毁?这东西到底是怎么工作的?
How does using dc.SelectObject()
twice help? If the solid brush object is used to create a circle with grey color, how does creating pen object help, if it is anyway destroyed as brush object is created? how exactly does this thing work?
推荐答案
SelectObject函数用于选择五种不同类型的对象进入DC
SelectObject function is used to select five different types of objects into DC
- 笔
- 画笔
- 字体
- 位图和
- 地区
文档指出新选择的对象替换之前的同类型对象
.所以这意味着你可以毫无问题地选择笔和画笔,但你不能选择笔两次.
The documentation states that
The newly selected object replaces the previous object of the same type
. So it means you can select pen and brush without any problem but you cant select pen twice.
此外,为了避免资源泄漏,您需要选择之前选择的旧笔/画笔
And moreover to avoid resource leak you need to select the old pen/brush whatever you have selected earlier
CPen pen(PS_NULL, 0, (RGB(0,0,0)));
CPen *oldPen = dc.SelectObject(& pen);
CBrush brush (RGB (192,192,192));
CBrush *oldBrush = dc.SelectObject (&brush);
dc.Ellipse(0,0, 100,100);
dc.SelectObject(oldPen);
dc.SelectObject(oldBrush);
这篇关于SelectObject 函数如何选择和销毁 GDI 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SelectObject 函数如何选择和销毁 GDI 对象


基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01