MFC EnableD2D尝试使用StrokeStyle不起作用

MFC EnableD2DSupport try using StrokeStyle is not working

本文关键字:StrokeStyle 不起作用 EnableD2D MFC      更新时间:2023-10-16

我有一个MFC应用程序,可以绘制线条、文本、不同颜色的矩形等,没有任何问题。但我想画点和虚线,但我没有找到任何方法!我在互联网上找到了以下例子,结果是最后画了一个空图:所有的窗口,而不仅仅是有罪的DrawLine(),这没有任何错误或坏状态。

以下是我的代码示例:

在构造函数中:

// Enable D2D support for this window:
EnableD2DSupport();
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
// Dash array for dashStyle D2D1_DASH_STYLE_CUSTOM
float dashes[] = {1.0f, 2.0f, 2.0f, 3.0f, 2.0f, 2.0f};
// Stroke Style with Dash Style -- Custom
if (SUCCEEDED(hr))
{
        hr = m_pD2DFactory->CreateStrokeStyle(
                D2D1::StrokeStyleProperties(
                        D2D1_CAP_STYLE_FLAT,
                        D2D1_CAP_STYLE_FLAT,
                        D2D1_CAP_STYLE_ROUND,
                        D2D1_LINE_JOIN_MITER,
                        10.0f,
                        D2D1_DASH_STYLE_DASH_DOT,//D2D1_DASH_STYLE_CUSTOM,
                        0.0f),
                0,//dashes,
                0,//ARRAYSIZE(dashes),
                &m_strokeStyle
                );
}

进入OnDraw2d(WPARAM WPARAM,LPARAM LPARAM):

    pRenderTarget->DrawLine(CPoint(rectGraph.left,rect.bottom), CPoint(rectGraph.right,rect.bottom), m_pBlackBrush, 1.0, m_strokeStyle);

但没有StrokeStyle的同一行运行良好:

    pRenderTarget->DrawLine(CPoint(rectGraph.left,rect.bottom), CPoint(rectGraph.right,rect.bottom), m_pBlackBrush, 1.0, NULL);

我做错了什么?

MS为什么要让它变得如此困难,这是个谜。截至2020年12月,以下内容在VS2019中对我有效:在CView派生类中声明成员变量

_AFX_D2D_STATE* m_pD2DState = nullptr;

在构造函数中,初始化它

EnableD2DSupport();
m_pD2DState = AfxGetD2DState();

最后-获得工厂并使用它来创建笔划样式的

ID2D1Factory *pFactory = m_pD2DState->GetDirect2dFactory();
pFactory->CreateStrokeStyle(...);