PathGradientBrush 類別允許你自訂填補形狀的方式,並以逐漸變化的顏色呈現。 PathGradientBrush 物件有邊界路徑和中心點。 你可以指定一種顏色作為中心點,另一種顏色作為邊界。 你也可以為邊界上的幾個點指定不同的顏色。
Note
在 GDI+ 中,路徑是由 GraphicsPath 物件維護的一系列線條和曲線。 欲了解更多關於GDI+路徑的資訊,請參閱「路徑」及「建構與繪製路徑」。
以下範例用路徑梯度刷子填充橢圓。 中心色設為藍色,邊界色設為水藍色。
// Create a path that consists of a single ellipse.
GraphicsPath path;
path.AddEllipse(0, 0, 140, 70);
// Use the path to construct a brush.
PathGradientBrush pthGrBrush(&path);
// Set the color at the center of the path to blue.
pthGrBrush.SetCenterColor(Color(255, 0, 0, 255));
// Set the color along the entire boundary of the path to aqua.
Color colors[] = {Color(255, 0, 255, 255)};
int count = 1;
pthGrBrush.SetSurroundColors(colors, &count);
graphics.FillEllipse(&pthGrBrush, 0, 0, 140, 70);
下圖顯示填滿橢圓形。
預設情況下,路徑梯度刷不會延伸出路徑邊界。 如果你用路徑漸層刷子填滿超出路徑邊界的形狀,路徑外的螢幕區域就不會被填滿。 以下圖示展示了將前述程式碼中的 Graphics::FillEllipse 呼叫改為 graphics.FillRectangle(&pthGrBrush, 0, 10, 200, 40)時會發生什麼事。
邊界上的指定點
以下範例會從星形路徑建立路徑漸層筆刷。 程式碼呼叫 PathGradientBrush::SetCenterColor 方法,將恆星重心處的顏色設定為紅色。 接著程式碼呼叫 PathGradientBrush::SetSurroundColors 方法,指定在 points 陣列中各個點的各種顏色(儲存在 colors 陣列中)。 最後一個程式碼陳述式會使用路徑漸層筆刷填滿星形路徑。
// Put the points of a polygon in an array.
Point points[] = {Point(75, 0), Point(100, 50),
Point(150, 50), Point(112, 75),
Point(150, 150), Point(75, 100),
Point(0, 150), Point(37, 75),
Point(0, 50), Point(50, 50)};
// Use the array of points to construct a path.
GraphicsPath path;
path.AddLines(points, 10);
// Use the path to construct a path gradient brush.
PathGradientBrush pthGrBrush(&path);
// Set the color at the center of the path to red.
pthGrBrush.SetCenterColor(Color(255, 255, 0, 0));
// Set the colors of the points in the array.
Color colors[] = {Color(255, 0, 0, 0), Color(255, 0, 255, 0),
Color(255, 0, 0, 255), Color(255, 255, 255, 255),
Color(255, 0, 0, 0), Color(255, 0, 255, 0),
Color(255, 0, 0, 255), Color(255, 255, 255, 255),
Color(255, 0, 0, 0), Color(255, 0, 255, 0)};
int count = 10;
pthGrBrush.SetSurroundColors(colors, &count);
// Fill the path with the path gradient brush.
graphics.FillPath(&pthGrBrush, &path);
以下圖示顯示填滿的星形。
以下範例基於一組點構建路徑梯度刷子。 陣列中的五個點各分配一種顏色。 如果你用直線連接這五個點,你會得到一個五邊形多邊形。 該多邊形的中心(重心)也會被指定顏色——在此範例中,中心(80, 75)被設定為白色。 範例中的最後一個程式碼陳述是用路徑梯度刷子填充一個矩形。
填滿矩形的顏色在(80, 75)處為白色,隨著你從(80, 75)向陣列中的點移動,顏色會逐漸改變。 例如,當你從 (80, 75) 移動到 (0, 0) 時,顏色會逐漸從白色變成紅色;當你從 (80, 75) 移動到 (160, 0) 時,顏色也會逐漸從白色變成綠色。
// Construct a path gradient brush based on an array of points.
PointF ptsF[] = {PointF(0.0f, 0.0f),
PointF(160.0f, 0.0f),
PointF(160.0f, 200.0f),
PointF(80.0f, 150.0f),
PointF(0.0f, 200.0f)};
PathGradientBrush pBrush(ptsF, 5);
// An array of five points was used to construct the path gradient
// brush. Set the color of each point in that array.
Color colors[] = {Color(255, 255, 0, 0), // (0, 0) red
Color(255, 0, 255, 0), // (160, 0) green
Color(255, 0, 255, 0), // (160, 200) green
Color(255, 0, 0, 255), // (80, 150) blue
Color(255, 255, 0, 0)}; // (0, 200) red
int count = 5;
pBrush.SetSurroundColors(colors, &count);
// Set the center color to white.
pBrush.SetCenterColor(Color(255, 255, 255, 255));
// Use the path gradient brush to fill a rectangle.
graphics.FillRectangle(&pBrush, Rect(0, 0, 180, 220));
請注意,前述程式碼中沒有 GraphicsPath 物件。 範例中的 PathGradientBrush 建構器會接收指向點陣列的指標,但不需要 GraphicsPath 物件。 另外,請注意路徑漸層刷子是用來填充矩形,而不是路徑。 這個矩形比用來定義畫筆的路徑還大,因此矩形有一部分不會由該畫筆繪製。 以下圖示展示了矩形(虛線)以及由路徑漸層畫筆所繪製的矩形部分。
自訂路徑梯度
自訂路徑漸層畫筆的一種方法是設定其焦點比例。 焦點刻度指定一條位於主路徑內部的內側路徑。 中心顏色會顯示在內側路徑的每個位置,而非僅在中心點。 要設定路徑漸層筆刷的焦點刻度,請呼叫 PathGradientBrush::SetFocusScales 方法。
以下範例基於橢圓路徑建立路徑梯度刷子。 程式碼將邊界顏色設為藍色,中心色設為水藍色,然後使用路徑漸層刷子填充橢圓形路徑。
接著程式碼設定路徑漸變刷的焦點刻度。 X 焦距刻度設為 0.3,y 對焦刻度設為 0.8。 程式碼呼叫 Graphics 物件的 Graphics::TranslateTransform 方法,使得後續呼叫 Graphics::FillPath 會填滿位於第一個橢圓右側的橢圓。
要觀察焦點刻度的效果,可以想像一個與主橢圓共用中心的小橢圓。 小型(內部)橢圓是將主橢圓以其中心為基準,在水平方向縮放為 0.3 倍、在垂直方向縮放為 0.8 倍後得到的橢圓。 當你從外層橢圓邊界移動到內層橢圓邊界時,顏色會逐漸從藍色變成青藍色。 當你從內層橢圓邊界移動到共享中心時,顏色仍保持水藍色。
// Create a path that consists of a single ellipse.
GraphicsPath path;
path.AddEllipse(0, 0, 200, 100);
// Create a path gradient brush based on the elliptical path.
PathGradientBrush pthGrBrush(&path);
pthGrBrush.SetGammaCorrection(TRUE);
// Set the color along the entire boundary to blue.
Color color(Color(255, 0, 0, 255));
INT num = 1;
pthGrBrush.SetSurroundColors(&color, &num);
// Set the center color to aqua.
pthGrBrush.SetCenterColor(Color(255, 0, 255, 255));
// Use the path gradient brush to fill the ellipse.
graphics.FillPath(&pthGrBrush, &path);
// Set the focus scales for the path gradient brush.
pthGrBrush.SetFocusScales(0.3f, 0.8f);
// Use the path gradient brush to fill the ellipse again.
// Show this filled ellipse to the right of the first filled ellipse.
graphics.TranslateTransform(220.0f, 0.0f);
graphics.FillPath(&pthGrBrush, &path);
以下圖示展示了前述程式碼的輸出。 左側的橢圓僅在中心點呈現水藍色。 右側的橢圓在內側路徑內的所有區域都是水藍色。
另一種自訂路徑漸層筆刷的方法,是指定預設顏色陣列和插值位置陣列。
以下範例基於三角形建立路徑梯度刷子。 程式碼呼叫路徑漸層刷子的 PathGradientBrush::SetInterpolationColors 方法,指定插值顏色陣列(深綠、青藍、藍色)及插值位置陣列(0、0.25、1)。 當你從三角形邊界移動到中心點時,顏色會逐漸從深綠變成青藍色,再從青藍色變成藍色。 從深綠色到水藍色的變化發生在深綠色到藍色距離的25%內。
// Vertices of the triangle
Point points[] = {Point(100, 0),
Point(200, 200),
Point(0, 200)};
// No GraphicsPath object is created. The PathGradient
// brush is constructed directly from the array of points.
PathGradientBrush pthGrBrush(points, 3);
Color presetColors[] = {
Color(255, 0, 128, 0), // Dark green
Color(255, 0, 255, 255), // Aqua
Color(255, 0, 0, 255)}; // Blue
REAL interpPositions[] = {
0.0f, // Dark green is at the boundary of the triangle.
0.25f, // Aqua is 25 percent of the way from the boundary
// to the center point.
1.0f}; // Blue is at the center point.
pthGrBrush.SetInterpolationColors(presetColors, interpPositions, 3);
// Fill a rectangle that is larger than the triangle
// specified in the Point array. The portion of the
// rectangle outside the triangle will not be painted.
graphics.FillRectangle(&pthGrBrush, 0, 0, 200, 200);
以下圖示展示了前述程式碼的輸出。
設定中心點
預設情況下,路徑梯度刷子的中心點位於用於構造刷子路徑的重心點。 你可以透過呼叫 PathGradientBrush 類別中的 PathGradientBrush::SetCenterPoint 方法來更改中心點的位置。
以下範例基於橢圓建立路徑梯度刷子。 橢圓的中心位於(70,35),但路徑漸層筆刷的中心點設定為(120,40)。
// Create a path that consists of a single ellipse.
GraphicsPath path;
path.AddEllipse(0, 0, 140, 70);
// Use the path to construct a brush.
PathGradientBrush pthGrBrush(&path);
// Set the center point to a location that is not the centroid of the path.
pthGrBrush.SetCenterPoint(Point(120, 40));
// Set the color at the center point to blue.
pthGrBrush.SetCenterColor(Color(255, 0, 0, 255));
// Set the color along the entire boundary of the path to aqua.
Color colors[] = {Color(255, 0, 255, 255)};
int count = 1;
pthGrBrush.SetSurroundColors(colors, &count);
graphics.FillEllipse(&pthGrBrush, 0, 0, 140, 70);
下圖顯示已填滿的橢圓以及路徑漸層筆刷的中心點。
你可以將路徑漸層筆刷的中心點設在用來建構該筆刷的路徑之外的位置。 在前述程式碼中,若將 PathGradientBrush::SetCenterPoint 的呼叫替換為 pthGrBrush.SetCenterPoint(Point(145, 35)),會得到以下結果。
在前述圖中,橢圓最右端的點並非純藍色(雖然非常接近)。 漸變中的顏色排列方式是,如果填充達到某點(145, 35),顏色會達到純藍色(0, 0, 255)。 但填滿範圍永遠不會延伸到 (145, 35),因為路徑漸層筆刷只會在其路徑內繪製。
路徑漸變色彩的計算方式
對於路徑內的每個像素,畫筆會從中心點投射一條假想光線,穿過像素,直到光線與路徑邊界交點。 該像素的顏色會根據其在該射線上的位置,在中心色與邊界色之間進行線性插值。 具體來說,插值因子是從中心點到像素的距離除以從中心點到邊界交點的總距離的比值。
當中心點在路徑外時,路徑上靠近中心點一側的像素會有短光線(因為邊界在該方向附近)。 由於這些像素距離其短光線相對較遠,因此看起來更接近中心色彩。 路徑遠端的像素有長光線,且看起來更接近邊界顏色。 這解釋了為何在前述圖中,靠近外部中心點的邊界呈現接近藍色(中心色)而非水藍色(邊界色)。