CPen繪製線條寬度大於1 的虛線

1、示例環境:VS2010,MFC對話框工程,主要是對話框的OnPaint函數中


2、首先,一般大多數人比較熟悉的CPen的用法如下:
		CPaintDC dc(this);

		CPen pen1(PS_SOLID, 1, RGB(0, 0, 0));
		CPen* pOldPen = dc.SelectObject(&pen1);

		dc.MoveTo(50, 50);
		dc.LineTo(500, 50);

		dc.SelectObject(pOldPen);



3、會發現想要繪製粗細大於1的虛線(或是其它非PS_SOLID風格線條)時,繪製出來的都是實線:
<pre name="code" class="cpp">		CPaintDC dc(this);

		CPen pen1(PS_DASH, 2, RGB(0, 0, 0));
		CPen* pOldPen = dc.SelectObject(&pen1);

		dc.MoveTo(50, 50);
		dc.LineTo(500, 50);

		pen1.DeleteObject();
		dc.SelectObject(pOldPen);




4、下面介紹另一種CPen對象的創建方法,以解決上述問題:

(1)對話框OnPaint函數中添加示例代碼:

		CPaintDC dc(this);

		LOGBRUSH logBrush;
		logBrush.lbStyle = BS_SOLID;
		logBrush.lbColor = RGB(0, 0, 0);
		CPen pen2(PS_DASH | PS_GEOMETRIC | PS_ENDCAP_ROUND, 2, &logBrush);
		CPen* pOldPen = dc.SelectObject(&pen2);

		dc.MoveTo(50, 50);
		dc.LineTo(500, 50);

		pen2.DeleteObject();
		dc.SelectObject(pOldPen);


(2)這樣粗細大於1的虛線就畫出來了:



5、下面是MSDN中的一段相關內容,英文好的話可以讀一讀:

CPen::CreatePen

This method initializes a pen with the specified style, width, and color. The pen can be subsequently selected as the current pen for any device context.

BOOL CreatePen (
int nPenStyle,
int nWidth,
COLORREF crColor ); 

Parameters

nPenStyle
Specifies the style for the pen. For a list of possible values, see the nPenStyle parameter in the CPen::CPen constructor.
nWidth
Specifies the width of the pen.
  • For the first version of CreatePen, if this value is zero, the width in device units is always 1 pixel, regardless of the mapping mode.
  • For the second version of CreatePen, if nPenStyle is PS_GEOMETRIC, the width is given in logical units. If nPenStyle is PS_COSMETIC, the width must be set to 1.
crColor
Contains an RGB color for the pen.

Return Value

Nonzero, or the handle of a logical pen, if successful; otherwise, it is zero.

Remarks

Pens that have a width greater than 1 pixel should always have either the PS_NULL, PS_SOLID, or PS_INSIDEFRAME style.

If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID pen style cannot be used to create a pen with a dithered color. The style PS_INSIDEFRAME is identical to PS_SOLID if the pen width is less than or equal to 1.

The width of a cosmetic pen is always 1; the width of a geometric pen is always specified in world units. After an application creates a logical pen, it can select that pen into a device context by calling the CDC::SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.

  • If nPenStyle is PS_COSMETIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in style units. A style unit is defined by the device in which the pen is used to draw a line.
  • If nPenStyle is PS_GEOMETRIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in logical units.
  • If nPenStyle is PS_ALTERNATE, the style unit is ignored and every other pixel is set.

When an application no longer requires a given pen, it should call the CGdiObject::DeleteObject method or destroy the CPen object so the resource is no longer in use. An application should not delete a pen when the pen is selected in a device context.

Windows CE does not support the following overloaded implementation of the CPen::CreatePen method:

BOOL CreatePen(
int nPenStyle,
int nWidth,
const LOGBRUSH* pLogBrush,
int nStyleCount = 0,
const DWORD* lpStyle = NULL ); 

In Windows CE version 1.0, only solid pens can draw wide lines.

Example

CPen myPen1, myPen2;

// Create a solid red pen of width 2.
myPen1.CreatePen(PS_SOLID, 2, RGB(255,0,0));

// Create a geometric pen.
LOGBRUSH logBrush;
logBrush.lbStyle = BS_SOLID;
logBrush.lbColor = RGB(0,255,0);
myPen2.CreatePen(PS_DOT|PS_GEOMETRIC|PS_ENDCAP_ROUND, 2, &logBrush);

Requirements

  Windows CE versions: 1.0 and later  
  Header file: Declared in Afxwin.h
  Platform: H/PC Pro, Palm-size PC, Pocket PC


6、最後,介紹一些CPen風格,以作備份:

(1)畫筆的樣式:

PS_SOLID      -- 實線畫筆
PS_DASH       -- 虛線畫筆, 只有當畫筆寬度爲1或更小(以設備單位計算)時纔有效
PS_DOT        -- 點線畫筆, 只有當畫筆寬度爲1或更小(以設備單位計算)時纔有效
PS_DASHDOT    -- 虛線和點交替, 只有當畫筆寬度爲1或更小(以設備單位計算)時纔有效
PS_DASHDOTDOT -- 創建一支虛線和兩點交替的畫筆。只有當畫筆寬度爲1或更小(以設備單位計算)時纔有效
PS_NULL       -- 空畫筆
PS_INSIDEFRAME-- 創建一支畫筆,該畫筆在Windows GDI輸出函數所產生的封閉形狀的框架內畫線,此輸出函數指定一個限定矩形(例如,Ellipse,Rectangle,RoundRect,Pie和Chord成員函數),當此風格用於沒有指定限定矩形的Windows GDI輸出函數(例如LineTo成員函數)時,此畫筆的繪製區域不受框架的限制


PS_GEOMETRIC -- 幾何畫筆
PS_COSMETIC  -- 裝飾畫筆
PS_ALTERNATE -- 創建一支交替設置像素的畫筆(此風格只用於裝飾畫筆)  
PS_USERSTYLE -- 創建一支使用用戶提供的風格數組的畫筆


(2)筆帽的樣式:

PS_ENDCAP_ROUND   -- 尾帽是圓的
PS_ENDCAP_SQUARE  -- 尾帽是方的
PS_ENDCAP_FLAT    -- 尾帽是平面的(注: 沒有筆帽)


(3)連接的樣式:

PS_JOIN_BEVEL -- 連接是斜截式的
PS_JOIN_MITER -- 當連接在::SetMiterLimit函數所設置的當前限制之內時, 連接是斜接式的. 如果連接超出這個限制則成爲斜截式的
PS_JOIN_ROUND -- 連接是圓的




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章