SelcetObjcet()函数的用法——hOldPen的存在

 

This function selects an object into a specified device context. The new object replaces the previous object of the same type.

HGDIOBJ SelectObject(
  HDC hdc, 
  HGDIOBJ hgdiobj
); 
Parameters
hdc
[in] Handle to the device context.
hgdiobj
[in] Handle to the object to be selected.

The specified object must have been created by using one of the following functions.

 

 

Return Values

If the selected object is not a region, the handle of the object being replaced indicates success.

If the selected object is a region, one of the following values indicates success.

 

Remarks

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.

An application cannot select a bitmap into more than one device context at a time. 

 

 

两点说明:

这个函数的功能:将一个新的绘画对象选进设备中。 

 

返回值:是DC中原来的、同类型的(绘画)对象。这一点很重要,通常需要新建一个(以PEN为例)hOldPen用于保存原来的PEN,在用新的PEN作为完图,再将DC中的PEN恢复为原来的状态——即SelectObject(hDC,hOldPen).

 

示例:(这是找到的一个WIN32编程示范,作用一样,可以说明问题)

 

 

程序效果如图:运行效果

 

 

另外:

 

其中的DelteObject()的使用非常重要,不然会造成“内在泄露”。相关的一段文字叙述如下:

 

Windows显示设备的属性,共有下面几种:位图、画刷、字体、画笔、区域。如果要设置它们到当前设备里,就需要使用SelectObject函数,比如上面介绍的字体设置,就会用到这个函数。

当你创建一个位图时,这时Windows就会在内存里分配一块内存空间,用来保存位图的数据。

当你创建字体时,也会分配一块内存空间保存字体。

如果程序只是分配,而不去删除,就会造成内存使用越来越多,最后导到Windows这幢大楼倒下来。

如果你忘记删除它,就造成了内存泄漏

因此,当你创建显示设备资源时,一定要记得删除它们啊,否则运行你的程序越长,就导致系统不稳定。记得使用DeleteObject函数去删除它们,把占用的内存释放回去给系统。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值