SelectObject 函数如何选择和销毁 GDI 对象 [英] How are GDI objects selected and destroyed by SelectObject function

查看:12
本文介绍了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

  1. 画笔
  2. 字体
  3. 位图和
  4. 地区

文档指出新选择的对象替换之前的同类型对象.所以这意味着你可以毫无问题地选择笔和画笔,但你不能选择笔两次.

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 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆