D2D1_GRADIENT_STOP 是 Direct2D 库中定义的一个结构,用于描述渐变中的一个颜色停止点。该结构在头文件 D2d1.h 中声明。

以下是该结构的定义:
typedef struct D2D1_GRADIENT_STOP {
  FLOAT          position;
  D2D1_COLOR_F color;
} D2D1_GRADIENT_STOP;

这个结构有以下成员:

  •  position: 表示渐变中颜色停止点的位置,范围在 0.0 到 1.0 之间。0.0 表示渐变的起始点,1.0 表示渐变的结束点。

  •  color: 一个 D2D1_COLOR_F 结构,表示在该位置的颜色。


使用这个结构可以方便地描述一个渐变中的颜色停止点,然后通过 Direct2D 的绘图接口进行渐变的绘制。

以下是一个使用 D2D1_GRADIENT_STOP 结构创建渐变刷子的简单示例:
#include <d2d1.h>

// 假设 pRenderTarget 是一个有效的 ID2D1RenderTarget 指针
ID2D1RenderTarget *pRenderTarget;

// 创建两个颜色停止点
D2D1_GRADIENT_STOP gradientStops[] = {
    {0.0f, D2D1::ColorF(D2D1::ColorF::Red)},
    {1.0f, D2D1::ColorF(D2D1::ColorF::Blue)}
};

// 创建渐变刷子
ID2D1GradientStopCollection *pGradientStopCollection;
pRenderTarget->CreateGradientStopCollection(gradientStops, ARRAYSIZE(gradientStops), &pGradientStopCollection);

ID2D1LinearGradientBrush *pLinearGradientBrush;
pRenderTarget->CreateLinearGradientBrush(D2D1::LinearGradientBrushProperties(D2D1::Point2F(0.0f, 0.0f), D2D1::Point2F(100.0f, 0.0f)), pGradientStopCollection, &pLinearGradientBrush);

// 使用生成的渐变刷子进行绘制

在实际使用中,请确保在调用相关绘图函数之前已经初始化了 Direct2D 环境,并适当处理可能的错误。


转载请注明出处:http://www.pingtaimeng.com/article/detail/25221/Win32 API/D2d1.h/D2D1_GRADIENT_STOP