如何通过扩展 ID2D1SimplifiedGeometrySink 检索几何数据
虽然 ID2D1Geometry 对象是不可变的,但在某些情况下,你需要操作路径 geometry 对象中的几何数据。 通过 Direct2D,可以通过提供名为 ID2D1SimplifiedGeometrySink 的可扩展接口来执行此操作。 对于概念图,本主题介绍如何扩展此接口以从路径 geometry 对象检索 geometry 数据。
扩展 ID2D1SimplifiedGeometrySink 接口
- 实现从 ID2D1SimplifiedGeometrySink 继承的类。
- 创建该类的实例,并将其传递给 ID2D1Geometry::Simplify。
下面的代码示例演示如何实现一个名为 SpecializedSink 的类,该类继承自 ID2D1SimplifiedGeometrySink 接口。 为简单起见,扩展的 AddLines 方法检索几何图形数据,然后将其显示在控制台窗口中;可以自定义此方法以满足特定数据需求。
class SpecializedSink : public ID2D1SimplifiedGeometrySink
{
public:
SpecializedSink()
: m_cRef(1)
{
}
STDMETHOD_(ULONG, AddRef)(THIS)
{
return InterlockedIncrement(reinterpret_cast<LONG volatile *>(&m_cRef));
}
STDMETHOD_(ULONG, Release)(THIS)
{
ULONG cRef = static_cast<ULONG>(
InterlockedDecrement(reinterpret_cast<LONG volatile *>(&m_cRef)));
if(0 == cRef)
{
delete this;
}
return cRef;
}
STDMETHOD(QueryInterface)(THIS_ REFIID iid, void** ppvObject)
{
HRESULT hr = S_OK;
if (__uuidof(IUnknown) == iid)
{
*ppvObject = static_cast<IUnknown*>(this);
AddRef();
}
else if (__uuidof(ID2D1SimplifiedGeometrySink) == iid)
{
*ppvObject = static_cast<ID2D1SimplifiedGeometrySink*>(this);
AddRef();
}
else
{
*ppvObject = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
STDMETHOD_(void, AddBeziers)(const D2D1_BEZIER_SEGMENT * /*beziers*/,
UINT /*beziersCount*/)
{
// Customize this method to meet your specific data needs.
}
STDMETHOD_(void, AddLines)(const D2D1_POINT_2F *points, UINT pointsCount)
{
// Customize this method to meet your specific data needs.
printf("\n\nRetrieving geometry data from a derived ID2D1SimplifiedGeometrySink object:\n");
for (UINT i = 0; i < pointsCount; ++i)
{
printf("%.0f, %.0f\n", points[i].x, points[i].y);
}
}
STDMETHOD_(void, BeginFigure)(D2D1_POINT_2F startPoint,
D2D1_FIGURE_BEGIN figureBegin)
{
}
STDMETHOD_(void, EndFigure)(D2D1_FIGURE_END figureEnd)
{
}
STDMETHOD_(void, SetFillMode)(D2D1_FILL_MODE fillMode)
{
}
STDMETHOD_(void, SetSegmentFlags)(D2D1_PATH_SEGMENT vertexFlags)
{
}
STDMETHOD(Close)()
{
return S_OK;
}
private:
UINT m_cRef;
};
然后,该示例使用一组数据 (182, 209) 、 (211、251) 、 (251、226) 、 (392、360) 和 (101、360) ,用于创建可检索数据的填充路径几何 (m_pGeometry) 。
hr = m_pD2DFactory->CreatePathGeometry(&m_pGeometry);
if(SUCCEEDED(hr))
{
ID2D1GeometrySink *pSink = NULL;
hr = m_pGeometry->Open(&pSink);
if (SUCCEEDED(hr))
{
pSink->SetFillMode(D2D1_FILL_MODE_WINDING);
pSink->BeginFigure(
D2D1::Point2F(101,360),
D2D1_FIGURE_BEGIN_FILLED
);
D2D1_POINT_2F points[5] = {
D2D1::Point2F(182,209),
D2D1::Point2F(211,251),
D2D1::Point2F(251,226),
D2D1::Point2F(392,360),
D2D1::Point2F(101,360),
};
printf("Adding the following geometry data to an ID2D1GeometrySink object:\n");
printf("182, 209\n");
printf("211, 251\n");
printf("251, 226\n");
printf("392, 360\n");
printf("101, 360\n");
pSink->AddLines(points, ARRAYSIZE(points));
pSink->EndFigure(D2D1_FIGURE_END_CLOSED);
hr = pSink->Close();
pSink->Release();
最后,该示例创建一个 SpecializedSink 对象,然后调用 ID2D1Geometry::Simplify 方法,传入 SpecializedSink 对象和 D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES 参数,这会导致任何曲线平展成线段。
SpecializedSink *pSpecializedSink = NULL;
if (SUCCEEDED(hr))
{
pSpecializedSink = new SpecializedSink();
if (!pSpecializedSink)
{
hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
hr = m_pGeometry->Simplify(
D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES, // This causes any curves to be flattened into line segments.
NULL, // world transform
pSpecializedSink
);
if (SUCCEEDED(hr))
{
hr = pSpecializedSink->Close();
}
pSpecializedSink->Release();
}
程序创建输出,如以下屏幕截图所示。
相关主题