【问题标题】:Drawing a line with a gradient color with VC++ & MFC用 VC++ 和 MFC 画一条渐变色的线
【发布时间】:2017-01-31 19:37:49
【问题描述】:

我的问题与以下链接相同

Drawing a line with a gradient color

我需要用渐变颜色绘制曲线。颜色应该从浅蓝色到深蓝色。我需要使用 VC++ 和 MFC 来完成。 CPen 类似乎只提供了使用 LOGBRUSH 的选项。可以选择使用各种具有闭合形状的渐变画笔,但不能使用线条或曲线。我计划在小段线中绘制曲线,每段具有不同的阴影,从而形成渐变。他们有更简单的方法吗?

【问题讨论】:

标签: visual-c++ mfc gdi


【解决方案1】:

您可以通过Gdi+ 做到这一点

首先你需要初始化 Gdi+ 参见 link

#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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多