【问题标题】:DrawText DT_CALCRECT and SetWorldTransformDrawText DT_CALCRECT 和 SetWorldTransform
【发布时间】: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

【问题讨论】:

    标签: c++ windows transform


    【解决方案1】:

    我找到了答案!

    DrawText 似乎不适用于缩放的各向同性视图,但 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 做的事情,但绘制它正确对齐并应对视口与窗口的缩放。

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 2023-03-26
      • 2011-07-15
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多