我有以下代码:

XFORM xForm;

SetGraphicsMode(dc, GM_ADVANCED);
SetMapMode(dc, MM_ISOTROPIC);
SetWindowExtEx(dc, 1000, 1000, nullptr);
SetViewportExtEx(dc, 1000, 1000, nullptr);

auto radians = rotation * PI / 180.0f;

xForm.eM11 = cosf(radians);
xForm.eM12 = sinf(radians);
xForm.eM21 = -xForm.eM12;
xForm.eM22 = xForm.eM11;
xForm.eDx = (FLOAT)x;
xForm.eDy = (FLOAT)y;
SetWorldTransform(dc, &xForm);

MoveToEx(dc, x - 100, y, nullptr);
LineTo(dc, x + 100, y);
MoveToEx(dc, x, y - 100, nullptr);
LineTo(dc, x, y + 100);

RECT rect;
rect.left = 0;
rect.right = 10000;
rect.top = 0;
rect.bottom = 10000;
DrawText(dc, text, -1, &rect, DT_CALCRECT);

auto width = rect.right - rect.left;
auto height = rect.bottom - rect.top;

rect.left = x - width / 2;
rect.right = rect.left + width;
rect.top = y - height / 2;
rect.bottom = rect.top + height;
DrawText(dc, text, -1, &rect, DT_TOP | DT_CENTER);

它旨在绘制居中并旋转90度的文本。问题是窗口ext与视口(viewport)ext之比。如果它们相同(即匹配),那么我会看到两行文本正确居中。

如果将视口(viewport)设置为500,500,则线条画得很好,但文字消失了。现在,我不知道在调用DrawText(... DT_CALCRECT ...)之前应将rect设置为什么,但是我猜是很大的东西,所以我尝试了10000个单位的大小,但这没什么区别。

如何使DrawText与其他所有元素一起使用和缩放?它为旋转做正确的事情,而不是窗口/视口(viewport)比率。

完整的Visual C++项目在这里:https://github.com/imekon/SampleTransform

最佳答案

我找到了答案!

DrawText似乎不适用于缩放的各向同性 View ,但是TextOut可以工作,因此,代码在其中使用DrawText输出文本,即在这里:

rect.left = x - width / 2;
rect.right = rect.left + width;
rect.top = y - height / 2;
rect.bottom = rect.top + height;
DrawText(dc, text, -1, &rect, DT_TOP | DT_CENTER);

替换为:
TextOut(dc, x - width / 2, y - height / 2, text, _tcslen(text));

这可以实现DrawText所期望的功能,但是可以正确对齐它,并可以处理视口(viewport)与窗口的缩放比例。

08-26 19:44