共用方式為


設定畫筆寬度和對齊方式

當您建立 Pen 物件時,您可以將畫筆寬度當做建構函式的其中一個自變數來提供。 您也可以使用 Pen::SetWidth 方法來變更畫筆寬度。

理論線的寬度為零。 當您繪製線條時,圖元會置中於理論線上。 下列範例會繪製一條指定的線條兩次:一次使用寬度為 1 的黑色畫筆,另一次使用寬度為 10 的綠色畫筆。

Pen blackPen(Color(255, 0, 0, 0), 1);
Pen greenPen(Color(255, 0, 255, 0), 10);
stat = greenPen.SetAlignment(PenAlignmentCenter);

// Draw the line with the wide green pen.
stat = graphics.DrawLine(&greenPen, 10, 100, 100, 50);

// Draw the same line with the thin black pen.
stat = graphics.DrawLine(&blackPen, 10, 100, 100, 50);

下圖顯示上述程式代碼的輸出。 綠色圖元和黑色圖元位於理論線上。

插圖顯示一條薄的黑色對角線,周圍環繞著寬大的綠線

下列範例會繪製指定的矩形兩次:一次使用寬度為 1 的黑色畫筆,另一次使用寬度為 10 的綠色畫筆。 程式碼將 PenAlignmentCenter 的值(為 PenAlignment 列舉中的一個元素)傳遞給 Pen::SetAlignment 方法,以指定使用綠色畫筆繪製的像素居中於矩形的邊界。

Pen blackPen(Color(255, 0, 0, 0), 1);
Pen greenPen(Color(255, 0, 255, 0), 10);
stat = greenPen.SetAlignment(PenAlignmentCenter);

// Draw the rectangle with the wide green pen.
stat = graphics.DrawRectangle(&greenPen, 10, 100, 50, 50);

// Draw the same rectangle with the thin black pen.
stat = graphics.DrawRectangle(&blackPen, 10, 100, 50, 50);

下圖顯示上述程式代碼的輸出。 綠色圖元置於理論矩形的中心,而理論矩形以黑色圖元表示。

圖例顯示矩形形狀的細黑色線條,周圍是較寬的綠色線條

您可以修改上述範例中的第三個語句,以變更綠色畫筆的對齊方式,如下所示:

stat = greenPen.SetAlignment(PenAlignmentInset);

現在,寬綠色線條中的圖元會出現在矩形內部,如下圖所示。

圖示顯示一個矩形中的細黑線,包圍了一個相同形狀的寬綠線