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函数去删除它们,把占用的内存释放回去给系统。