【发布时间】:2019-04-04 13:42:19
【问题描述】:
我有以下代码:
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 度的文本。问题是窗口扩展与视口扩展的比率。如果它们相同,即匹配,那么我会看到两行文本正确居中。
如果我将视口设置为 500、500,线条画得很好,但文字消失了。现在我不知道在调用 DrawText(...DT_CALCRECT...) 之前我应该将 rect 设置为什么,但我猜测有些大,所以我尝试了 10000 个单位大小,但没有任何区别。
如何让 DrawText 与其他所有内容一起工作和缩放?它正在为旋转做正确的事情,而不是窗口/视口比率。
完整的 Visual C++ 项目在这里:https://github.com/imekon/SampleTransform
【问题讨论】: