SelectObject
把一个对象(位图、画笔、画刷等)选入指定的设备描写叙述表。新的对象取代同一类型的老对象。 HGDIOBJ SelectObject( HDC hdc, // handle to DC HGDIOBJ hgdiobj // handle to object ); |
參数
- hdc 设备描写叙述表句柄(要加载的设备描写叙述表句柄)
- 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(选择一个对象到设备上下文中去).
- CPen* SelectObject(
- CPen* pPen
- );
- CBrush* SelectObject(
- CBrush* pBrush
- );
- virtual CFont* SelectObject(
- CFont* pFont
- );
- CBitmap* SelectObject(
- CBitmap* pBitmap
- );
- int SelectObject(
- CRgn* pRgn
- );
- CGdiObject* SelectObject(
- CGdiObject* pObject
- );
參数:
pPenA pointer to a CPen object to be selected.
pBrushA pointer to a CBrush object to be selected.
pFontA pointer to a CFont object to be selected.
pBitmapA pointer to a CBitmap object to be selected.
pRgnA pointer to a CRgn object to be selected.
pObjectA 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、測试代码
- void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- m_ptBegin=point;//m_ptBegin是类的成员。类型为CPoint
- CView::OnLButtonDown(nFlags, point);
- }
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
- CClientDC dc(this);
- CPen* pOldPen=dc.SelectObject(&pen);//保存原始的CPen。即黑色的CPen
- dc.MoveTo(m_ptBegin);
- dc.LineTo(point);
- dc.SelectObject(pOldPen);//恢复原始的CPen
- dc.MoveTo(m_ptBegin);
- dc.LineTo(point.x+100, point.y);
- CView::OnLButtonUp(nFlags, point);
- }
3、执行结果: