The following example creates a
Graphics object and sets its compositing mode to
CompositingModeSourceOver. The code creates a solid brush based on a color that has an alpha component of 128. The code passes the address of that brush to the
GdipFillRectangle function to fill a rectangle with a color that is a half-and-half blend of the brush color and the background color. Then the code sets the compositing mode of the
Graphics object to
CompositingModeSourceCopy and fills a second rectangle with the same brush. In that second rectangle, the brush color is not blended with the background color.
C++
VOID Example_SetCompositingMode(HDC hdc)
{
Graphics graphics(hdc);
// Create a SolidBrush object with an alpha-blended color.
SolidBrush alphaBrush(Color(180, 255, 0, 0));
// Set the compositing mode to CompositingModeSourceOver,
// and fill a rectangle.
graphics.SetCompositingMode(CompositingModeSourceOver);
graphics.FillRectangle(&alphaBrush, 0, 0, 100, 100);
// Set the compositing mode to CompositingModeSourceCopy,
// and fill a rectangle.
graphics.SetCompositingMode(CompositingModeSourceCopy);
graphics.FillRectangle(&alphaBrush, 100, 0, 100, 100);
}
PowerBASIC
SUB GDIP_SetCompositingMode (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pAlphaBrush AS DWORD
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create a SolidBrush object with an alpha-blended color.
hStatus = GdipCreateSolidFill(GDIP_ARGB(180, 255, 0, 0), pAlphaBrush)
' // Set the compositing mode to CompositingModeSourceOver, and fill a rectangle.
hStatus = GdipSetCompositingMode(pGraphics, %CompositingModeSourceOver)
hStatus = GdipFillRectangle(pGraphics, pAlphaBrush, 0, 0, 100, 100)
' // Set the compositing mode to CompositingModeSourceCopy, and fill a rectangle.
hStatus = GdipSetCompositingMode(pGraphics, %CompositingModeSourceCopy)
hStatus = GdipFillRectangle(pGraphics, pAlphaBrush, 100, 0, 100, 100)
' // Cleanup
IF pAlphaBrush THEN GdipDeleteBrush(pAlphaBrush)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipSetCompositingMode.png)