【问题标题】:how to fill gradient for roundrect in pure gdi (not gdi+)如何在纯 gdi(不是 gdi+)中为圆形填充渐变
【发布时间】:2011-02-05 11:48:01
【问题描述】:

只是在纯 gdi 中。 欢迎大家提出想法或代码。

【问题讨论】:

    标签: winapi gdi


    【解决方案1】:

    创建一个圆角矩形作为路径,选择路径作为剪切路径,然后对同一矩形进行渐变填充。使用 MFC 的代码如下所示:

    int top = 10;
    int left = 10;
    int right = 200;
    int bottom = 200;
    int radius = 20;
    
    
    pDC->BeginPath();
    pDC->RoundRect(left, top, right, bottom, radius, radius);
    pDC->EndPath();
    pDC->SelectClipPath(RGN_COPY);
    
    TRIVERTEX vertices[2];
    
    vertices[0].x = left;
    vertices[0].y = top;
    vertices[0].Red = 0xffff;
    vertices[0].Green = 0;
    vertices[0].Blue = 0;
    vertices[0].Alpha = 0xffff;
    
    vertices[1].x = right;
    vertices[1].y = bottom;
    vertices[1].Red = 0;
    vertices[1].Green = 0;
    vertices[1].Blue = 0xffff;
    vertices[1].Alpha = 0xffff;
    
    GRADIENT_RECT r;
    r.UpperLeft = 0;
    r.LowerRight = 1;
    
    pDC->GradientFill(vertices, 2, &r, 1, GRADIENT_FILL_RECT_V);
    

    如果您不使用 MFC,pDC->x(...) 将替换为 x(your_DC, ...)

    【讨论】: