请教下文本有很多段组成,每段有不同颜色,如何输出比较好。

rageliu 2017-06-20 08:00:20
比如“上山老虎”。
中间有可能换行,比如
上山打老虎,
红薯”。

用gdi+画了下,效果不太好。大家有啥建议么
...全文
296 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-06-21
  • 打赏
  • 举报
回复
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. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. Import Library: Use user32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also Fonts and Text Overview, Font and Text Functions, GrayString, TabbedTextOut, TextOut, RECT
schlafenhamster 2017-06-21
  • 打赏
  • 举报
回复
多行 带颜色

// StepText(hdc,&rect,"haha 你好!\r\nhaha 你也好!\r\n\r\nhaha 他好!\r\nhaha 她也好!");
void StepText(HDC hdc,RECT *rc,char *str)//文字逐字显示
{
int len=strlen(str);//计算字符串的长度
int posx;//当前字符位置
int posy=rc->top;
SIZE size;//存储每个字符的宽高
SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//设置自动更新文字位置
while(len>0)
{
posx=rc->left;
MoveToEx(hdc,rc->left, posy,NULL);//移动到行首
while(len>=0)
{
int t;
if((str[0])<0) {t=2;SetTextColor(hdc,255);}//中西字符辨别
else {t=1;SetTextColor(hdc,0);}
GetTextExtentPoint32(hdc,str,t,&size);//获取字符宽高
if(posx+size.cx>rc->right) break;//若超出边界
posx+=size.cx;//更新当前位置
if(*str==0x0D){ str+=1; continue;}
if(*str==0x0A){ str+=1; len=strlen(str); break;}
TextOut(hdc,0,0,str,t);//输出字符
str+=t;//更新字符
len-=t;//更新字符长度
if(len<=0) break;//若字符输出完毕
} // end while(len>=0)
// newline:
posy+=size.cy; //进行移行处理
if((posy+size.cy+ size.cy)>=rc->bottom)//下一行无法书写,进行滚屏
{
RECT rect=*rc;
rect.top+=size.cy;//滚屏位置
ScrollDC(hdc,0,-size.cy,&rect,NULL,NULL,NULL);
posy-=size.cy; //恢复移行处理
}
}
}


躺着睡的蜗牛 2017-06-20
  • 打赏
  • 举报
回复
假如字符串为 “上山打老虎,下地挖红薯” 窗口宽度为7个字符 1. 根据窗口宽度分隔字符串 字符串变为 字符串1: “上山打老虎,“ 字符串2: ”挖红薯“ 2. 根据颜色再次分割字符串 字符串变为: 字符串1-1: “上山” 字符串1-2: “打老虎,” 字符串1-3: “” 字符串2-1: “” 字符串2-2: ”挖红薯“ 3. 按顺序依次描画字符串
schlafenhamster 2017-06-20
  • 打赏
  • 举报
回复
如果是汉字,宽和高 是一样的,只要算一次
rageliu 2017-06-20
  • 打赏
  • 举报
回复
比如“下地”两字的时候,跨行了,绘制的RECT等如何处理
rageliu 2017-06-20
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
不必“得计算每个字符的位置” 但要 改变 Pen
改变pen没问题,如何做呢,比如这样的 “上山打老虎, 挖红薯”
worldy 2017-06-20
  • 打赏
  • 举报
回复
如果简单的显示,使用普通窗口,通过DC输出,输出前使用SetTextColor设置文本颜色(必须使用DC_PEN) 如果要复杂显示,并且可以编辑,则使用CRichCtrl,内容中包含着显示格式的文本控制参数
schlafenhamster 2017-06-20
  • 打赏
  • 举报
回复
不必“得计算每个字符的位置” 但要 改变 Pen
rageliu 2017-06-20
  • 打赏
  • 举报
回复
逐字 输出可以,得计算每个字符的位置等信息
schlafenhamster 2017-06-20
  • 打赏
  • 举报
回复
每个字 都配颜色,逐字 输出。
rageliu 2017-06-20
  • 打赏
  • 举报
回复
多谢回复,这是个方式。 如果通过HDC处理的话,有直接能绘制的方式么
zgl7903 2017-06-20
  • 打赏
  • 举报
回复
CRichEditCtrl SetSelectionCharFormat
schlafenhamster 2017-06-20
  • 打赏
  • 举报
回复
注意 SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//设置自动更新文字位置 及 TextOut(hdc,0,0,str,t);//输出字符
schlafenhamster 2017-06-20
  • 打赏
  • 举报
回复
试试 单行的:

void StepText(HDC hdc,RECT *rc,char *str)//文字逐字显示 
{ 
  int len=strlen(str);//计算字符串的长度 
  int posx;//当前字符位置 
  int posy=rc->top; 
  SIZE size;//存储每个字符的宽高 
  SetTextAlign(hdc,TA_UPDATECP|TA_LEFT|TA_TOP);//设置自动更新文字位置 
  while(len>0) 
  { 
    posx=rc->left; 
    MoveToEx(hdc,rc->left, posy,NULL);//移动到行首 
    while(len>=0) 
    { 
      int t; 
      if((str[0])<0) t=2;//中西字符辨别 
      else			 t=1; 
      GetTextExtentPoint32(hdc,str,t,&size);//获取字符宽高 
      if(posx+size.cx>rc->right) break;//若超出边界 
      posx+=size.cx;//更新当前位置 
      TextOut(hdc,0,0,str,t);//输出字符 
      str+=t;//更新字符 
      len-=t;//更新字符长度 
      if(len<=0) break;//若字符输出完毕 
    } 
    posy+=size.cy; //进行移行处理 
    if((posy+size.cy+ size.cy)>=rc->bottom)//下一行无法书写,进行滚屏 
    { 
      RECT rect=*rc; 
      rect.top+=size.cy;//滚屏位置 
      ScrollDC(hdc,0,-size.cy,&rect,NULL,NULL,NULL); 
      posy-=size.cy; //恢复移行处理 
    } 
  } 
}
rageliu 2017-06-20
  • 打赏
  • 举报
回复
恩,明白楼上的意思,多谢
计算机应用基础 1 计算机应用基础课程组 计算机应用基础项目化教程—第2模块全文共32页,当前为第1页。 2023/5/31 计算机应用基础 2 模块二 操作系统Windows 7 项目一 如何布置Windows桌面环境 计算机应用基础项目化教程—第2模块全文共32页,当前为第2页。 2023/5/31 计算机应用基础 3 小张是新入学的大学生,上中学时就学习过计算机,因此他对大学里学习的"计算机应用基础"并没有太在意,觉得很简单。但是当他看到学校下发的计算机应用课程作业后,发现要设置Windows7桌面,要求比原来想象的复杂的多。例如,设置"开始"菜单和"任务栏"、定制桌面项目等,这些知识是小张从前没有遇到过的,于是他去请教王老师。如何才能布置一个适合自己的windows桌面环境呢? 项目提出 计算机应用基础项目化教程—第2模块全文共32页,当前为第3页。 2023/5/31 计算机应用基础 4 王老师说,小张遇到的问题很普遍。如何能更好更快地使用Windows7,首先要根据自己的使用习惯和工作需要设置好使用环境。这需要了解桌面图标设置、"开始"菜单和"任务栏"、屏幕分辨率等操作知识。对很多用户来说,Windows7默认配置的桌面环境用起来不太方便,其实只需稍加设置,就能为自己量身订做一个Windows7桌面环境。 项目分析 计算机应用基础项目化教程—第2模块全文共32页,当前为第4页。 2023/5/31 计算机应用基础 5 1.桌面图标 操作系统安装完成后,会自动进入Windows7桌面。桌面图标包括"计算机"、"网络"、"回收站"、"用户的文件"等。通过这些图标,可以方便的对计算机进行操作和管理。除了Windows7自带图标外,也可以在桌面上添加应用程序快捷方式。 2. 计算机主题设置 计算机主题指计算机用户界面的视觉效果和系统声音,它包含桌面背景、窗口颜色、声音、屏幕保护等,可对主题进行整体更换,也可以只更改主题的某一部分。 相关知识点: 计算机应用基础项目化教程—第2模块全文共32页,当前为第5页。 2023/5/31 计算机应用基础 6 3.屏幕分辨率 屏幕分辨率指屏幕图像的精密度,也即显示器所能显示的像素数,以水平和垂直像素来衡量。屏幕分辨率低时(如800*600),在屏幕上显示的项目少,但尺寸比较大。屏幕分辨率高时(如1600*1200),在屏幕上显示的项目多,但尺寸比较少。 4.任务栏操作 任务栏是显示在桌面底部的水平长条,主要由4部分组成,即快速启动区、程序按钮区、语言栏和通知区域,主要用于显示程序的快速启动和当前运行的所有任务。 5."开始"菜单操作 "开始"菜单用于存放操作系统或设置系统的绝大多数命令,而且还可以通过"开始"菜单使用安装到当前系统中的所有程序。 计算机应用基础项目化教程—第2模块全文共32页,当前为第6页。 2023/5/31 计算机应用基础 7 任务一:更改桌面图标 任务二:计算机主题个性化 任务三:设置屏幕分辨率 计算机应用基础项目化教程—第2模块全文共32页,当前为第7页。 2023/5/31 计算机应用基础 8 拓展提高: 1. 常用快捷操作方式 打开窗口菜单的快捷键为Alt+菜单名后的英文字母。如打开"文件(F)"菜单的快捷键是[Alt+F]。 关闭当前窗口的快捷键为[Alt+F4]。 整个屏幕图像 截屏的快捷键是[Print Screen]。 活动窗口图像截屏的快捷键是[Alt +Print Screen]。 计算机应用基础项目化教程—第2模块全文共32页,当前为第8页。 2023/5/31 计算机应用基础 9 小张是毕业班的学生,快要毕业了,老师布置了3000字的论文。小张查询并上网下载了很多资料到计算机上,结果却找不到自己下载的资料保存在哪里。为什么会出现这种情况呢?计算机中的文件很多、很乱,这是重要原因。因此,学习管理计算机中的各种资源十分必要。 项目提出 计算机应用基础项目化教程—第2模块全文共32页,当前为第9页。 2023/5/31 计算机应用基础 10 老师告诉小张,在计算机系统中,计算机信息是以文件的形式保存的,用户所做的工作都是围绕文件展开的。这些文件包括操作系统文件、应用程序文件、文本文件等,它们根据不同的类别存储在磁盘上不同的文件夹中。利用计算机对文件进行管理,和文件柜存放文件道理是一样的。如果将文件柜比作硬盘,那么文件柜的抽屉就是文件夹,抽屉里的纸就是文件。因此,只要在硬盘上根据需要创建不同名称的文件夹,再将文件存放到相应的文件夹中即可。 项目分析 计算机应用基础项目化教程—第2模块全文共32页,当前为第10页。 2023/5/31 计算机应用基础 11 1.文件和文件夹 文件是磁盘上存储信息的基本单位。将一系列相关的信息(这些信息可以是文字、图像、
微信小程序系统教程[初级阶],微信小程序0基础学起,讲解微信小程序开发的基础知识。微信小程序系统教程共有“微信小程序系统教程[初级阶]”、“微信小程序系统教程[中级阶]——核心技术”、“微信小程序系统教程[阶]客服消息+微信支付+九宝电商系统”。“微信小程序系统教程[阶]全套课程”包含:1.微信小程序系统教程[阶]_客服消息2.微信小程序系统教程[阶]_微信支付3.微信小程序系统教程[阶]_九宝电商系统学习“微信小程序系统教程[阶]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶]”、“微信小程序系统教程[中级阶]”,后在报名“微信小程序系统教程[阶]”。阶讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,没有使用三方技术。这样降低同学们学习成本。微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。“微信小程序系统教程[中级阶]——核心技术”重点讲解微信小程序事件、组件、API微信小程序系统教程[初级阶],微信小程序0基础学起,讲解微信小程序开发的基础知识。购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。购课请咨询qq2326321088

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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