1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
6 //// Copyright (c) Microsoft Corporation. All rights reserved
9 #include "SampleOverlay.h"
11 using namespace Windows::UI::Core;
12 using namespace Windows::Foundation;
13 using namespace Microsoft::WRL;
14 using namespace Windows::UI::ViewManagement;
15 using namespace Windows::Graphics::Display;
18 SampleOverlay::SampleOverlay() :
23 void SampleOverlay::Initialize(
24 _In_ ID2D1Device* d2dDevice,
25 _In_ ID2D1DeviceContext* d2dContext,
26 _In_ IWICImagingFactory* wicFactory,
27 _In_ IDWriteFactory* dwriteFactory,
28 _In_ Platform::String^ caption
31 m_wicFactory = wicFactory;
32 m_dwriteFactory = dwriteFactory;
33 m_sampleName = caption;
34 m_d2dDevice = d2dDevice;
35 m_d2dContext = d2dContext;
37 m_textVerticalOffset = 5.0f;
38 m_logoSize = D2D1::SizeF(0.0f, 0.0f);
39 m_overlayWidth = 0.0f;
41 ComPtr<ID2D1Factory> factory;
42 d2dDevice->GetFactory(&factory);
45 factory.As(&m_d2dFactory)
48 ResetDirectXResources();
51 void SampleOverlay::ResetDirectXResources()
54 m_d2dContext->CreateSolidColorBrush(ColorF(ColorF::White), &m_whiteBrush)
57 ComPtr<IWICBitmapDecoder> wicBitmapDecoder;
59 m_wicFactory->CreateDecoderFromFilename(
60 L"windowsbig-sdk.png",
63 WICDecodeMetadataCacheOnDemand,
68 ComPtr<IWICBitmapFrameDecode> wicBitmapFrame;
70 wicBitmapDecoder->GetFrame(0, &wicBitmapFrame)
73 ComPtr<IWICFormatConverter> wicFormatConverter;
75 m_wicFactory->CreateFormatConverter(&wicFormatConverter)
79 wicFormatConverter->Initialize(
81 GUID_WICPixelFormat32bppPBGRA,
82 WICBitmapDitherTypeNone,
85 WICBitmapPaletteTypeCustom // the BGRA format has no palette so this value is ignored
92 wicFormatConverter->GetResolution(&dpiX, &dpiY)
96 m_d2dContext->CreateBitmapFromWicBitmap(
97 wicFormatConverter.Get(),
99 PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
100 static_cast<float>(dpiX),
101 static_cast<float>(dpiY)
107 m_logoSize = m_logoBitmap->GetSize();
109 ComPtr<IDWriteTextFormat> nameTextFormat;
111 m_dwriteFactory->CreateTextFormat(
114 DWRITE_FONT_WEIGHT_LIGHT,
115 DWRITE_FONT_STYLE_NORMAL,
116 DWRITE_FONT_STRETCH_NORMAL,
124 nameTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING)
128 nameTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR)
132 m_dwriteFactory->CreateTextLayout(
133 m_sampleName->Data(),
134 m_sampleName->Length(),
135 nameTextFormat.Get(),
142 DWRITE_TEXT_METRICS metrics = {0};
144 m_textLayout->GetMetrics(&metrics)
147 m_overlayWidth = m_padding * 3.0f + m_logoSize.width + metrics.width;
150 m_d2dFactory->CreateDrawingStateBlock(&m_stateBlock)
153 UpdateForWindowSizeChange();
156 void SampleOverlay::UpdateForWindowSizeChange()
158 if (CoreWindow::GetForCurrentThread()->Bounds.Width < m_overlayWidth)
160 m_drawOverlay = false;
164 m_drawOverlay = true;
168 void SampleOverlay::Render()
172 m_d2dContext->SaveDrawingState(m_stateBlock.Get());
174 m_d2dContext->BeginDraw();
175 m_d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
176 m_d2dContext->DrawBitmap(
178 D2D1::RectF(m_padding, 0.0f, m_logoSize.width + m_padding, m_logoSize.height)
181 m_d2dContext->DrawTextLayout(
182 Point2F(m_logoSize.width + 2.0f * m_padding, m_textVerticalOffset),
187 // We ignore the HRESULT returned as we want to application to handle the
188 // error when it uses Direct2D next.
189 m_d2dContext->EndDraw();
191 m_d2dContext->RestoreDrawingState(m_stateBlock.Get());
195 float SampleOverlay::GetTitleHeightInDips()
197 return m_logoSize.height;