GDI的一些很方便的代码

自己有点孤陋寡闻!!!!!!!!!!!!
改变画笔颜色和画刷颜色很方便
ms-help://MS.MSDNQTR.v90.en/gdi/devcons_34qa.htm


Setting the Pen or Brush Color


The following example shows how an application can change the DC pen color by using theGetStockObject function or SetDCPenColor and the SetDCBrushColor functions.


HGDIOBJ original = NULL;


//Save original object.
original = SelectObject(hdc,GetStockObject(DC_PEN));
//Change the DC pen color
SetDCPenColor(hdc,RGB(0x00,0xff,0x00));
Rectangle(0,0,20,20);
SetDCPenColor(hdc,RGB(0x00,0x00,0xff));
Rectangle(0,0,20,20);


// The brush color can be changed in a similar manner. SetDCPenColor 
// and SetDCBrushColor can be used interchangeably with GetStockObject 
// to change the current color.  
SelectObject(hDC,GetStockObject(DC_BRUSH));
SetDCBrushColor(hDC,RGB(0x00,0x00,0x00)); 


// Provides the same flexibility as:
SelectObject(hDC,GetStockObject(BLACK_BRUSH));

//Restore original object.
SelectObject(hDc,original);


// It is not necessary to call DeleteObject to delete stock objects.




我自己的习惯是, 在需要什么颜色的画笔, 就创建什么颜色的画笔, 然后选择那个画笔, 用完后恢复DC的画笔和销毁创建的画笔.
如果一段代码中要频繁改变画笔的颜色, 上面的代码无疑是很好的, 例如有1000中颜色, 难道创建1000中颜色的画笔吗?
上面的代码可以不需要创建, 画刷的也类似.

但要注意的是SetDCPenColor(hdc,RGB(0x00,0xff,0x00));之前必须调用original = SelectObject(hdc,GetStockObject(DC_PEN));

你可能感兴趣的:(GDI的一些很方便的代码)