The following example sets the colors that are interpolated for this linear gradient brush to red, blue, and green and sets the blend positions to 0, 0.3, and 1. The code calls the
GdipGetLinePresetBlend function to obtain the number of colors currently set to be interpolated for the brush. Next, the code gets the colors and their positions. Then, the code fills a small rectangle with each color.
C++
VOID Example_GetInterpColors(HDC hdc)
{
Graphics myGraphics(hdc);
// Create a linear gradient brush, and set the colors to be interpolated.
Color col[] = {
Color(255, 255, 0, 0), // red
Color(255, 0, 0, 255), // blue
Color(255, 0, 255, 0)}; // green
REAL pos[] = {
0.0f, // red at the left edge
0.3f, // blue at 30 percent of the distance from
// left edge to right edge
1.0f}; // green at the right edge
LinearGradientBrush linGrBrush(
Point(0, 0),
Point(100, 0),
Color(255, 0, 0, 0), // black
Color(255, 255, 255, 255)); // white
linGrBrush.SetInterpolationColors(col, pos, 3);
// Obtain information about the linear gradient brush.
INT colorCount = 0;
Color* colors = NULL;
REAL* positions = NULL;
// How many colors have been specified to be interpolated
// for this brush?
colorCount = linGrBrush.GetInterpolationColorCount();
// Allocate a buffer large enough to hold the set of colors.
colors = new Color[colorCount];
// Allocate a buffer to hold the relative positions of the colors.
positions = REAL[colorCount];
// Get the colors and their relative positions.
linGrBrush.GetInterpolationColors(colors, positions, colorCount);
// Fill a small rectangle with each of the colors.
SolidBrush* pSolidBrush;
for(INT j = 0; j < colorCount; j++)
{
pSolidBrush = new SolidBrush(colors[j]);
myGraphics.FillRectangle(pSolidBrush, 15*j, 0, 10, 10);
delete(pSolidBrush);
}
}
PowerBASIC
SUB GDIP_GetLinePresetBlend (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pLinBrush AS DWORD
LOCAL colorBlack AS DWORD
LOCAL colorWhite AS DWORD
LOCAL colorCount AS LONG
LOCAL pBrush AS DWORD
LOCAL i AS LONG
LOCAL pt1 AS POINTF
LOCAL pt2 AS POINTF
DIM col(2) AS DWORD
DIM pos(2) AS SINGLE
DIM colors(0) AS DWORD
DIM positions(0) AS SINGLE
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a linear gradient brush, and set the colors to be interpolated.
col(0) = GDIP_ARGB(255, 255, 0, 0)
col(1) = GDIP_ARGB(255, 0, 0, 255)
col(2) = GDIP_ARGB(255, 0, 255, 0)
pos(0) = 0.0! ' // red at the left edge
pos(1) = 0.3! ' // blue at 30 percent of the distance from left edge to right edge
pos(2) = 1.0! ' // green at the right edge
pt1.x = 0 : pt1.y = 0 : pt2.x = 100 : pt2.y = 0
colorBlack = GDIP_ARGB(255, 0, 0, 0)
colorWhite = GDIP_ARGB(255, 255, 255, 255)
hStatus = GdipCreateLineBrush(pt1, pt2, colorBlack, colorWhite, %WrapModeTile, pLinBrush)
hStatus = GdipSetLinePresetBlend(pLinBrush, col(0), pos(0), 3)
' // Obtain information about the linear gradient brush.
' // How many colors have been specified to be interpolated for this brush?
hStatus = GdipGetLinePresetBlendCount(pLinBrush, colorCount)
IF colorCount THEN
' // Allocate a buffer large enough to hold the set of colors.
REDIM colors(colorCount-1)
' // Allocate a buffer to hold the relative positions of the colors.
REDIM positions(colorCount-1)
' // Get the colors and their relative positions.
hStatus = GdipGetLinePresetBlend(pLinBrush, colors(0), positions(0), colorCount)
FOR i = 0 TO colorCount - 1
hStatus = GdipCreateSolidFill(colors(i), pBrush)
IF pBrush THEN
hStatus = GdipFillRectangle(pGraphics, pBrush, 15 * i, 0, 10, 10)
hStatus = GdipDeleteBrush(pBrush)
END IF
NEXT
END IF
' // Cleanup
IF pLinBrush THEN GdipDeleteBrush(pLinBrush)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipGetLinePresetBlend.png)