// Ends drawing updates started by a previous BeginDraw call. void Scenario1ImageSource::EndDraw() { // Remove the transform and clip applied in BeginDraw since // the target area can change on every update. m_d2dContext->SetTransform(D2D1::IdentityMatrix()); m_d2dContext->PopAxisAlignedClip(); // Remove the render target and end drawing. DX::ThrowIfFailed( m_d2dContext->EndDraw() ); m_d2dContext->SetTarget(nullptr); // Query for ISurfaceImageSourceNative interface. Microsoft::WRL::ComPtr<ISurfaceImageSourceNative> sisNative; DX::ThrowIfFailed( reinterpret_cast<IUnknown*>(this)->QueryInterface(IID_PPV_ARGS(&sisNative)) ); DX::ThrowIfFailed( sisNative->EndDraw() ); }
void TextInlineFormat_Shadow::ApplyInlineFormat(ID2D1RenderTarget* target, IDWriteTextLayout* layout, ID2D1SolidColorBrush* solidBrush, const UINT32& strLen, const D2D1_POINT_2F& drawPosition) { if (!target || !layout) return; // In order to make a shadow effect using the built-in D2D effect, we first need to make // certain parts of the string transparent. We then draw only the parts of the string we // we want a shadow for onto a memory bitmap. From this bitmap we can create the shadow // effect and draw it. D2D1_COLOR_F color = D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.0f); Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> transparent; HRESULT hr = target->CreateSolidColorBrush(color, transparent.GetAddressOf()); if (FAILED(hr)) return; // Only change characters outside of the range(s) transparent for (UINT32 i = 0; i < strLen; ++i) { bool found = false; for (const auto& range : GetRanges()) { if (range.length <= 0) continue; if (i >= range.startPosition && i < (range.startPosition + range.length)) { found = true; break; } } if (!found) { DWRITE_TEXT_RANGE temp = { i, 1 }; layout->SetDrawingEffect(transparent.Get(), temp); } } Microsoft::WRL::ComPtr<ID2D1Bitmap> bitmap; Microsoft::WRL::ComPtr<ID2D1BitmapRenderTarget> bTarget; hr = target->CreateCompatibleRenderTarget(bTarget.GetAddressOf()); if (FAILED(hr)) return; // Draw onto memory bitmap target bTarget->BeginDraw(); bTarget->DrawTextLayout(drawPosition, layout, solidBrush); bTarget->EndDraw(); hr = bTarget->GetBitmap(bitmap.GetAddressOf()); if (FAILED(hr)) return; // Shadow effects can only be drawn with a D2D device context Microsoft::WRL::ComPtr<ID2D1DeviceContext> dc; hr = target->QueryInterface(__uuidof(ID2D1DeviceContext), reinterpret_cast<void**>(dc.GetAddressOf())); if (FAILED(hr)) return; // Create shadow effect Microsoft::WRL::ComPtr<ID2D1Effect> shadow; hr = dc->CreateEffect(CLSID_D2D1Shadow, &shadow); if (FAILED(hr)) return; // Load shadow options to effect shadow->SetInput(0, bitmap.Get()); shadow->SetValue(D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, m_Blur); shadow->SetValue(D2D1_SHADOW_PROP_COLOR, ToVector4F(m_Color)); shadow->SetValue(D2D1_SHADOW_PROP_OPTIMIZATION, D2D1_SHADOW_OPTIMIZATION_SPEED); // Draw effect dc->DrawImage(shadow.Get(), m_Offset); }