首页 > 代码库 > SelectObject()函数

SelectObject()函数

SelectObject 
把一个对象(位图、画笔、画刷等)选入指定的设备描述表。新的对象代替同一类型的老对象。

HGDIOBJ SelectObject(
  HDC hdc,         // handle to DC
  HGDIOBJ hgdiobj  // handle to object
);

 

参数

  1. hdc 设备描述表句柄(要载入的设备描述表句柄)
  2. hgdiobj 选择要载入的对象的句柄

但该对象的句柄必须使用以下函数创建才有效:

Bitmap(位图)          CreateBitmap, CreateBitmapIndirect, 
                            CreateCompatibleBitmap, CreateDIBitmap, 
                            CreateDIBSection 
Brush(画刷)           CreateBrushIndirect, eateDIBPatternBrush,  
                            CreateDIBPatternBrushPt, CreateHatchBrush,  
                            CreatePatternBrush, CreateSolidBrush 
Font(字体)              CreateFont, CreateFontIndirect


Pen(画笔)               CreatePen, CreatePenIndirect


Region(区域)             CombineRgn, CreateEllipticRgn,  
                               CreateEllipticRgnIndirect,  
                               CreatePolygonRgn, CreateRectRgn,  
                               CreateRectRgnIndirect 

返回值:如果选择对象不是区域并且函数执行成功,那么返回值是被取代的对象的句柄;如果选择对象是区域并且函数执行成功,返回如下一值;

 =======================================================

1、看MSDN中的解释:

Selects an object into the device context(选择一个对象到设备上下文中去).

[cpp] view plaincopy
  1. CPen* SelectObject(  
  2.    CPen* pPen   
  3. );  
  4. CBrush* SelectObject(  
  5.    CBrush* pBrush   
  6. );  
  7. virtual CFont* SelectObject(  
  8.    CFont* pFont   
  9. );  
  10. CBitmap* SelectObject(  
  11.    CBitmap* pBitmap   
  12. );  
  13. int SelectObject(  
  14.    CRgn* pRgn   
  15. );  
  16. CGdiObject* SelectObject(  
  17.    CGdiObject* pObject  
  18. );  

 

参数:

pPen

A pointer to a CPen object to be selected.

pBrush

A pointer to a CBrush object to be selected.

pFont

A pointer to a CFont object to be selected.

pBitmap

A pointer to a CBitmap object to be selected.

pRgn

A pointer to a CRgn object to be selected.

pObject

A pointer to a CGdiObject object to be selected.

 

返回值:

A pointer to the object being replaced. This is a pointer to an object of one of the classes derived fromCGdiObject, such as CPen, depending on which version of the function is used.

 

返回一个指向被代替的对象的指针。这个真正指向的对象是重载于CGdiObject类的一种,比如CPen,具体以来与所使用的函数版本。

 

2、测试代码

[cpp] view plaincopy
  1. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)  
  2. {  
  3.     // TODO: Add your message handler code here and/or call default  
  4.     m_ptBegin=point;//m_ptBegin是类的成员,类型为CPoint  
  5.   
  6.     CView::OnLButtonDown(nFlags, point);  
  7. }  
  8.   
  9. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)  
  10. {  
  11.     // TODO: Add your message handler code here and/or call default  
  12.     CPen pen(PS_SOLID, 1, RGB(255, 0, 0));  
  13.     CClientDC dc(this);  
  14.     CPen* pOldPen=dc.SelectObject(&pen);//保存原始的CPen,即黑色的CPen  
  15.     dc.MoveTo(m_ptBegin);  
  16.     dc.LineTo(point);  
  17.     dc.SelectObject(pOldPen);//恢复原始的CPen  
  18.     dc.MoveTo(m_ptBegin);  
  19.     dc.LineTo(point.x+100, point.y);  
  20.     CView::OnLButtonUp(nFlags, point);  
  21. }  

 

3、运行结果: