Advertisement

D2D coordinates not matching screen coordinates

Started by April 04, 2020 12:52 AM
1 comment, last by Enzio599 4 years, 10 months ago

I am using D2D with D3D11. I have some code that uses GetCursorpos() from the windows API which is then converted to client coordinates and then draws a small circle at this position using D2D FillEllipse(). The screen to client coordinates work perfectly but for some reason D2D draws the circle a small distance from the expected location (tens of pixels) as if the coordinate had been scaled by a small factor so that the error gets larger as the circle is drawn further from (0, 0). I noticed changing the dpi for the D2D1_RENDER_TARGET_PROPERTIES affects this 'scaling' so I suspect the problem has something to do with dpi. I want to know how I can fix my code so that it will draw the circle at the position given by GetCursorPos().

This is the code for creating the D2D render target from the DXGI surface I obtained from the swapchain in my D3D11 code. Here, dpiX and dpiY become 96 which I notice is also the constant that GetDpiForWindow() from the windows API returns when it is not dpi aware.

// Create render target
    float dpiX, dpiY;
    this->factory->GetDesktopDpi(&dpiX, &dpiY);
    D2D1_RENDER_TARGET_PROPERTIES rtDesc = D2D1::RenderTargetProperties(
        D2D1_RENDER_TARGET_TYPE_HARDWARE,
        D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
        dpiX,
        dpiY
    );
    AssertHResult(this->factory->CreateDxgiSurfaceRenderTarget(
        surface.Get(),
        &rtDesc,
        &this->renderTarget
    ), "Failed to create D2D render target");

try to map this offset of position by setting currect render target transform….

This topic is closed to new replies.

Advertisement