在使用Direct2D的MFC应用程序中,我的代码非常简单:
//在ctor:
EnableD2DSupport();
m_pBlackBrush = new CD2DSolidColorBrush(GetRenderTarget(), D2D1::ColorF(D2D1::ColorF::Black));
现在问题是,我应该在m_pBlackBrush上调用delete吗?如果是这样的话?我试图在析构函数中调用delete,但是我在我的脸上发出了错误,说有写入访问冲突。任何人都知道我是否应该删除这个画笔或者只是留下它(这看起来很奇怪)?
答案 0 :(得分:4)
此构造函数的签名是:
CD2DSolidColorBrush(
CRenderTarget* pParentTarget,
D2D1_COLOR_F color,
CD2DBrushProperties* pBrushProperties = NULL,
BOOL bAutoDestroy = TRUE
);
注意最后一个参数。来自MSDN(CD2DSolidColorBrush::CD2DSolidColorBrush):
bAutoDestroy
表示该对象将被所有者(pParentTarget)销毁。
答案 1 :(得分:-1)
以下是Direct2D对象的一些示例:
CChildView::CChildView()
: m_pBitmamLogo(NULL),
m_pBrushBackground(NULL)
{
}
HRESULT CChildView::_LoadBackgroundBrush(CHwndRenderTarget* pRenderTarget)
{
ASSERT_VALID(pRenderTarget);
// Create and load a Direct2D brush from a "PNG" resource
// NOTE: D2D1_EXTEND_MODE_WRAP repeats the brush's content
m_pBrushBackground = new CD2DBitmapBrush(pRenderTarget, // render target
IDB_PNG_BACKGROUND, // resource ID
_T("PNG"), // resource type
CD2DSizeU(0, 0),
&D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP,
D2D1_EXTEND_MODE_WRAP));
return m_pBrushBackground->Create(pRenderTarget);
}
CChildView::~CChildView()
{
// No need to free Direct2D resources
// because they are automatically destroyed by the parent render target
}
来源: http://codexpert.ro/blog/2016/01/18/easy-png-resource-loading-with-mfc/