@@ -108,55 +108,61 @@ static float GetPenThickness (const NUIE::Pen& pen)
108108Direct2DContext::Direct2DContext (const Direct2DImageLoaderPtr& imageLoader) :
109109 NUIE::NativeDrawingContext (),
110110 direct2DHandler (),
111- hwnd (NULL ),
112111 width (0 ),
113112 height (0 ),
114113 imageLoader (imageLoader),
115- renderTarget (nullptr )
114+ renderTarget (nullptr ),
115+ memoryDC (NULL ),
116+ memoryBitmap (NULL )
116117{
117118
118119}
119120
120121Direct2DContext::~Direct2DContext ()
121122{
123+ DeleteObject (memoryBitmap);
124+ DeleteDC (memoryDC);
122125 SafeRelease (&renderTarget);
123126}
124127
125128void Direct2DContext::Init (void * nativeHandle)
126129{
127- hwnd = (HWND) nativeHandle;
130+ HWND hwnd = (HWND) nativeHandle;
131+
132+ RECT clientRect;
133+ GetClientRect (hwnd, &clientRect);
134+ width = clientRect.right - clientRect.left ;
135+ height = clientRect.bottom - clientRect.top ;
136+
128137 CreateRenderTarget ();
138+ CreateOffscreenContext ();
129139}
130140
131141void Direct2DContext::BlitToWindow (void * nativeHandle)
132142{
133- HWND targetHwnd = (HWND) nativeHandle;
134- if (hwnd == targetHwnd) {
135- return ;
136- }
143+ HWND hwnd = (HWND) nativeHandle;
144+
137145 PAINTSTRUCT ps;
138- HDC targetHdc = BeginPaint (targetHwnd , &ps);
139- BlitToContext (targetHdc );
140- EndPaint (targetHwnd , &ps);
146+ HDC hdc = BeginPaint (hwnd , &ps);
147+ BlitToContext (hdc );
148+ EndPaint (hwnd , &ps);
141149}
142150
143151void Direct2DContext::BlitToContext (void * nativeContext)
144152{
145- HDC targetHdc = (HDC) nativeContext;
146- if (hwnd == WindowFromDC (targetHdc)) {
147- return ;
148- }
149- BitBlt (targetHdc, 0 , 0 , width, height, GetDC (renderTarget->GetHwnd ()), 0 , 0 , SRCCOPY);
153+ HDC hdc = (HDC) nativeContext;
154+ BitBlt (hdc, 0 , 0 , width, height, memoryDC, 0 , 0 , SRCCOPY);
150155}
151156
152157void Direct2DContext::Resize (int newWidth, int newHeight)
153158{
159+ if (newWidth == 0 || newHeight == 0 || renderTarget == nullptr ) {
160+ return ;
161+ }
162+
154163 width = newWidth;
155164 height = newHeight;
156- if (width > 0 && height > 0 ) {
157- D2D1_SIZE_U size = D2D1::SizeU (width, height);
158- renderTarget->Resize (size);
159- }
165+ CreateOffscreenContext ();
160166}
161167
162168double Direct2DContext::GetWidth () const
@@ -326,23 +332,34 @@ void Direct2DContext::DrawIcon (const NUIE::Rect& rect, const NUIE::IconId& icon
326332
327333void Direct2DContext::CreateRenderTarget ()
328334{
329- RECT clientRect;
330- GetClientRect (hwnd, &clientRect);
331- width = clientRect.right - clientRect.left ;
332- height = clientRect.bottom - clientRect.top ;
333-
334- D2D1_SIZE_U size = D2D1::SizeU (width, height);
335- D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties ();
336- D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties = D2D1::HwndRenderTargetProperties (hwnd, size);
337-
338335 SafeRelease (&renderTarget);
339336 if (imageLoader != nullptr ) {
340337 imageLoader->ClearCache ();
341338 }
342339
343- direct2DHandler.direct2DFactory ->CreateHwndRenderTarget (renderTargetProperties, hwndRenderTargetProperties, &renderTarget);
344- renderTarget->SetDpi (96 .0f , 96 .0f );
340+ D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties (
341+ D2D1_RENDER_TARGET_TYPE_DEFAULT,
342+ D2D1::PixelFormat (DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
343+ 96 .0f , 96 .0f ,
344+ D2D1_RENDER_TARGET_USAGE_NONE,
345+ D2D1_FEATURE_LEVEL_DEFAULT
346+ );
347+ direct2DHandler.direct2DFactory ->CreateDCRenderTarget (&renderTargetProperties, &renderTarget);
345348 DBGASSERT (renderTarget != nullptr );
346349}
347350
351+ void Direct2DContext::CreateOffscreenContext ()
352+ {
353+ DeleteObject (memoryBitmap);
354+ DeleteDC (memoryDC);
355+
356+ HDC hdc = GetDC (NULL );
357+ memoryDC = CreateCompatibleDC (hdc);
358+ memoryBitmap = CreateCompatibleBitmap (hdc, width, height);
359+ SelectObject (memoryDC, memoryBitmap);
360+
361+ RECT clientRect = { 0 , 0 , width, height };
362+ renderTarget->BindDC (memoryDC, &clientRect);
363+ }
364+
348365}
0 commit comments