D2D 坐标与屏幕坐标不匹配

D2D coordinates not matching screen coordinates

我正在使用 D2D 和 D3D11。我有一些代码使用 windows API 中的 GetCursorpos(),然后将其转换为客户端坐标,然后使用 D2D FillEllipse() 在此位置绘制一个小圆圈。屏幕到客户端的坐标工作得很好,但由于某种原因,D2D 绘制的圆与预期位置(数十个像素)的距离很小,就好像坐标已按小比例缩放,因此随着圆的进一步绘制,误差会变大从 (0, 0)。 我注意到更改 D2D1_RENDER_TARGET_PROPERTIES 的 dpi 会影响此 'scaling' 所以我怀疑问题与 dpi 有关。这是我从 D3D11 代码中的交换链获得的 DXGI 表面创建 D2D 渲染目标的代码。

    // 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");

在这里,dpiX 和 dpiY 变为 96,我注意到它也是 windows API returns 中的 GetDpiForWindow() 的常量,当它不知道 dpi 时。

我想知道如何修复我的代码,以便它在 GetCursorPos() 给定的位置绘制圆。

更多相关代码:

Driver代码

    Vector3f cursPos = input.GetCursorPos();
    DrawCircle(Colour::Green, cursPos.x, cursPos.y, 3/*radius*/);

输入

    POINT pt{};
    ::GetCursorPos(&pt);
    // Convert from screen pixels to client pixels
    return ConvertPixelSpace(this->hWnd, (float)pt.x, (float)pt.x, PixelSpace::Screen, PixelSpace::Client);

Direct2D

void DrawCircle(const Colour& c, float centreX, float centreY, float radius, PixelSpace ps)
{
    Vector3f centre = ConvertPixelSpace(this->gfx.hWnd, centreX, centreY, ps, PixelSpace::Client);
    centreX = centre.x;
    centreY = centre.y;
    D2D1_ELLIPSE el{};
    el.point.x = centreX;
    el.point.y = centreY;
    el.radiusX = radius;
    el.radiusY = radius;
    auto brush = this->CreateBrush(c);
    this->renderTarget->FillEllipse(
        &el,
        brush.Get()
    );
}

像素空间转换

Vector3f ConvertPixelSpace(HWND hWnd, float x, float y, PixelSpace curSpace, PixelSpace newSpace)
{
    RECT rc = GetClientRectOfWindow(hWnd);
    struct
    {
        float top, left, width, height;
    } rectf;
    rectf.top = static_cast<float>(rc.top);
    rectf.left = static_cast<float>(rc.left);
    rectf.width = static_cast<float>(rc.right - rc.left);
    rectf.height = static_cast<float>(rc.bottom - rc.top);

    // Convert to client space
    if (curSpace == PixelSpace::Screen)
    {
        x -= rectf.left;
        y -= rectf.top;
    }

    // Convert to new space
    if (newSpace == PixelSpace::Screen)
    {
        x += rectf.left;
        y += rectf.top;
    }

    return Vector3f(x, y);
}

RECT GetClientRectOfWindow(HWND hWnd)
{
    RECT rc;
    ::GetClientRect(hWnd, &rc);
    // Pretty sure these are valid casts.
    // rc.top is stored directly after rc.left and this forms a POINT struct
    ClientToScreen(hWnd, reinterpret_cast<POINT*>(&rc.left));
    ClientToScreen(hWnd, reinterpret_cast<POINT*>(&rc.right));
    return rc;
}

问题是我使用 window 区域而不是客户区域创建 D3D11 交换链。

    RECT rect{};
    GetWindowRect(hWnd, &rect); // !!! This should be GetClientRect()
    this->width = rect.right - rect.left;
    this->height = rect.bottom - rect.top;

    DXGI_SWAP_CHAIN_DESC scDesc{};
    scDesc.BufferDesc.Width = width;
    scDesc.BufferDesc.Height = height;
    //...