参考: case WM_PAINT: /* Divide client width into equal-sized parts, one per palette                
* entry, and re-calculate client width so that it will display                
* exactly as many vertical bars as there are palette entries. */
       GetClientRect(hWnd,(LPRECT) &rClientRect);
       nSizeX = (rClientRect.right - rClientRect.left);
       nSizeX = (nSizeX/iNumColors) * iNumColors;
       nSizeY = rClientRect.bottom - rClientRect.top;
       GetWindowRect(hWnd,(LPRECT) &rClientRect);
       /* Adjust window width so that it can display exactly                
* as many vertical bars( of equal width) as there are palette                
* colors.                */ 
SetWindowPos( hWnd,(HWND)NULL, 0, 0, nSizeX + 2*nXBorder, rClientRect.bottom - rClientRect.top,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
        hDC = BeginPaint(hWnd, &ps);
        /* Select the palette into the window device context and 
     make the Palette Manager map the logical palette to the
   system palette (realize it). */
SelectPalette (hDC,(HPALETTE)hPal, 1);
//could not used !  
//n=RealizePalette (hDC);//=0 ??
/* Calculate width of each color bar to be displayed */
nIncr = nSizeX / iNumColors;
/* Paint the individual bars separately on the app. window */
for (nStart = iLoop = 0; iLoop < iNumColors; iLoop++)
{/* Since this app. uses a logical palette, use the
            * PALETTEINDEX macro to specify the palette entry
            * index instead of using an explicit RGB value. */
hBrush = CreateSolidBrush (PALETTEINDEX (iLoop));
dwPal[iLoop] = GetNearestColor (hDC, PALETTEINDEX (iLoop) );
hOldBrush    = (HBRUSH)SelectObject (hDC,hBrush) ; 
PatBlt (hDC, nStart, 0, nIncr, nSizeY, PATCOPY);
nStart       += nIncr;
SelectObject (hDC, hOldBrush); 
DeleteObject (hBrush) ;
}
wsprintf (szTitlebuf, "MyPal Colors= %d", iNumColors);
SetWindowText (hWnd, (LPSTR)szTitlebuf); 
EndPaint(hWnd,&ps);
break ;

解决方案 »

  1.   

    绘制渐变色BOOL CMyButton::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;
    }DrawGraden(dc.GetSafeHdc(), &CRect(rt.left + 1,rt.top + 1, rt.right - 1, rt.bottom), cl, 3, GRADIENT_FILL_RECT_V); //绘制渐变色
      

  2.   

    http://www.zhaodll.com/ocx/m/41070.html
      

  3.   

    矩形渐变GDI和容易就能实现,你这个只需要划分几个矩形块多次调用GradientFill拼起来即可