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

查看:282
本文介绍了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,它将使边框消失,然后笔刷创建了一个灰色的Circle,但是dc如何使用pen它已经在使用画笔了吗?这有点令人困惑.

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 newly selected object replaces the previous object of the same type.因此,这意味着您可以毫无问题地选择笔和画笔,但不能两次选择笔.

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天全站免登陆