0

我将一些属于某些对象的符号绘制到设备上下文中,现在希望以后能够测试鼠标光标是否在这样的符号上方。

为此,我的计划是首先创建一个CDC路径并使用它来创建一个CRgn区域对象。

pDC->BeginPath();
pDC->Ellipse(ellipse[0], ellipse[1], ellipse[2], ellipse[3]); // Create path only
pDC->EndPath();

// Actually draw the ellipse
pDC->StrokeAndFillPath(); // Apparently removes the path from the DC

CRgn region;
if (region.CreateFromPath(pDC)) // Would also remove the path from the DC
{
    // We never get here :-/

    // Here I would copy the region's data,
    // attach it to the object being drawn and
    // destroy the region.
    // That way I can create a region later on and do the hit-testing.
}

我怎样才能同时使用路径,绘制和创建区域,而不必绘制两次?绘制两次几乎使我的绘制方法所花费的时间翻了一番,这是我想避免的。

4

1 回答 1

1

region.CreateFromPath调用 StrokeAndFillPath 之前。这会将路径保存为区域 - 然后您可以 StrokeAndFillPath,然后使用该区域。

编辑:哎呀,完全正确。幸运的是,似乎也有解决该问题的方法:尽管文档没有直接说明,但 SaveDC/RestoreDC 似乎与其他“东西”一起保存并恢复了路径,因此您可以执行以下操作:

pDC->BeginPath();
pDC->Ellipse(ellipse[0], ellipse[1], ellipse[2], ellipse[3]);
pDC->EndPath();

int dc_id = pDC->SaveDC();

pDC->StrokeAndFillPath();

pDC->RestoreDC(dc_id);

CRgn region;
if (region.CreateFromPath(pDC))
{
            // Now we do get here...
    MessageBox(L"Region Created");
}
于 2009-10-23T16:02:37.133 回答