自动换行 Win32

发布于 2024-07-17 21:49:04 字数 216 浏览 5 评论 0原文

我正在尝试对一段文本进行自动换行,以便在窗口中显示,否则该文本块会太长。 我使用的字体不是固定宽度字体,因此 GetSystemMetrics 将不会返回我的文本的准确值。

这里不建议使用static窗口,因为static窗口不会告诉我一条重要信息:自动换行后绘制的文本的高度。

有任何想法吗?

比利3

I'm trying to wordwrap a block of text for display in a window that would otherwise be too long. The font I'm using is not a fixed width font, so GetSystemMetrics will not return accurate values for my text.

Using a static window is not an option here because a static window doesn't tell me one crucial piece of information: The height of the text drawn after wordwrapping.

Any ideas?

Billy3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与酒说心事 2024-07-24 21:49:04

查看 Win32 API 调用 DrawtextEx。 您需要传递DT_CALCRECT选项,告诉Windows您希望计算矩形。

Check out the Win32 API call DrawtextEx. You'll need to pass the DT_CALCRECT option, telling Windows that you wish for the rectangle to be calculated.

如此安好 2024-07-24 21:49:04

您可以使用DrawText() 设置了 DT_CALCRECT 标志的 API 函数。 您需要首先为 HDC 选择正确的字体。

You could use the DrawText() API function with the DT_CALCRECT flag set. You would need to select the correct font for the HDC first.

丢了幸福的猪 2024-07-24 21:49:04

如果整个文本要以单一字体显示,DrawTextEx() 将执行您想要和需要的一切。

如果您需要混合字体,则必须自己完成这项工作。 在这种情况下,您需要查看诸如 GetTextMetrics()(不是 GetSystemMetrics())和 GetTextExtentExPoint32() 之类的 API 来弄清楚每次运行的位置和大小。

如果您需要处理复杂的脚本(例如,从右到左的语言和字母根据上下文改变形状的脚本),您将需要 Uniscribe。 注意:Uniscribe 功能强大,但级别很低。 用更高级别的接口来包装它可能需要大量的工作。 如果您需要复杂的脚本处理,那么最好使用浏览器控件。

DrawTextEx() will do everything you want and need if the entire text is to be displayed in a single font.

If you need to mix fonts, you'll have to do the work yourself. In that case, you'll want to look at APIs like GetTextMetrics() (not GetSystemMetrics()) and GetTextExtentExPoint32() to figure out positions and sizes for each run.

If you need to handle complex scripts (e.g., right-to-left languages and scripts where the letters change shape depending on context), you'll need Uniscribe. Caution: Uniscribe is powerful but very low level. It can take a lot of work to wrap it with a higher-level interface. If you need complex script handling, you might be better off using a browser control.

人疚 2024-07-24 21:49:04

DrawText()DrawTextEx() 都可以实现这一点。 关键是在format参数中使用DT_WORDBREAK

要在顶部的 100 x 100 矩形内绘制文本,可以使用以下任一方法:

    RECT textRect;
    SetRect(&textRect, 0, 0, 100, 100);
    DrawText(hdc,TEXT("THIS IS TEXT THAT WILL PROBABLY WRAP AROUND A 100 BY 100 RECTANGLE. BUT YOU CAN'T BE SURE UNLESS YOU TRY."),-1,&textRect, DT_LEFT | DT_NOCLIP | DT_WORDBREAK);

DT_LEFT 选项将句子向左对齐。 DT_NOCLIP 确保底部的文本不会被剪切。

或者

使用DrawTextEx():

    RECT textRect;
    SetRect(&textRect, 0, 0, 100,100);
    DrawTextEx(hdc,(LPSTR)TEXT("THIS IS TEXT THAT WILL PROBABLY WRAP AROUND A 100 BY 100 RECTANGLE. BUT YOU CAN'T BE SURE UNLESS YOU TRY."), -1, &textRect, DT_LEFT| DT_NOCLIP|DT_WORDBREAK , NULL);

Both DrawText() and DrawTextEx() can accomplish that. The key is to use DT_WORDBREAK in the format parameter.

To draw text within a 100 x 100 rectangle at the top let, one could use either:

    RECT textRect;
    SetRect(&textRect, 0, 0, 100, 100);
    DrawText(hdc,TEXT("THIS IS TEXT THAT WILL PROBABLY WRAP AROUND A 100 BY 100 RECTANGLE. BUT YOU CAN'T BE SURE UNLESS YOU TRY."),-1,&textRect, DT_LEFT | DT_NOCLIP | DT_WORDBREAK);

The DT_LEFT option aligns the sentences to the left. The DT_NOCLIP ensures that text isn't cut out at the bottom.

OR

Use DrawTextEx():

    RECT textRect;
    SetRect(&textRect, 0, 0, 100,100);
    DrawTextEx(hdc,(LPSTR)TEXT("THIS IS TEXT THAT WILL PROBABLY WRAP AROUND A 100 BY 100 RECTANGLE. BUT YOU CAN'T BE SURE UNLESS YOU TRY."), -1, &textRect, DT_LEFT| DT_NOCLIP|DT_WORDBREAK , NULL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文