2012-10-17 27 views

回答

2

當您要求設備上下文畫一個矩形時,它將使用當前的Pen

您可以使用SelectObject()方法設置當前的Pen,Brush,剪輯區域或任何其他內容。你基本上是說「從現在開始使用這支筆」。

SelectObject()也返回它以前使用的項目,以便它不會丟失並泄漏內存,並且如果需要可以稍後再放回。這就是爲什麼penOld被保存到代碼中的一個變量。稍後可能會再次選擇它。

CPen pen;         //declare a new Pen object 
pen.CreatePen(PS_DASH, 20, RGB(0, 0, 0)); //Create the GDI Pen, dashed, 20 pixels wide, black. 
CPen* penOld = dc.SelectObject(&pen);  //Tell the DC to use this pen from now on. 

dc.Rectangle(rect);      //Draw a rectangle (using the current pen) 
dc.FillRect(rect, &brush);     //Fill a rectangle (using the current brush) 
0

它創建一支筆,將其選中到DC中,然後指示DC使用筆(寬20個像素)在指定座標處繪製一個矩形。然後用刷子填充矩形的內部。

相關問題