GDI的DrawText多行输出,空间很浪费问题

凌乱哥 2017-10-23 02:20:45
同样的文本,同样的区域范围
只是一个单行输出,一个多行输出
很明显,多行输出中每行后面很大一块地方都没用上,怎么能让它不那么快就换行呢


CPaintDC dc(this);
CString str = _T("8618826252112@s.whatsapp.net; 8618825078258@s.whatsapp.net; 8618126258615@s.whatsapp.net");

CRect rc1(50, 50, 440, 100), rc2(50, 150, 440, 200);
dc.Rectangle(rc1);
dc.DrawText(str, rc1, DT_LEFT);
dc.Rectangle(rc2);
dc.DrawText(str, rc2, DT_LEFT|DT_EDITCONTROL|DT_WORDBREAK);


...全文
829 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2017-10-26
  • 打赏
  • 举报
回复
引用 3 楼 dingxz105090 的回复:
[quote=引用 2 楼 worldy 的回复:] DT_WORDBREAK 一个词如果计算后,超出范围,比如一个字母超出,则当前行会在这个词前截断,这个词会显示到下一行,这个词减一个字母的宽度就浪费 如果显示的是中文,则不会在截断,汉字应该是以字为单位计算的
以“词”为单位,那么第一行也应该是 8618826252112@s.whatsapp.net; 8618825078258@s. 吧 那不用DT_WORDBREAK参数,应该选用别的参数吗?或者像一楼说的那样,只能自己计算并绘制[/quote]对于英文内容,系统的计算方式应该是标点之后有空格,才算一个英文词汇结束。而不是遇到一个英文逗号 , 或英文句号 . 都认为是结束。
赵4老师 2017-10-25
  • 打赏
  • 举报
回复
zgl7903 2017-10-24
  • 打赏
  • 举报
回复
自己计算输出 或者使用EDIT的多行属性
worldy 2017-10-23
  • 打赏
  • 举报
回复
DrawText The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth). int DrawText( HDC hDC, // handle to device context LPCTSTR lpString, // pointer to string to draw int nCount, // string length, in characters LPRECT lpRect, // pointer to struct with formatting dimensions UINT uFormat // text-drawing flags ); Parameters hDC Handle to the device context. lpString Pointer to the string to be drawn. If the nCount parameter is –1, the string must be null-terminated. If uFormat includes DT_MODIFYSTRING, the function could add up to four additional characters to this string. The buffer containing the string should be large enough to accommodate these extra characters. nCount Specifies the number of characters in the string. If nCount is –1, then the lpString parameter is assumed to be a pointer to a null-terminated string and DrawText computes the character count automatically. lpRect Pointer to a RECT structure that contains the rectangle (in logical coordinates) in which the text is to be formatted. uFormat Specifies the method of formatting the text. It can be any combination of the following values: Value Description DT_BOTTOM Justifies the text to the bottom of the rectangle. This value must be combined with DT_SINGLELINE. DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text but does not draw the text. DT_CENTER Centers text horizontally in the rectangle. DT_EDITCONTROL Duplicates the text-displaying characteristics of a multiline edit control. Specifically, the average character width is calculated in the same manner as for an edit control, and the function does not display a partially visible last line. DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified. You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash. DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight. DT_EXTERNALLEADING Includes the font external leading in line height. Normally, external leading is not included in the height of a line of text. DT_INTERNAL Uses the system font to calculate text metrics. DT_LEFT Aligns text to the left. DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified. DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used. DT_NOPREFIX Turns off processing of prefix characters. Normally, DrawText interprets the mnemonic-prefix character & as a directive to underscore the character that follows, and the mnemonic-prefix characters && as a directive to print a single &. By specifying DT_NOPREFIX, this processing is turned off. DT_RIGHT Aligns text to the right. DT_RTLREADING Layout in right to left reading order for bi-directional text when the font selected into the hdc is a Hebrew or Arabic font. The default reading order for all text is left to right. DT_SINGLELINE Displays text on a single line only. Carriage returns and linefeeds do not break the line. DT_TABSTOP Sets tab stops. Bits 15–8 (high-order byte of the low-order word) of the uFormat parameter specify the number of characters for each tab. The default number of characters per tab is eight. DT_TOP Top-justifies text (single line only). DT_VCENTER Centers text vertically (single line only). DT_WORDBREAK Breaks words. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by the lpRect parameter. A carriage return-linefeed sequence also breaks the line. DT_WORD_ELLIPSIS Truncates text that does not fit in the rectangle and adds ellipses. Note The DT_CALCRECT, DT_EXTERNALLEADING, DT_INTERNAL, DT_NOCLIP, and DT_NOPREFIX values cannot be used with the DT_TABSTOP value. Return Values If the function succeeds, the return value is the height of the text. If the function fails, the return value is zero. Windows NT: To get extended error information, callGetLastError. Remarks The DrawText function uses the device context's selected font, text color, and background color to draw the text. Unless the DT_NOCLIP format is used, DrawText clips the text so that it does not appear outside the specified rectangle. All formatting is assumed to have multiple lines unless the DT_SINGLELINE format is specified. If the selected font is too large for the specified rectangle, the DrawText function does not attempt to substitute a smaller font. Windows CE: If DT_CALCRECT is specified for the uFormat parameter, you must set the right and bottom members of the RECT structure pointed to by lpRect. Windows CE does not support the DT_EXTERNALLEADING flag for the uFormat parameter.
schlafenhamster 2017-10-23
  • 打赏
  • 举报
回复
DT_CALCRECT 先 计算
凌乱哥 2017-10-23
  • 打赏
  • 举报
回复
引用 2 楼 worldy 的回复:
DT_WORDBREAK 一个词如果计算后,超出范围,比如一个字母超出,则当前行会在这个词前截断,这个词会显示到下一行,这个词减一个字母的宽度就浪费 如果显示的是中文,则不会在截断,汉字应该是以字为单位计算的
以“词”为单位,那么第一行也应该是 8618826252112@s.whatsapp.net; 8618825078258@s. 吧 那不用DT_WORDBREAK参数,应该选用别的参数吗?或者像一楼说的那样,只能自己计算并绘制
worldy 2017-10-23
  • 打赏
  • 举报
回复
DT_WORDBREAK 一个词如果计算后,超出范围,比如一个字母超出,则当前行会在这个词前截断,这个词会显示到下一行,这个词减一个字母的宽度就浪费 如果显示的是中文,则不会在截断,汉字应该是以字为单位计算的
xiaohuh421 2017-10-23
  • 打赏
  • 举报
回复
DT_WORDBREAK 这个会以单词来截断换行. 要完美的做到这个, 只能自己计算了. 这个不只是换行问题, 当你字体变大之后, 你会发现行间距也有很大问题. 解决办法都只有一个, 那就是自己计算并绘制.

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧