Rufen Sie 0x887a0022 für CreateHwndRenderTarget mit Direct2D in UWP abC++

Programme in C++. Entwicklerforum
Guest
 Rufen Sie 0x887a0022 für CreateHwndRenderTarget mit Direct2D in UWP ab

Post by Guest »

Ich versuche, ein Win32-Hwnd zu erstellen und dann Direct2D zum Zeichnen darauf zu verwenden (in einer UWP-App), aber ich erhalte 0x887a0022 mit dem Aufruf d2dFactory->CreateHwndRenderTarget.
Derselbe Code funktioniert auf einer normalen Win32-App. Ich weiß nicht, was für UWP spezifisch ist, da ich die fehlenden eingefügt habe lib.
Fügen Sie einfach einen Aufruf der Show()-Funktion in einem Button.Click-Ereignis in MainPage.xaml hinzu, und das ist die Durchführung. Der Einfachheit halber ignorieren Sie bitte, dass kein RAII-Muster verwendet wird

Code: Select all

#include "pch.h"
#include "D2DWindow.h"
#include 

#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "user32.lib")

ID2D1Factory* d2dFactory{};
ID2D1SolidColorBrush* redBrush{};
ID2D1HwndRenderTarget* renderTarget{};
ID2D1PathGeometry* trianglePath{};
ID2D1GeometrySink* sink{};

inline void AssertHResult(HRESULT hr)
{
bool const succeed = SUCCEEDED(hr);
assert(succeed);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
{
AssertHResult(D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2dFactory));
RECT clientRect{};
GetClientRect(hwnd, &clientRect);
AssertHResult(d2dFactory->CreateHwndRenderTarget(  //error here
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
{
.width = static_cast(clientRect.right - clientRect.left),
.height = static_cast(clientRect.bottom - clientRect.top)
}
),
&renderTarget
));
AssertHResult(renderTarget->CreateSolidColorBrush(D2D1::ColorF{ D2D1::ColorF::Red }, &redBrush));
AssertHResult(d2dFactory->CreatePathGeometry(&trianglePath));
AssertHResult(trianglePath->Open(&sink));
D2D1_POINT_2F const points[]
{
{0.f, 400.f},
{400.f, 400.f},
{200.f, 0.f}
};
sink->BeginFigure(points[0], D2D1_FIGURE_BEGIN::D2D1_FIGURE_BEGIN_FILLED);
sink->AddLines(points + 1, std::size(points) - 1);
sink->EndFigure(D2D1_FIGURE_END::D2D1_FIGURE_END_CLOSED);
sink->Close();
}
case WM_SIZE:
if (renderTarget)
renderTarget->Resize({ .width = LOWORD(lparam), .height = HIWORD(lparam) });
return 0;
case WM_PAINT:
renderTarget->BeginDraw();
renderTarget->Clear(D2D1::ColorF{ D2D1::ColorF::White });
renderTarget->FillGeometry(trianglePath, redBrush);
AssertHResult(renderTarget->EndDraw());
ValidateRect(hwnd, nullptr);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}

}

void D2DWindow::Create()
{
WNDCLASS const windowClass
{
.lpfnWndProc = WindowProc,
.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH),
.lpszClassName = L"UWP Win32"
};
RegisterClass(&windowClass);

int const width = 800;
int const height = 600;
HWND const hwnd = CreateWindowEx(
0,
windowClass.lpszClassName,
L"UWP Win32 window",
WS_OVERLAPPEDWINDOW,
0, 0,
width, height,
nullptr,
nullptr,
nullptr,
nullptr
);
ShowWindow(hwnd, SW_SHOW);
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post