使用 VC++ 和 MFC 绘制一条带有渐变颜色的线



我的问题与以下链接相同

绘制渐变颜色的直线

我需要用渐变颜色画一条曲线。颜色应该非常从浅蓝色到深蓝。我需要用vc++和MFC来做。CPen类似乎只提供使用LOGBRUSH的选项。有选项可以使用各种渐变笔刷与封闭的形状,但不与线或曲线。我计划在每一小段不同阴影的线段中绘制曲线,从而形成一个梯度。有更简单的方法吗?

可以使用Gdi+

首先你需要初始化Gdi+,参见这个链接。

#include <Gdiplus.h>
using namespace Gdiplus;
...
struct GdiplusInit {
    GdiplusInit() {
        GdiplusStartupInput inp;
        GdiplusStartupOutput outp;
        GdiplusStartup(&token_, &inp, &outp);
    }
    ~GdiplusInit() {
        GdiplusShutdown(token_);
    }
private:
    ULONG_PTR token_;
} gdiplusInit; //This will initialize Gdi+ once, and shuts it down on exit

复制问题中的c#示例:

void CMyWnd::OnPaint()
{
    CPaintDC dc(this);
    Graphics gr(dc);
    Point x = Point(0, 0);
    Point y = Point(100, 100);
    LinearGradientBrush brush(x, y, Color(255, 255, 255), Color(255, 0, 0));
    Gdiplus::Pen pen(&brush, 2.0f);
    gr.DrawLine(&pen, x, y);
}

最新更新