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++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01