gpt4 book ai didi

c++ - 如何不丢弃 CDC 路径?

转载 作者:行者123 更新时间:2023-11-28 08:31:54 25 4
gpt4 key购买 nike

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

为此,我的计划是首先创建一个 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.
}

如何使用路径来绘制和创建区域,而不必绘制两次?绘制两次几乎使我在绘图方法上花费的时间翻倍,这是我想避免的事情。

最佳答案

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

关于c++ - 如何不丢弃 CDC 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1614187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com