rgn.CreateRoundRectRgn(rect.left,rect.top,rect.right,rect.bottom,10,10);
dc.SelectClipRgn(&rgn);
COLORREF clGreen = RGB(0,255,255);
COLORREF clYellow = RGB(255,255,0);
COLORREF clRed = RGB(255,0,0);
COLORREF clBlue = RGB(0,0,255);
COLORREF clFuchsia = RGB(2,48,54);
DWORD cl[5] = {clGreen, clYellow, clRed, clBlue, clFuchsia};
DrawGraden(hdc, &rect, cl, 5, GRADIENT_FILL_RECT_H);
//GRADIENT_FILL_RECT_H代表左右填充,GRADIENT_FILL_RECT_V上下填充;
BOOL CDlgTest:: DrawGraden(HDC hdc, CONST RECT *pRect, CONST DWORD *cl, int Num, DWORD dwMode)
{
int Width;
int Height;
TRIVERTEX *pvert;
GRADIENT_RECT *pgRect;
if (cl == NULL || Num < 1 || pRect == NULL || dwMode == GRADIENT_FILL_TRIANGLE)
{
::SetLastError(ERROR_INVALID_PARAMETER);
return TRUE;
}
if (Num == 1)
{
HBRUSH hBrush = CreateSolidBrush(cl[0]);
SelectObject(hdc, hBrush);
FillRect(hdc, pRect, hBrush);
DeleteObject(hBrush);
return TRUE;
}
pvert = new TRIVERTEX[Num * 2 - 2];
pgRect = new GRADIENT_RECT[Num];
Width = pRect->right - pRect->left;
Height = pRect->bottom - pRect->top;
for (int i = 0; i < Num - 1; i++)
{
if (dwMode == GRADIENT_FILL_RECT_V)
{
pvert[i * 2].x = pRect->left;
pvert[i * 2].y = pRect->top + Height / (Num - 1) * i;
pvert[i * 2 + 1].x = pRect->right;
pvert[i * 2 + 1].y = pRect->top + Height / (Num - 1) * (i + 1);
}
else if (dwMode == GRADIENT_FILL_RECT_H)
{
pvert[i * 2].x = pRect->left + Width / (Num - 1) * i;
pvert[i * 2].y = pRect->top;
pvert[i * 2 + 1].x = pRect->left + Width / (Num - 1) * (i + 1);
pvert[i * 2 + 1].y = pRect->bottom;
}
pvert[i * 2] .Red = (WORD)GetRValue((cl[i])) << 8;
pvert[i * 2] .Green = (WORD)GetGValue((cl[i])) << 8;
pvert[i * 2] .Blue = (WORD)GetBValue((cl[i])) << 8;
pvert[i * 2] .Alpha = 0x0000;
pvert[i * 2 + 1] .Red = (WORD)GetRValue((cl[i + 1])) << 8;
pvert[i * 2 + 1] .Green = (WORD)GetGValue((cl[i + 1])) << 8;
pvert[i * 2 + 1] .Blue = (WORD)GetBValue((cl[i + 1])) << 8;
pvert[i * 2 + 1] .Alpha = 0x0000;
pgRect[i].UpperLeft = i * 2;
pgRect[i].LowerRight = i * 2 + 1;
}
BOOL bRet = ::GradientFill(hdc, pvert, Num * 2, pgRect, Num - 1, dwMode);
delete []pvert;
delete []pgRect;
return bRet;
}