C++开源界面库duilib的使用细节与实战技巧总结(实战经验分享)

时间:2024-10-04 07:29:01

目录

1、使用CEditUI编辑框

2、使用CLabelUI或CTextUI的Html文本效果

3、使用CTextUI控件对文字宽度自适应的特性

4、CRichEditUI富文本控件使用注意点

4.1、指定CRichEditUI加在2.0版本的库

4.2、解决向CRichEditUI中插入文字后显示空白的问题

5、设置窗口透明度的接口CPaintManagerUI::SetTransparent的问题

5.1、窗口透明度设置为255(不透明),之前添加的WS_EX_LAYERED风格被删除了

5.2、 先调用了SetLayeredWindowAttributes接口,导致后面调用UpdateLayeredWindow失败

6、弹出一个dui窗口的模态框时要将自销毁标记置为false

6.1、dui窗口对象的自销毁实现说明

6.2、自销毁特性引发的崩溃问题

7、CMenuWnd菜单窗口中的菜单项相关实现细节

7.1、设置菜单窗口属性以及构建菜单窗口中的菜单项

7.2、操作菜单项的其他接口

8、在格式化CStdString字符串变量时要注意的问题

8.1、将CStdString::Format接口中不支持浮点的wvsprintf接口换成_vstprintf_s

8.2、在格式化CStdString对象时遇到的崩溃问题 

9、常用布局CVerticalLayOutUI、CHorizontalLayoutUI和CTileLayoutUI的使用技巧

9.1、常用布局特性说明

9.2、使用布局去构建UI界面的实例

10、为了给字体设置提高文字清晰度的ClearType属性,_WIN32_WINNT宏的值从0x500修改成0x501,导致ToolTip窗口不显示的问题


C++软件异常排查从入门到精通系列教程(专栏文章列表,欢迎订阅,持续更新...)/chenlycly/article/details/125529931C/C++实战进阶(专栏文章已更新400多篇,持续更新中...)/chenlycly/category_11931267.htmlWindows C++ 软件开发从入门到精通(专栏文章,持续更新中...)/chenlycly/category_12695902.htmlVC++常用功能开发汇总(专栏文章列表,欢迎订阅,持续更新...)/chenlycly/article/details/124272585C++软件分析工具从入门到精通案例集锦(专栏文章,持续更新中...)/chenlycly/article/details/131405795开源组件及数据库技术(专栏文章,持续更新中...)/chenlycly/category_12458859.html网络编程与网络问题分享(专栏文章,持续更新中...)/chenlycly/category_2276111.html       项目中使用开源的duilib界面库已经好几年了,在使用的过程中发现了一些问题和缺陷,对duilib库进行部分优化和改进,并根据业务需求,新增了一些自定义的控件。本文罗列出使用duilib的若干细节和实战技巧,以供大家借鉴或参考。duilib界面库使用细节与实战技巧总结

1、使用CEditUI编辑框

       CEditUI编辑框支持设置有效字符串过滤,比如用于输入端口号的编辑框,只能输入数字,则可以将有效字符过滤串设置为_T("0123456789")。再比如输入密码的编辑框,只允许输入个别特殊字符以及数字、大小写字母等,可以将有效字符过滤串设置为_T("_.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")。
不属于这个有效字符过滤串之内的字符,将无法输入,这是CEditUI内部代码控制的。

       可以直接在xml中设置allowinputfilter属性,如下所示:

也可以在代码中调用CEditUI的SetAllowInputFilter接口去设置:

m_pEditPwd->SetAllowInputFilter( _T("_.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") );

       另外,对于密码编辑框,我们要设置成*号或者圆点这种不可见的模式,防止输入密码时被别人看到。需要做两个设置,一个是将编辑框设置成密码模式,可以在xml中设置属性password="true",也可以调用接口SetPasswordMode去设置。

       设置用于遮盖密码的固定字符,可以设置为*号,可以设置为实心圆点●,可以调用SetPasswordChar接口设置(也可以在xml中设置属性)。设置*号的代码如下:

m_pEditPwd->SetPasswordChar( _T('*') );

显示效果如下:

显示效果不是很好,现在很多软件都将掩藏字符设置为实心圆点●了,比如QQ的密码框:

设置实心圆点的代码如下:

m_pEditPwd->SetPasswordChar( _T('●') );  

实心圆点的效果如下:

对比看,还是实心圆点●的效果要更好一些的!

       这个实心圆点是个特殊字符,从哪里获取呢?可以在搜狗输入法的符号大全窗口中的特殊字符栏中可以找到,如下:


       在这里,给大家重点推荐一下我的几个热门畅销专栏,欢迎订阅:(博客主页还有其他专栏,可以去查看)

专栏1:(该精品技术专栏的订阅量已达到510多个,专栏中包含大量项目实战分析案例,有很强的实战参考价值,广受好评!专栏文章持续更新中,预计更新到200篇以上!欢迎订阅!)

C++软件调试与异常排查从入门到精通系列文章汇总/chenlycly/article/details/125529931

本专栏根据多年C++软件异常排查的项目实践,系统地总结了引发C++软件异常的常见原因以及排查C++软件异常的常用思路与方法,详细讲述了C++软件的调试方法与手段,以图文并茂的方式给出具体的项目问题实战分析实例(很有实战参考价值),带领大家逐步掌握C++软件调试与异常排查的相关技术,适合基础进阶和想做技术提升的相关C++开发人员!

考察一个开发人员的水平,一是看其编码及设计能力,二是要看其软件调试能力!所以软件调试能力(排查软件异常的能力)很重要,必须重视起来!能解决一般人解决不了的问题,既能提升个人能力及价值,也能体现对团队及公司的贡献!

专栏中的文章都是通过项目实战总结出来的,包含大量项目问题实战分析案例,有很强的实战参考价值!专栏文章还在持续更新中,预计文章篇数能更新到200篇以上!

专栏2:  

C++常用软件分析工具从入门到精通案例集锦汇总(专栏文章,持续更新中...)/chenlycly/article/details/131405795

常用的C++软件辅助分析工具有PE工具、Dependency Walker、Process Explorer、Process Monitor、API Monitor、Clumsy、Windbg、IDA Pro等,本专栏详细介绍如何使用这些工具去巧妙地分析和解决日常工作中遇到的问题,很有实战参考价值!

专栏3:(本专栏涵盖了多方面的内容,是当前重点打造的专栏,专栏文章已经更新到400多篇,持续更新中...)

C/C++实战进阶(专栏文章已更新380多篇,持续更新中...)/chenlycly/category_11931267.html

以多年的开发实战为基础,总结并讲解一些的C/C++基础与项目实战进阶内容,以图文并茂的方式对相关知识点进行详细地展开与阐述!专栏涉及了C/C++领域多个方面的内容,包括C++基础及编程要点(模版泛型编程、STL容器及算法函数的使用等)、C++11及以上新特性(不仅看开源代码会用到,日常编码中也会用到部分新特性,面试时也会涉及到)、常用C++开源库的介绍与使用、代码分享(调用系统API、使用开源库)、常用编程技术(动态库、多线程、多进程、数据库及网络编程等)、软件UI编程(Win32/duilib/QT/MFC)、C++软件调试技术(排查软件异常的手段与方法、分析C++软件异常的基础知识、常用软件分析工具使用、实战问题分析案例等)、设计模式、网络基础知识与网络问题分析进阶内容等。

专栏4:   

VC++常用功能开发汇总(专栏文章,持续更新中...)/chenlycly/article/details/124272585

将10多年C++开发实践中常用的功能,以高质量的代码展现出来。这些常用的高质量规范代码,可以直接拿到项目中使用,能有效地解决软件开发过程中遇到的问题。

专栏5: 

Windows C++ 软件开发从入门到精通(专栏文章,持续更新中...)/chenlycly/category_12695902.html

根据多年C++软件开发实践,详细地总结了Windows C++ 应用软件开发相关技术实现细节,分享了大量的实战案例,很有实战参考价值。


2、使用CLabelUI或CTextUI的Html文本效果

       比如要实现类似如下效果的文字块:

文字有不同大小,有不同颜色,使用CLabelUI或CTextUI的showhtml属性就可以实现这样的效果,将控件的showhtml属性设置为true即可,即showhtml="true"。

       要实现上述效果,要按文字大小和颜色对文字串分块处理,使用html属性对每一块文字设置不同的大小和颜色,上述截图中的文字效果的实现代码如下:

  1. // 1、姓名(c-设置颜色,f-设置字体)
  2. CString strName;
  3. // ...(获取人员的代码省略)
  4. CString strContactInfo;
  5. strContactInfo = _T("<c #323232><f 4>"); 
  6. strContactInfo += CopyUtf8ToCStringT( strName );
  7. strContactInfo += _T("</f></c>");
  8. // 2、部门信息(x-设置水平方向上的offset偏移,c-设置颜色,f-设置字体)
  9. CString strDepartmenName;
  10. // ...(获取部门名称的代码省略)
  11. CString strDepartmentInfo;
  12. strDepartmentInfo = _T("|   ") + strDepartmenName;
  13. strDepartmentInfo = _T("<x 14><c #969696><f 1>") + strDepartmentInfo + _T("</f></c>");
  14. strContactInfo += strDepartmentInfo;
  15. // 3、邮箱(n-设置换行,c-设置颜色,f-设置字体)
  16. CString strMail;
  17. // ...(获取邮箱的代码省略)
  18. strMail = _T("<n><c #969696><f 3>") + strMail + _T("</f></c>");
  19. strContactInfo += strMail;
  20. m_pContaceInfo->SetText( strContactInfo );

        至于html支持哪些属性设置,可以查看绘制html文字的接口CRenderEngine::DrawHtmlText入口处的注释

  1. void CRenderEngine::DrawHtmlText( HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText
  2. , DWORD dwTextColor, RECT* prcLinks, CStdString* sLinks, int& nLinkRects, UINT uStyle )
  3. {
  4. // 考虑到在xml编辑器中使用<>符号不方便,可以使用{}符号代替
  5. // 支持标签嵌套(如<l><b>text</b></l>),但是交叉嵌套是应该避免的(如<l><b>text</l></b>)
  6. // The string formatter supports a kind of "mini-html" that consists of various short tags:
  7. //
  8. // Bold: <b>text</b>
  9. // Color: <c #xxxxxx>text</c> where x = RGB in hex
  10. // Font: <f x>text</f> where x = font id
  11. // Italic: <i>text</i>
  12. // Image: <i x y z> where x = image name and y = imagelist num and z(optional) = imagelist id
  13. // Link: <a x>text</a> where x(optional) = link content, normal like app:notepad or http:
  14. // NewLine <n>
  15. // Paragraph: <p x>text</p> where x = extra pixels indent in p
  16. // Raw Text: <r>text</r>
  17. // Selected: <s>text</s>
  18. // Underline: <u>text</u>
  19. // X Indent: <x i> where i = hor indent in pixels
  20. // Y Indent: <y i> where i = ver indent in pixels
  21. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  22. if( NULL == pstrText || NULL == pManager )
  23. {
  24. return;
  25. }
  26. if( ::IsRectEmpty(&rc) )
  27. {
  28. return;
  29. }
  30. bool bDraw = (uStyle & DT_CALCRECT) == 0;
  31. CStdPtrArray aFontArray(10);
  32. CStdPtrArray aColorArray(10);
  33. CStdPtrArray aPIndentArray(10);
  34. RECT rcClip = { 0 };
  35. ::GetClipBox( hDC, &rcClip );
  36. HRGN hOldRgn = ::CreateRectRgnIndirect( &rcClip );
  37. HRGN hRgn = ::CreateRectRgnIndirect( &rc );
  38. if( bDraw )
  39. {
  40. ::ExtSelectClipRgn( hDC, hRgn, RGN_AND );
  41. }
  42. TEXTMETRIC* pTm = &pManager->GetDefaultFontInfo()->tm;
  43. HFONT hOldFont = (HFONT) ::SelectObject( hDC, pManager->GetDefaultFontInfo()->hFont );
  44. ::SetBkMode( hDC, TRANSPARENT );
  45. ::SetTextColor( hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)) );
  46. DWORD dwBkColor = pManager->GetDefaultSelectedBkColor();
  47. ::SetBkColor( hDC, RGB(GetBValue(dwBkColor), GetGValue(dwBkColor), GetRValue(dwBkColor)) );
  48. // If the drawstyle include a alignment, we'll need to first determine the text-size so
  49. // we can draw it at the correct position...
  50. if( (uStyle & DT_SINGLELINE) != 0 && (uStyle & DT_VCENTER) != 0 && (uStyle & DT_CALCRECT) == 0 )
  51. {
  52. RECT rcText = { 0, 0, 9999, 100 };
  53. int nLinks = 0;
  54. DrawHtmlText(hDC, pManager, rcText, pstrText, dwTextColor, NULL, NULL, nLinks, uStyle | DT_CALCRECT);
  55. = + (( - ) / 2) - (( - ) / 2);
  56. = + ( - );
  57. }
  58. if( (uStyle & DT_SINGLELINE) != 0 && (uStyle & DT_CENTER) != 0 && (uStyle & DT_CALCRECT) == 0 )
  59. {
  60. RECT rcText = { 0, 0, 9999, 100 };
  61. int nLinks = 0;
  62. DrawHtmlText(hDC, pManager, rcText, pstrText, dwTextColor, NULL, NULL, nLinks, uStyle | DT_CALCRECT);
  63. = + (( - ) / 2) - (( - ) / 2);
  64. = + ( - );
  65. }
  66. if( (uStyle & DT_SINGLELINE) != 0 && (uStyle & DT_RIGHT) != 0 && (uStyle & DT_CALCRECT) == 0 )
  67. {
  68. RECT rcText = { 0, 0, 9999, 100 };
  69. int nLinks = 0;
  70. DrawHtmlText(hDC, pManager, rcText, pstrText, dwTextColor, NULL, NULL, nLinks, uStyle | DT_CALCRECT);
  71. = - ( - );
  72. }
  73. bool bHoverLink = false;
  74. CStdString sHoverLink;
  75. POINT ptMouse = pManager->GetMousePos();
  76. for( int i = 0; !bHoverLink && i < nLinkRects; i++ )
  77. {
  78. if( ::PtInRect(prcLinks + i, ptMouse) )
  79. {
  80. sHoverLink = *(CStdString*)(sLinks + i);
  81. bHoverLink = true;
  82. }
  83. }
  84. POINT pt = { , };
  85. int iLinkIndex = 0;
  86. int cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
  87. int cyMinHeight = 0;
  88. int cxMaxWidth = 0;
  89. POINT ptLinkStart = { 0 };
  90. bool bLineEnd = false; // 一行结束
  91. bool bInRaw = false; // 原始文本,标记了之后这段文字不被解析,原样输出
  92. bool bInLink = false;
  93. bool bInSelected = false;
  94. int iLineLinkIndex = 0;
  95. // 排版习惯是图文底部对齐,所以每行绘制都要分两步,先计算高度,再绘制
  96. CStdPtrArray aLineFontArray; // 所以,第二次解析的时候要把aFontArray保存起来,行解析结束在还原到回去。这三个结构体起着备份作用。
  97. CStdPtrArray aLineColorArray;
  98. CStdPtrArray aLinePIndentArray;
  99. LPCTSTR pstrLineBegin = pstrText;
  100. bool bLineInRaw = false;
  101. bool bLineInLink = false;
  102. bool bLineInSelected = false;
  103. int cyLineHeight = 0;
  104. bool bLineDraw = false; // 行的第二阶段:绘制
  105. while( *pstrText != _T('\0') )
  106. {
  107. if( >= || *pstrText == _T('\n') || bLineEnd )
  108. {
  109. if( *pstrText == _T('\n') ) pstrText++;
  110. if( bLineEnd ) bLineEnd = false;
  111. if( !bLineDraw )
  112. {
  113. if( bInLink && iLinkIndex < nLinkRects )
  114. {
  115. ::SetRect(&prcLinks[iLinkIndex++], , , MIN(, ), + cyLine);
  116. CStdString *pStr1 = (CStdString*)(sLinks + iLinkIndex - 1);
  117. CStdString *pStr2 = (CStdString*)(sLinks + iLinkIndex);
  118. *pStr2 = *pStr1;
  119. }
  120. for( int i = iLineLinkIndex; i < iLinkIndex; i++ )
  121. {
  122. prcLinks[i].bottom = + cyLine;
  123. }
  124. if( bDraw )
  125. {
  126. bInLink = bLineInLink;
  127. iLinkIndex = iLineLinkIndex;
  128. }
  129. }
  130. else
  131. {
  132. if( bInLink && iLinkIndex < nLinkRects ) iLinkIndex++;
  133. bLineInLink = bInLink;
  134. iLineLinkIndex = iLinkIndex;
  135. }
  136. if( (uStyle & DT_SINGLELINE) != 0 && (!bDraw || bLineDraw) )
  137. {
  138. break;
  139. }
  140. if( bDraw )
  141. {
  142. bLineDraw = !bLineDraw; // !
  143. }
  144. = ;
  145. if( !bLineDraw )
  146. {
  147. += cyLine;
  148. }
  149. if( > && bDraw )
  150. {
  151. break;
  152. }
  153. ptLinkStart = pt;
  154. cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
  155. if( >= )
  156. {
  157. break;
  158. }
  159. }
  160. else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') )
  161. && ( pstrText[1] >= _T('a') && pstrText[1] <= _T('z') )
  162. && ( pstrText[2] == _T(' ') || pstrText[2] == _T('>') || pstrText[2] == _T('}') ) )
  163. {
  164. pstrText++;
  165. LPCTSTR pstrNextStart = NULL;
  166. switch( *pstrText )
  167. {
  168. case _T('a'): // Link
  169. {
  170. pstrText++;
  171. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  172. if( iLinkIndex < nLinkRects && !bLineDraw )
  173. {
  174. CStdString *pStr = (CStdString*)(sLinks + iLinkIndex);
  175. pStr->Empty();
  176. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') )
  177. {
  178. LPCTSTR pstrTemp = ::CharNext(pstrText);
  179. while( pstrText < pstrTemp)
  180. {
  181. *pStr += *pstrText++;
  182. }
  183. }
  184. }
  185. DWORD clrColor = pManager->GetDefaultLinkFontColor();
  186. if( bHoverLink && iLinkIndex < nLinkRects )
  187. {
  188. CStdString *pStr = (CStdString*)(sLinks + iLinkIndex);
  189. if( sHoverLink == *pStr )
  190. {
  191. clrColor = pManager->GetDefaultLinkHoverFontColor();
  192. }
  193. }
  194. aColorArray.Add( (LPVOID)clrColor );
  195. ::SetTextColor( hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)) );
  196. TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
  197. if( aFontArray.GetSize() > 0 )
  198. {
  199. pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  200. }
  201. if( false == pFontInfo->bUnderline )
  202. {
  203. HFONT hFont = pManager->GetFont( pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold
  204. , true, pFontInfo->bItalic);
  205. if( NULL == hFont )
  206. {
  207. hFont = pManager->AddFont( pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic );
  208. }
  209. pFontInfo = pManager->GetFontInfo( hFont );
  210. aFontArray.Add( pFontInfo );
  211. pTm = &pFontInfo->tm;
  212. ::SelectObject( hDC, pFontInfo->hFont );
  213. cyLine = MAX( cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  214. }
  215. ptLinkStart = pt;
  216. bInLink = true;
  217. }
  218. break;
  219. case _T('b'): // Bold
  220. {
  221. pstrText++;
  222. TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
  223. if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  224. if( false == pFontInfo->bBold )
  225. {
  226. HFONT hFont = pManager->GetFont( pFontInfo->sFontName, pFontInfo->iSize
  227. , true, pFontInfo->bUnderline, pFontInfo->bItalic );
  228. if( NULL == hFont )
  229. {
  230. hFont = pManager->AddFont( pFontInfo->sFontName, pFontInfo->iSize
  231. , true, pFontInfo->bUnderline, pFontInfo->bItalic );
  232. }
  233. pFontInfo = pManager->GetFontInfo( hFont );
  234. aFontArray.Add( pFontInfo );
  235. pTm = &pFontInfo->tm;
  236. ::SelectObject( hDC, pFontInfo->hFont );
  237. cyLine = MAX( cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  238. }
  239. }
  240. break;
  241. case _T('c'): // Color
  242. {
  243. pstrText++;
  244. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  245. {
  246. pstrText = ::CharNext(pstrText);
  247. }
  248. if( *pstrText == _T('#'))
  249. {
  250. pstrText++;
  251. }
  252. DWORD clrColor = _tcstol( pstrText, const_cast<LPTSTR*>(&pstrText), 16 );
  253. aColorArray.Add( (LPVOID)clrColor );
  254. ::SetTextColor( hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)) );
  255. }
  256. break;
  257. case _T('f'): // Font
  258. {
  259. pstrText++;
  260. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  261. {
  262. pstrText = ::CharNext(pstrText);
  263. }
  264. LPCTSTR pstrTemp = pstrText;
  265. int iFont = (int) _tcstol( pstrText, const_cast<LPTSTR*>(&pstrText), 10 );
  266. if( pstrTemp != pstrText )
  267. {
  268. TFontInfo* pFontInfo = pManager->GetFontInfo( iFont );
  269. aFontArray.Add( pFontInfo );
  270. pTm = &pFontInfo->tm;
  271. ::SelectObject( hDC, pFontInfo->hFont );
  272. }
  273. else
  274. {
  275. CStdString sFontName;
  276. int iFontSize = 10;
  277. CStdString sFontAttr;
  278. bool bBold = false;
  279. bool bUnderline = false;
  280. bool bItalic = false;
  281. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') )
  282. {
  283. pstrTemp = ::CharNext(pstrText);
  284. while( pstrText < pstrTemp)
  285. {
  286. sFontName += *pstrText++;
  287. }
  288. }
  289. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  290. {
  291. pstrText = ::CharNext(pstrText);
  292. }
  293. if( isdigit(*pstrText) )
  294. {
  295. iFontSize = (int) _tcstol( pstrText, const_cast<LPTSTR*>(&pstrText), 10 );
  296. }
  297. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  298. {
  299. pstrText = ::CharNext(pstrText);
  300. }
  301. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') )
  302. {
  303. pstrTemp = ::CharNext( pstrText );
  304. while( pstrText < pstrTemp)
  305. {
  306. sFontAttr += *pstrText++;
  307. }
  308. }
  309. sFontAttr.MakeLower();
  310. if( sFontAttr.Find(_T("bold")) >= 0 ) bBold = true;
  311. if( sFontAttr.Find(_T("underline")) >= 0 ) bUnderline = true;
  312. if( sFontAttr.Find(_T("italic")) >= 0 ) bItalic = true;
  313. HFONT hFont = pManager->GetFont( sFontName, iFontSize, bBold, bUnderline, bItalic );
  314. if( NULL == hFont )
  315. {
  316. hFont = pManager->AddFont( sFontName, iFontSize, bBold, bUnderline, bItalic );
  317. }
  318. TFontInfo* pFontInfo = pManager->GetFontInfo( hFont );
  319. aFontArray.Add( pFontInfo );
  320. pTm = &pFontInfo->tm;
  321. ::SelectObject( hDC, pFontInfo->hFont );
  322. }
  323. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  324. }
  325. break;
  326. case _T('i'): // Italic or Image
  327. {
  328. pstrNextStart = pstrText - 1;
  329. pstrText++;
  330. CStdString sImageString = pstrText;
  331. int iWidth = 0;
  332. int iHeight = 0;
  333. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  334. {
  335. pstrText = ::CharNext(pstrText);
  336. }
  337. const TImageInfo* pImageInfo = NULL;
  338. CStdString sName;
  339. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') )
  340. {
  341. LPCTSTR pstrTemp = ::CharNext( pstrText );
  342. while( pstrText < pstrTemp)
  343. {
  344. sName += *pstrText++;
  345. }
  346. }
  347. if( sName.IsEmpty() ) // Italic
  348. {
  349. pstrNextStart = NULL;
  350. TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
  351. if( aFontArray.GetSize() > 0 )
  352. {
  353. pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  354. }
  355. if( pFontInfo->bItalic == false )
  356. {
  357. HFONT hFont = pManager->GetFont( pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold
  358. , pFontInfo->bUnderline, true );
  359. if( NULL == hFont )
  360. {
  361. hFont = pManager->AddFont( pFontInfo->sFontName, pFontInfo->iSize
  362. , pFontInfo->bBold, pFontInfo->bUnderline, true );
  363. }
  364. pFontInfo = pManager->GetFontInfo( hFont );
  365. aFontArray.Add( pFontInfo );
  366. pTm = &pFontInfo->tm;
  367. ::SelectObject( hDC, pFontInfo->hFont );
  368. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  369. }
  370. }
  371. else
  372. {
  373. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  374. {
  375. pstrText = ::CharNext(pstrText);
  376. }
  377. int iImageListNum = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  378. if( iImageListNum <= 0 ) iImageListNum = 1;
  379. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  380. int iImageListIndex = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  381. if( iImageListIndex < 0 || iImageListIndex >= iImageListNum ) iImageListIndex = 0;
  382. if( _tcsstr(sImageString.GetData(), _T("file=\'")) != NULL || _tcsstr(sImageString.GetData(), _T("res=\'")) != NULL )
  383. {
  384. CStdString sImageResType;
  385. CStdString sImageName;
  386. LPCTSTR pStrImage = sImageString.GetData();
  387. CStdString sItem;
  388. CStdString sValue;
  389. while( *pStrImage != _T('\0') )
  390. {
  391. sItem.Empty();
  392. sValue.Empty();
  393. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  394. while( *pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' ') )
  395. {
  396. LPTSTR pstrTemp = ::CharNext(pStrImage);
  397. while( pStrImage < pstrTemp)
  398. {
  399. sItem += *pStrImage++;
  400. }
  401. }
  402. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  403. if( *pStrImage++ != _T('=') ) break;
  404. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  405. if( *pStrImage++ != _T('\'') ) break;
  406. while( *pStrImage != _T('\0') && *pStrImage != _T('\'') )
  407. {
  408. LPTSTR pstrTemp = ::CharNext(pStrImage);
  409. while( pStrImage < pstrTemp)
  410. {
  411. sValue += *pStrImage++;
  412. }
  413. }
  414. if( *pStrImage++ != _T('\'') ) break;
  415. if( !sValue.IsEmpty() )
  416. {
  417. if( sItem == _T("file") || sItem == _T("res") )
  418. {
  419. sImageName = sValue;
  420. }
  421. else if( sItem == _T("restype") )
  422. {
  423. sImageResType = sValue;
  424. }
  425. }
  426. if( *pStrImage++ != _T(' ') ) break;
  427. }
  428. pImageInfo = pManager->GetImageEx((LPCTSTR)sImageName, sImageResType);
  429. }
  430. else
  431. {
  432. pImageInfo = pManager->GetImageEx((LPCTSTR)sName);
  433. }
  434. if( pImageInfo )
  435. {
  436. iWidth = pImageInfo->nX;
  437. iHeight = pImageInfo->nY;
  438. if( iImageListNum > 1 ) iWidth /= iImageListNum;
  439. if( + iWidth > && > && (uStyle & DT_SINGLELINE) == 0 )
  440. {
  441. bLineEnd = true;
  442. }
  443. else
  444. {
  445. pstrNextStart = NULL;
  446. if( bDraw && bLineDraw )
  447. {
  448. CDirectRect rcImage(, + cyLineHeight - iHeight, + iWidth, + cyLineHeight);
  449. if( iHeight < cyLineHeight )
  450. {
  451. -= (cyLineHeight - iHeight) / 2;
  452. = - iHeight;
  453. }
  454. CDirectRect rcBmpPart(0, 0, iWidth, iHeight);
  455. = iWidth * iImageListIndex;
  456. = iWidth * (iImageListIndex + 1);
  457. CDirectRect rcCorner(0, 0, 0, 0);
  458. DrawImage(hDC, pImageInfo->hBitmap, rcImage, rcImage, rcBmpPart, rcCorner, \
  459. pImageInfo->alphaChannel, 255);
  460. }
  461. cyLine = MAX(iHeight, cyLine);
  462. += iWidth;
  463. cyMinHeight = + iHeight;
  464. cxMaxWidth = MAX(cxMaxWidth, );
  465. }
  466. }
  467. else pstrNextStart = NULL;
  468. }
  469. }
  470. break;
  471. case _T('n'): // Newline
  472. {
  473. pstrText++;
  474. if( (uStyle & DT_SINGLELINE) != 0 ) break;
  475. bLineEnd = true;
  476. }
  477. break;
  478. case _T('p'): // Paragraph
  479. {
  480. pstrText++;
  481. if( > ) bLineEnd = true;
  482. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  483. {
  484. pstrText = ::CharNext(pstrText);
  485. }
  486. int cyLineExtra = (int)_tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  487. aPIndentArray.Add((LPVOID)cyLineExtra);
  488. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + cyLineExtra);
  489. }
  490. break;
  491. case _T('r'): // Raw Text
  492. {
  493. pstrText++;
  494. bInRaw = true;
  495. }
  496. break;
  497. case _T('s'): // Selected text background color
  498. {
  499. pstrText++;
  500. bInSelected = !bInSelected;
  501. if( bDraw && bLineDraw )
  502. {
  503. if( bInSelected )
  504. {
  505. ::SetBkMode( hDC, OPAQUE );
  506. }
  507. else
  508. {
  509. ::SetBkMode( hDC, TRANSPARENT );
  510. }
  511. }
  512. }
  513. break;
  514. case _T('u'): // Underline text
  515. {
  516. pstrText++;
  517. TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
  518. if( aFontArray.GetSize() > 0 )
  519. {
  520. pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  521. }
  522. if( false == pFontInfo->bUnderline )
  523. {
  524. HFONT hFont = pManager->GetFont( pFontInfo->sFontName, pFontInfo->iSize
  525. , pFontInfo->bBold, true, pFontInfo->bItalic );
  526. if( NULL == hFont )
  527. {
  528. hFont = pManager->AddFont( pFontInfo->sFontName, pFontInfo->iSize
  529. , pFontInfo->bBold, true, pFontInfo->bItalic );
  530. }
  531. pFontInfo = pManager->GetFontInfo( hFont );
  532. aFontArray.Add( pFontInfo );
  533. pTm = &pFontInfo->tm;
  534. ::SelectObject( hDC, pFontInfo->hFont );
  535. cyLine = MAX( cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  536. }
  537. }
  538. break;
  539. case _T('x'): // X Indent
  540. {
  541. pstrText++;
  542. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  543. {
  544. pstrText = ::CharNext(pstrText);
  545. }
  546. int iWidth = (int) _tcstol( pstrText, const_cast<LPTSTR*>(&pstrText), 10 );
  547. += iWidth;
  548. cxMaxWidth = MAX(cxMaxWidth, );
  549. }
  550. break;
  551. case _T('y'): // Y Indent
  552. {
  553. pstrText++;
  554. while( *pstrText > _T('\0') && *pstrText <= _T(' ') )
  555. {
  556. pstrText = ::CharNext(pstrText);
  557. }
  558. cyLine = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  559. }
  560. break;
  561. }
  562. if( pstrNextStart != NULL )
  563. {
  564. pstrText = pstrNextStart;
  565. }
  566. else
  567. {
  568. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') )
  569. {
  570. pstrText = ::CharNext(pstrText);
  571. }
  572. pstrText = ::CharNext(pstrText);
  573. }
  574. }
  575. else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') ) && pstrText[1] == _T('/') )
  576. {
  577. pstrText++;
  578. pstrText++;
  579. switch( *pstrText )
  580. {
  581. case _T('c'):
  582. {
  583. pstrText++;
  584. aColorArray.Remove(aColorArray.GetSize() - 1);
  585. DWORD clrColor = dwTextColor;
  586. if( aColorArray.GetSize() > 0 )
  587. {
  588. clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  589. }
  590. ::SetTextColor( hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)) );
  591. }
  592. break;
  593. case _T('p'):
  594. {
  595. pstrText++;
  596. if( > )
  597. {
  598. bLineEnd = true;
  599. }
  600. aPIndentArray.Remove(aPIndentArray.GetSize() - 1);
  601. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  602. break;
  603. }
  604. case _T('s'):
  605. {
  606. pstrText++;
  607. bInSelected = !bInSelected;
  608. if( bDraw && bLineDraw )
  609. {
  610. if( bInSelected )
  611. {
  612. ::SetBkMode( hDC, OPAQUE );
  613. }
  614. else
  615. {
  616. ::SetBkMode( hDC, TRANSPARENT );
  617. }
  618. }
  619. }
  620. break;
  621. case _T('a'):
  622. {
  623. if( iLinkIndex < nLinkRects )
  624. {
  625. if( !bLineDraw )
  626. {
  627. ::SetRect( &prcLinks[iLinkIndex], ,
  628. , MIN(, ), + pTm->tmHeight + pTm->tmExternalLeading );
  629. }
  630. iLinkIndex++;
  631. }
  632. aColorArray.Remove( aColorArray.GetSize() - 1 );
  633. DWORD clrColor = dwTextColor;
  634. if( aColorArray.GetSize() > 0 )
  635. {
  636. clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  637. }
  638. ::SetTextColor( hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)) );
  639. bInLink = false;
  640. }
  641. case _T('b'):
  642. case _T('f'):
  643. case _T('i'):
  644. case _T('u'):
  645. {
  646. pstrText++;
  647. aFontArray.Remove(aFontArray.GetSize() - 1);
  648. TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  649. if( NULL == pFontInfo )
  650. {
  651. pFontInfo = pManager->GetDefaultFontInfo();
  652. }
  653. if( pTm->tmItalic && false == pFontInfo->bItalic )
  654. {
  655. ABC abc;
  656. ::GetCharABCWidths(hDC, _T(' '), _T(' '), &abc);
  657. += / 2; // 简单修正一下斜体混排的问题, 正确做法应该是/kb/244798/en-us
  658. }
  659. pTm = &pFontInfo->tm;
  660. ::SelectObject(hDC, pFontInfo->hFont);
  661. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  662. }
  663. break;
  664. }
  665. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') )
  666. {
  667. pstrText = ::CharNext( pstrText );
  668. }
  669. pstrText = ::CharNext( pstrText );
  670. }
  671. else if( !bInRaw && *pstrText == _T('<') && pstrText[2] == _T('>') && (pstrText[1] == _T('{') || pstrText[1] == _T('}')) )
  672. {
  673. SIZE szSpace = { 0 };
  674. ::GetTextExtentPoint32( hDC, &pstrText[1], 1, &szSpace );
  675. if( bDraw && bLineDraw )
  676. {
  677. ::TextOut( hDC, , + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1 );
  678. }
  679. += ;
  680. cxMaxWidth = MAX(cxMaxWidth, );
  681. pstrText++;pstrText++;pstrText++;
  682. }
  683. else if( !bInRaw && *pstrText == _T('{') && pstrText[2] == _T('}') && (pstrText[1] == _T('<') || pstrText[1] == _T('>')) )
  684. {
  685. SIZE szSpace = { 0 };
  686. ::GetTextExtentPoint32(hDC, &pstrText[1], 1, &szSpace);
  687. if( bDraw && bLineDraw )
  688. {
  689. ::TextOut( hDC, , + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1 );
  690. }
  691. += ;
  692. cxMaxWidth = MAX(cxMaxWidth, );
  693. pstrText++;pstrText++;pstrText++;
  694. }
  695. else if( !bInRaw && *pstrText == _T(' ') )
  696. {
  697. SIZE szSpace = { 0 };
  698. ::GetTextExtentPoint32(hDC, _T(" "), 1, &szSpace);
  699. // Still need to paint the space because the font might have
  700. // underline formatting.
  701. if( bDraw && bLineDraw )
  702. {
  703. ::TextOut( hDC, , + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, _T(" "), 1 );
  704. }
  705. += ;
  706. cxMaxWidth = MAX(cxMaxWidth, );
  707. pstrText++;
  708. }
  709. else
  710. {
  711. POINT ptPos = pt;
  712. int cchChars = 0;
  713. int cchSize = 0;
  714. int cchLastGoodWord = 0;
  715. int cchLastGoodSize = 0;
  716. LPCTSTR p = pstrText;
  717. LPCTSTR pstrNext;
  718. SIZE szText = { 0 };
  719. if( !bInRaw && *p == _T('<') || *p == _T('{') ) p++, cchChars++, cchSize++;
  720. while( *p != _T('\0') && *p != _T('\n') )
  721. {
  722. // This part makes sure that we're word-wrapping if needed or providing support
  723. // for DT_END_ELLIPSIS. Unfortunately the GetTextExtentPoint32() call is pretty
  724. // slow when repeated so often.
  725. // TODO: Rewrite and use GetTextExtentExPoint() instead!
  726. if( bInRaw )
  727. {
  728. if( ( *p == _T('<') || *p == _T('{') ) && p[1] == _T('/')
  729. && p[2] == _T('r') && ( p[3] == _T('>') || p[3] == _T('}') ) )
  730. {
  731. p += 4;
  732. bInRaw = false;
  733. break;
  734. }
  735. }
  736. else
  737. {
  738. if( *p == _T('<') || *p == _T('{') ) break;
  739. }
  740. pstrNext = ::CharNext(p);
  741. cchChars++;
  742. cchSize += (int)(pstrNext - p);
  743. = cchChars * pTm->tmMaxCharWidth;
  744. if( + >= )
  745. {
  746. ::GetTextExtentPoint32(hDC, pstrText, cchSize, &szText);
  747. }
  748. if( + > )
  749. {
  750. if( + > && != )
  751. {
  752. cchChars--;
  753. cchSize -= (int)(pstrNext - p);
  754. }
  755. if( (uStyle & DT_WORDBREAK) != 0 && cchLastGoodWord > 0 )
  756. {
  757. cchChars = cchLastGoodWord;
  758. cchSize = cchLastGoodSize;
  759. }
  760. if( (uStyle & DT_END_ELLIPSIS) != 0 && cchChars > 0 )
  761. {
  762. cchChars -= 1;
  763. LPCTSTR pstrPrev = ::CharPrev(pstrText, p);
  764. if( cchChars > 0 )
  765. {
  766. cchChars -= 1;
  767. pstrPrev = ::CharPrev(pstrText, pstrPrev);
  768. cchSize -= (int)(p - pstrPrev);
  769. }
  770. else
  771. {
  772. cchSize -= (int)(p - pstrPrev);
  773. }
  774. = ;
  775. }
  776. bLineEnd = true;
  777. cxMaxWidth = MAX(cxMaxWidth, );
  778. break;
  779. }
  780. if (!( ( p[0] >= _T('a') && p[0] <= _T('z') ) || ( p[0] >= _T('A') && p[0] <= _T('Z') ) ))
  781. {
  782. cchLastGoodWord = cchChars;
  783. cchLastGoodSize = cchSize;
  784. }
  785. if( *p == _T(' ') )
  786. {
  787. cchLastGoodWord = cchChars;
  788. cchLastGoodSize = cchSize;
  789. }
  790. p = ::CharNext( p );
  791. }
  792. ::GetTextExtentPoint32( hDC, pstrText, cchSize, &szText );
  793. if( bDraw && bLineDraw )
  794. {
  795. ::TextOut( hDC, , + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, pstrText, cchSize );
  796. if( >= && (uStyle & DT_END_ELLIPSIS) != 0 )
  797. {
  798. //英文或者日文环境下字体比预设的小两号,所以控件原先的位置将不能正确反应文字的绘制区域 SDM-00049935 by luanchao
  799. if ( _tcsicmp( GetLanPtr()->GetCurrentLan(), LANGTYPE_JAP ) == 0 || _tcsicmp( GetLanPtr()->GetCurrentLan(), LANGTYPE_ENG ) == 0 )
  800. {
  801. ::TextOut(hDC, + , + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, _T("..."), 3);
  802. }
  803. else
  804. {
  805. ::TextOut(hDC, + , , _T("..."), 3);
  806. }
  807. }
  808. }
  809. += ;
  810. cxMaxWidth = MAX(cxMaxWidth, );
  811. pstrText += cchSize;
  812. }
  813. if( >= || *pstrText == _T('\n') || *pstrText == _T('\0') )
  814. {
  815. bLineEnd = true;
  816. }
  817. if( bDraw && bLineEnd )
  818. {
  819. if( !bLineDraw )
  820. {
  821. aFontArray.Resize( aLineFontArray.GetSize() );
  822. ::CopyMemory( aFontArray.GetData(), aLineFontArray.GetData(), aLineFontArray.GetSize() * sizeof(LPVOID) );
  823. aColorArray.Resize( aLineColorArray.GetSize() );
  824. ::CopyMemory( aColorArray.GetData(), aLineColorArray.GetData(), aLineColorArray.GetSize() * sizeof(LPVOID) );
  825. aPIndentArray.Resize( aLinePIndentArray.GetSize() );
  826. ::CopyMemory( aPIndentArray.GetData(), aLinePIndentArray.GetData(), aLinePIndentArray.GetSize() * sizeof(LPVOID) );
  827. cyLineHeight = cyLine;
  828. pstrText = pstrLineBegin;
  829. bInRaw = bLineInRaw;
  830. bInSelected = bLineInSelected;
  831. DWORD clrColor = dwTextColor;
  832. if( aColorArray.GetSize() > 0 )
  833. {
  834. clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  835. }
  836. ::SetTextColor( hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)) );
  837. TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  838. if( NULL == pFontInfo )
  839. {
  840. pFontInfo = pManager->GetDefaultFontInfo();
  841. }
  842. pTm = &pFontInfo->tm;
  843. ::SelectObject( hDC, pFontInfo->hFont );
  844. if( bInSelected )
  845. {
  846. ::SetBkMode( hDC, OPAQUE );
  847. }
  848. }
  849. else
  850. {
  851. aLineFontArray.Resize( aFontArray.GetSize() );
  852. ::CopyMemory( aLineFontArray.GetData(), aFontArray.GetData(), aFontArray.GetSize() * sizeof(LPVOID) );
  853. aLineColorArray.Resize( aColorArray.GetSize() );
  854. ::CopyMemory( aLineColorArray.GetData(), aColorArray.GetData(), aColorArray.GetSize() * sizeof(LPVOID) );
  855. aLinePIndentArray.Resize( aPIndentArray.GetSize() );
  856. ::CopyMemory( aLinePIndentArray.GetData(), aPIndentArray.GetData(), aPIndentArray.GetSize() * sizeof(LPVOID) );
  857. pstrLineBegin = pstrText;
  858. bLineInSelected = bInSelected;
  859. bLineInRaw = bInRaw;
  860. }
  861. }
  862. ASSERT( iLinkIndex<=nLinkRects );
  863. }
  864. nLinkRects = iLinkIndex;
  865. // Return size of text when requested
  866. if( (uStyle & DT_CALCRECT) != 0 )
  867. {
  868. = MAX( cyMinHeight, + cyLine );
  869. = MIN( , cxMaxWidth );
  870. }
  871. if( bDraw ) ::SelectClipRgn( hDC, hOldRgn );
  872. ::DeleteObject( hOldRgn );
  873. ::DeleteObject( hRgn );
  874. ::SelectObject( hDC, hOldFont );
  875. }

3、使用CTextUI控件对文字宽度自适应的特性

       CTextUI控件比较特别,会根据当前给其设置的文字宽度,自动调整自己控件的宽度,刚好能将文字显示全,也不占用多余的宽度。

       这种自适应的特性可以巧妙地用在一些场合。比如IM群组的聊天框,在聊天框的右侧会显示当前的讨论组成员列表,如果成员是管理员,我们需要在成员名称后面添加一个小图标,以标识该成员是管理员,如下:

       一般的做法是,利用CContainer的自动布局特性,我们将不变宽度的控件设置固定宽度值,位于右边的管理员图标宽度也设置为固定值,这样中间的成员名称是文本,可长可短,不设置成固定值,使用CLabelUI控件即可。这样,中间的名称宽度就是可变的,但是这样做有个问题,管理员图标会固定在每个成员item的最右边,不能跟随名称紧贴名称后面,显得不太美观。后来考虑到CTextUI的对文字长度能自适应,就改用CTextUI,能很好的实现管理员图标能紧跟在成员名称后面。 

      之前在项目中使用CTextUI空间控件时遇到过给该控件设置新的文字后控件显示的文字并没有更新,显示的还是老的文字,这个问题的排查与解决,专门写了一篇文章,感兴趣可以去看一下:

duilib中CTextUI控件更新文字后不显示最新文字(不刷新显示)的问题排查/chenlycly/article/details/140444416


       

4、CRichEditUI富文本控件使用注意点

4.1、指定CRichEditUI加在2.0版本的库

       相关的代码在CTxtWinHost::Init接口中,原先是直接调用CreateTextServices接口,现在改成去动态加载2.0版本的库,调用该库中的CreateTextServices接口去创建Text服务,这样指定使用2.0版本的库,相关代码如下:

  1. BOOL CTxtWinHost::Init(CRichEditUI *re, const CREATESTRUCT *pcs)
  2. {
  3. IUnknown *pUnk;
  4. HRESULT hr;
  5. m_re = re;
  6. // Initialize Reference count
  7. cRefs = 1;
  8. // Create and cache CHARFORMAT for this control
  9. if(FAILED(InitDefaultCharFormat(re, &cf, NULL)))
  10. goto err;
  11. // Create and cache PARAFORMAT for this control
  12. if(FAILED(InitDefaultParaFormat(re, &pf)))
  13. goto err;
  14. // edit controls created without a window are multiline by default
  15. // so that paragraph formats can be
  16. dwStyle = ES_MULTILINE;
  17. // edit controls are rich by default
  18. fRich = re->IsRich();
  19. cchTextMost = re->GetLimitText();
  20. if (pcs )
  21. {
  22. dwStyle = pcs->style;
  23. if ( !(dwStyle & (ES_AUTOHSCROLL | WS_HSCROLL)) )
  24. {
  25. fWordWrap = TRUE;
  26. }
  27. }
  28. if( !(dwStyle & ES_LEFT) )
  29. {
  30. if(dwStyle & ES_CENTER)
  31. = PFA_CENTER;
  32. else if(dwStyle & ES_RIGHT)
  33. = PFA_RIGHT;
  34. }
  35. fInplaceActive = TRUE;
  36. // 此处修改代码,明确指定加载库
  37. //CreateTextServices()函数
  38. typedef HRESULT(_stdcall *CTSFunc)(IUnknown *punkOuter, ITextHost *pITextHost, IUnknown **ppUnk);
  39. CTSFunc ctsFunc = NULL;
  40. auto hRiched20 = LoadLibrary(_T(""));
  41. if (NULL == hRiched20)
  42. goto err;
  43. else
  44. {
  45. ctsFunc = (CTSFunc)GetProcAddress(hRiched20, "CreateTextServices");
  46. if (NULL == ctsFunc)
  47. goto err;
  48. }
  49. if (FAILED(ctsFunc(NULL, this, &pUnk)))
  50. goto err;
  51. // Create Text Services component
  52. //if(FAILED(CreateTextServices(NULL, this, &pUnk)))
  53. // goto err;
  54. hr = pUnk->QueryInterface(IID_ITextServices,(void **)&pserv);
  55. // Whether the previous call succeeded or failed we are done
  56. // with the private interface.
  57. pUnk->Release();
  58. if(FAILED(hr))
  59. {
  60. goto err;
  61. }
  62. hr = pserv->TxSendMessage(EM_GETOLEINTERFACE, 0, (LPARAM)&m_pRichEditOle, NULL);
  63. if(FAILED(hr))
  64. {
  65. goto err;
  66. }
  67. m_pIRichEditOleCallback = new IRichEditOleCallbackEx;
  68. pserv->TxSendMessage(EM_SETOLECALLBACK, 0, (LPARAM)m_pIRichEditOleCallback, 0);
  69. // Set window text
  70. if(pcs && pcs->lpszName)
  71. {
  72. #ifdef _UNICODE
  73. if(FAILED(pserv->TxSetText((TCHAR *)pcs->lpszName)))
  74. goto err;
  75. #else
  76. size_t iLen = _tcslen(pcs->lpszName);
  77. LPWSTR lpText = new WCHAR[iLen + 1];
  78. ::ZeroMemory(lpText, (iLen + 1) * sizeof(WCHAR));
  79. ::MultiByteToWideChar(CP_ACP, 0, pcs->lpszName, -1, (LPWSTR)lpText, iLen) ;
  80. if(FAILED(pserv->TxSetText((LPWSTR)lpText))) {
  81. delete[] lpText;
  82. goto err;
  83. }
  84. delete[] lpText;
  85. #endif
  86. }
  87. return TRUE;
  88. err:
  89. return FALSE;
  90. }

4.2、解决向CRichEditUI中插入文字后显示空白的问题

       我们在项目中使用CRichEditUI控件时,遇到一个较严重的问题,即向CRichEditUI控件插入文字后,控件显示空白,如果手动操作鼠标在richedit中滚动一下就能显示出文字了,这个问题我们遇到很多次了。针对手动滚动一下鼠标就能显示出来文字,我们搞出了一个规避的办法,可以将滚动条向上或向下滚动一下,然后再滚动回原来的位置。这里分两种场景:

1)插入文字后,将滚动条默认滚动到最上面(让用户看到CRichEditUI控件中最开始的文字)
先向下滚动一下(发送SB_LINEDOWN),然后再回到顶端(发送SB_TOP),相关代码如下:

  1. // 默认是滚到顶部,为了解决显示空白的问题,先向下滚动一下,然后再滚动到顶部
  2. m_pRichEditVerInfo->TxSendMessage(WM_VSCROLL, SB_LINEDOWN, 0L, 0);
  3. m_pRichEditVerInfo->TxSendMessage(WM_VSCROLL, SB_TOP, 0L, 0);
  4. m_pRichEditVerInfo->Invalidate();

2)插入文字后,将滚动条默认滚动到最下面(让用户看到CRichEditUI控件中最下面最新的文字)
先向上滚动一下(发送SB_LINEUP),然后再回到顶端(发送SB_BOTTOM),相关代码如下:

  1. // 默认是滚到顶部,为了解决显示空白的问题,先向上滚动一下,然后再滚动到底部
  2. m_pRichEditVerInfo->TxSendMessage(WM_VSCROLL, SB_LINEUP, 0L, 0);
  3. m_pRichEditVerInfo->TxSendMessage(WM_VSCROLL, SB_BOTTOM, 0L, 0);
  4. m_pRichEditVerInfo->Invalidate();

5、设置窗口透明度的接口CPaintManagerUI::SetTransparent的问题

       duilib框架中的窗口支持设置透明度,设置窗口透明度的是CPaintManagerUI::SetTransparent接口,如下:

  1. // 设置透明度,调用内核库函数
  2. void CPaintManagerUI::SetTransparent(int nOpacity)
  3. {
  4.     if (NULL == m_hWndPaint)
  5.     {
  6.         return;
  7.     }
  8.     typedef BOOL(__stdcall *PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
  9.     PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes;
  10.     HMODULE hUser32 = ::GetModuleHandle(_T(""));
  11.     if (hUser32)
  12.     {
  13.         fSetLayeredWindowAttributes =
  14.         (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32,     "SetLayeredWindowAttributes");
  15.         if (NULL == fSetLayeredWindowAttributes)
  16.         {
  17.             return;
  18.         }
  19.     }
  20.     DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  21.     DWORD dwNewStyle = dwStyle;
  22.     if (nOpacity >= 0 && nOpacity < 255)
  23.     {
  24.         dwNewStyle |= WS_EX_LAYERED;
  25.     }
  26.     else
  27.     {
  28.         // 1、问题1:不设置透明度,即nOpacity=255,也不应该取消WS_EX_LAYERED风格
  29.         // 可能外部要实现异形窗口,是需要WS_EX_LAYERED窗口风格
  30.         dwNewStyle &= ~WS_EX_LAYERED;
  31.     }
  32.     if (dwStyle != dwNewStyle)
  33.     {
  34.         ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  35.     }
  36.     // 问题2:不设置透明度,即nOpacity=255,是不需要调用SetLayeredWindowAttributes接口
  37.     // 不管有没有设置透明度,都去调用SetLayeredWindowAttributes,可能和异形窗口调用UpdateLayeredWindow
  38.     fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
  39. }

5.1、窗口透明度设置为255(不透明),之前添加的WS_EX_LAYERED风格被删除了

      我们之前在一个项目中要实现一个异形窗口,创建了一个dui窗口,给窗口设置WS_EX_LAYERED风格,然后调用UpdateLayeredWindow。

要实现异形窗口,首先要给窗口设置WS_EX_LAYERED分层窗口风格,然后要调用系统API接口UpdateLayeredWindow。

结果调试发现,异形窗口并没有生效,查看UpdateLayeredWindow函数返回值,发现UpdateLayeredWindow函数调用失败了,GetLastError获取到的错误码为87:

       使用SPY++查看该窗口的属性,结果发现该窗口并没有WS_EX_LAYERED风格,明明代码中给窗口设置了WS_EX_LAYERED风格,难道是该窗口风格在什么地方被取消了?于是在代码中全局搜索了一下WS_EX_LAYERED,发现在duilib中的CPaintManagerUI::SetTransparent中有取消WS_EX_LAYERED风格的代码,如下所示:

  1. DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  2. DWORD dwNewStyle = dwStyle;
  3. if (nOpacity >= 0 && nOpacity < 255)
  4. {
  5.     dwNewStyle |= WS_EX_LAYERED;
  6. }
  7. else
  8. {
  9.     // 1、问题1:不设置透明度,即nOpacity=255,也不应该取消WS_EX_LAYERED风格
  10.     // 可能外部要实现异形窗口,是需要WS_EX_LAYERED窗口风格
  11.     dwNewStyle &= ~WS_EX_LAYERED;
  12. }
  13.  
  14. if (dwStyle != dwNewStyle)
  15. {
  16.    ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  17. }

窗口不需要设置透明度,所以传入的透明度为255,从上述代码看,透明度为255时就被人为取消了WS_EX_LAYERED风格,显然这是不合理的。窗口不设置透明度,也有可能要实现异形窗口,也是需要WS_EX_LAYERED风格的。于是修改了一下,即不设置透明度(即nOpacity=255),也不能取消WS_EX_LAYERED:(将取消WS_EX_LAYERED风格代码删除掉)

  1. DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  2. DWORD dwNewStyle = dwStyle;
  3. if (nOpacity >= 0 && nOpacity < 255)
  4. {
  5.     dwNewStyle |= WS_EX_LAYERED;
  6. }
  7.  
  8. if (dwStyle != dwNewStyle)
  9. {
  10.    ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  11. }

5.2、 先调用了SetLayeredWindowAttributes接口,导致后面调用UpdateLayeredWindow失败

        但修改代码之后,UpdateLayeredWindow函数还是会调用失败,错误码还是87。于是回到CPaintManagerUI::SetTransparent这个接口,发现了第二个问题,没设置透明度(即nOpacity=255),代码中还是调用了SetLayeredWindowAttributes,而这个SetLayeredWindowAttributes函数调用是和UpdateLayeredWindow互斥的,一旦调用了SetLayeredWindowAttributes函数,UpdateLayeredWindow函数调用就会失败,于是又修改了此处的代码,即没设置透明度(即nOpacity=255),就不要调用SetLayeredWindowAttributes函数:(增加了一个需要设置透明度的标记变量bNeedSetWindowOpacity)

  1. if (bNeedSetWindowOpacity)
  2. {
  3.     fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
  4. }

综上,修改后的完整代码如下:

  1. void CPaintManagerUI::SetTransparent(int nOpacity)
  2. {
  3. if (NULL == m_hWndPaint)
  4. {
  5. return;
  6. }
  7. typedef BOOL(__stdcall *PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
  8. PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes;
  9. HMODULE hUser32 = ::GetModuleHandle(_T(""));
  10. if (hUser32)
  11. {
  12. fSetLayeredWindowAttributes =
  13. (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
  14. if (NULL == fSetLayeredWindowAttributes)
  15. {
  16. return;
  17. }
  18. }
  19. BOOL bNeedSetWindowOpacity = FALSE;
  20. DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  21. DWORD dwNewStyle = dwStyle;
  22. if (nOpacity >= 0 && nOpacity < 255)
  23. {
  24. dwNewStyle |= WS_EX_LAYERED;
  25. bNeedSetWindowOpacity = TRUE;
  26. }
  27. else
  28. {
  29. // 1、问题1:不设置透明度,即nOpacity=255,也不应该取消WS_EX_LAYERED风格
  30. // 可能外部要实现异形窗口,是需要WS_EX_LAYERED窗口风格
  31. //dwNewStyle &= ~WS_EX_LAYERED;
  32. }
  33. if (dwStyle != dwNewStyle)
  34. {
  35. ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  36. }
  37. // 问题2:不设置透明度,即nOpacity=255,是不需要调用SetLayeredWindowAttributes接口
  38. // 不管有没有设置透明度,都去调用SetLayeredWindowAttributes,可能和异形窗口调用UpdateLayeredWindow
  39. if (bNeedSetWindowOpacity)
  40. {
  41. fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
  42. }
  43. }

       上述问题的排查过程以及实战功能实现的完整案例,可以参见我之前写的文章:

开发实例分享 | 使用Layered分层窗口实现视频会议中的桌面区域共享(使用分层窗口将部分区域透掉,鼠标可穿透)/chenlycly/article/details/139588302

       关于SetLayeredWindowAttributes调用后再调用UpdateLayeredWindow会失败的详细说明,可以查看我的文章:

duilib中设置窗口透明度的接口CPaintManagerUI::SetTransparent有问题导致使用duilib窗口实现异形窗口无效的排查/chenlycly/article/details/140687246

6、弹出一个dui窗口的模态框时要将自销毁标记置为false

6.1、dui窗口对象的自销毁实现说明

        在duilib框架中,dui窗口对象在窗口销毁时默认是自销毁的(delete this),相关代码如下所示:

  1. LRESULT CALLBACK CWindowWnd::__WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  2. {
  3.     CWindowWnd* pThis = NULL;
  4.     if( WM_NCCREATE == uMsg ) 
  5.     {
  6.         LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  7.         pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  8.         pThis->m_hWnd = hWnd;
  9.         ::SetWindowLongPtr( hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis) );
  10.     } 
  11.     else 
  12.     {
  13.         pThis = reinterpret_cast<CWindowWnd*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
  14.         if( WM_NCDESTROY == uMsg && pThis != NULL
  15.         {
  16.             LRESULT lRes = ::CallWindowProc( pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam );
  17.             ::SetWindowLongPtr( pThis->m_hWnd, GWLP_USERDATA, 0L );
  18.             if( pThis->m_bSubclassed ) 
  19.             {
  20.                 pThis->Unsubclass();
  21.             }
  22.             pThis->m_hWnd = NULL;
  23.             pThis->OnFinalMessage( hWnd );
  24.             return lRes;
  25.         }
  26.     }
  27.     if( pThis != NULL
  28.     {
  29.         return pThis->HandleMessage( uMsg, wParam, lParam );
  30.     } 
  31.     else 
  32.     {
  33.         return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
  34.     }
  35. }
  36. void CXXXWindow::OnFinalMessage( HWND /*hWnd*/ ) 
  37.     if( m_bDeleteSelf )
  38.     {
  39.         delete this// 自动销毁当前的窗口类
  40.     }
  41. }

窗口销毁时会产生WM_NCDESTROY消息,该消息进入窗口处理过程函数中处理,会调用OnFinalMessage接口,该接口中会自动将该窗口对象销毁。

6.2、自销毁特性引发的崩溃问题

       这个自销毁的特性,在某些场景下可能会引发问题。可以来看如下的一段代码:

  1. class CMessageBoxDlg : public WindowImplBase
  2. {
  3. public:
  4. CMessageBoxDlg( STRINGorID strUI, unsigned int dwTransparent = 255, bool bDeleteSelf = true );   
  5. virtual ~CMessageBoxDlg();
  6. // ...(其余代码省略)
  7. }
  8. void ShowMsgBoxTip()
  9. {
  10.     CMessageBoxDlg dlg(IDR_XML_MSG_NOTIFY_DLG);
  11.     dlg.SetInfo(_T("文件正被占用中,请稍后再发送"), STRING_TIP, m_hParentWnd, ID_OK);
  12.     dlg.Create(m_hParentWnd, STRING_TIP, UI_WNDSTYLE_BOX, WS_EX_TOOLWINDOW);
  13.     dlg.CenterWindow(); // 居中显示
  14.     dlg.ShowModal();
  15. }

首先,定义了窗口类CMessageBoxDlg,其构造函数中第三个参数是bDeleteSelf自销毁选项。我们在用这个窗口类定义一个局部变量,然后创建这个窗口,然后调用ShowModal显示模态框。弹出该模态提示框,点击确定,将窗口关闭(窗口将被销毁),ShowModal返回,dui框架中会自动将该CMessageBoxDlg dlg对象自动delete掉,就会产生崩溃!

       为什么会产生崩溃呢?因为CMessageBoxDlg dlg对象是局部变量,对象的内存是在栈上分配的,而delete释放的是堆内存,去释放栈内存肯定就有问题了!新手可能会犯这样的错误!关于C++程序内存的五大分区,可以去查看文章:

实例详解C++程序的五大内存分区/chenlycly/article/details/120958761       解决办法很简单,在定义CMessageBoxDlg dlg对象时将第三个参数bDeleteSelf设置为false,就是不要dui框架去自动销毁了:

  1. // 第三个参数bDeleteSelf设置false,让dui框架不要自动delete窗口对象
  2. CMessageBoxDlg dlg(IDR_XML_MSG_NOTIFY_DLG, 255, false);

因为是局部变量,该局部变量的内存在所在的函数ShowMsgBoxTip退出时会自动释放。

       如果CMessageBoxDlg对象是new出来的,如下:

  1. CMessageBoxDlg* pDlg = new CMessageBoxDlg(IDR_XML_MSG_NOTIFY_DLG);
  2. pDlg->SetInfo(strTip, STRING_TIP, NULL);
  3. pDlg->Create(g_pMainLogic->GetMainHwnd(), STRING_ERR_TIP, UI_WNDSTYLE_BOX, WS_EX_TOOLWINDOW);
  4. pDlg->CenterWindow();
  5. pDlg->ShowWindow(SW_SHOW);

此时是可以依赖dui框架的自销毁的。

7、CMenuWnd菜单窗口中的菜单项相关实现细节

7.1、设置菜单窗口属性以及构建菜单窗口中的菜单项

       可以直接在xml中添加菜单项,也可以实时去new一个菜单项(new CMenuElementUI)添加到菜单窗口CMenuWnd中。但不管哪种方式,都需要在xml中设置菜单窗口的基本属性,如下:

包括菜单项的文字颜色itemtextcolor、悬浮选中时的颜色itemselectedtextcolor、菜单项背景图片itembkimage、悬浮选中时的背景图片itemselectedimage等。

       上面的截图中,就是直接在xml文件中添加菜单项的,有两个菜单项。也可以动态地去new一个CMenuElementUI菜单项,添加到菜单窗口CMenuWnd中,如下所示:

  1. CMenuWnd* pMenu = new CMenuWnd( IDR_XML_DEFAULT_MENU_CONFIG, _T("xml"), &m_pm );
  2. ASSERT( pMenu != NULL );
  3. // 添加菜单项1
  4. CMenuElementUI* pElement = new CMenuElementUI();
  5. pElement->SetName( _T("MenuOpenOper") );
  6. pElement->SetText( STRING_WB_OPEN_DATA_OPER );
  7. pElement->SetFixedHeight( MENU_ITEM_HEIGHT );
  8. pMenu->AddMenuItem( pElement );
  9. // 添加菜单项2
  10. pElement = new CMenuElementUI();
  11. pElement->SetName( _T("MenuCloseOper") );
  12. pElement->SetText( STRING_WB_CLOSE_DATA_OPER );
  13. pElement->SetFixedHeight( MENU_ITEM_HEIGHT );
  14. pMenu->AddMenuItem( pElement );
  15. // 调用TrackPopupMenu将菜单窗口显示出来
  16. RECT rcBtnDataOper = pBtnDataOper->GetPos();
  17. CPoint pt;
  18. = ;
  19. = ;
  20. ::ClientToScreen( GetHWND(), &pt );
  21. pMenu->TrackPopupMenu( eMenuAlignment_Left | eMenuAlignment_Top, pt, GetHWND() );

7.2、操作菜单项的其他接口

       此外,还有几个操作菜单项的其他接口。

       可以调用CMenuWnd::EnableMenuItem接口根据菜单控件名称将菜单项置灰或者使能

pMenu->EnableMenuItem( _T("menu_open_oper"), false );

       可以调用CMenuWnd::ModifyMenu接口,修改菜单项的文字

pMenu->ModifyMenu(_T("menu_ctrl"),  _T("控制"));

       可以调用CMenuWnd::DeleteMenuItem删除菜单项:(比如我们在xml中配置了固定的几个菜单项,如果有个菜单项不要,可以通过代码删除)

pMenu->DeleteMenuItem(_T("menu_shortmsg"));

       还可以在菜单项之间设置一个分割线项,该分割线项只设置一个像素高度,设置一条横线的背景图即可:

  1. // 分割线,设置一个像素高度,设置一条横线的背景图
  2. CControlUI* pControl = new CControlUI();
  3. pControl->SetName(MENU_SEPARATOR_NAME);
  4. pControl->SetFixedHeight(1);
  5. pControl->SetBkImage(MENU_SEPARATOR_BK_IMG);
  6. pMenuWnd->AddMenuItem(pControl);

8、在格式化CStdString字符串变量时要注意的问题

8.1、将CStdString::Format接口中不支持浮点的wvsprintf接口换成_vstprintf_s

        在CStdString类的格式化字符串的Format接口中,要将不支持浮点数的wvsprintf接口,换成支持浮点数的_vstprintf_s,代码如下:

  1. int CStdString::Format( LPCTSTR pstrFormat, ... )
  2. {
  3.     CStdString sFormat = pstrFormat;
  4.     // Do ordinary printf replacements
  5.     // NOTE: Documented max-length of wvsprintf() is 1024
  6.     TCHAR szBuffer[1025] = { 0 };
  7.     va_list argList;
  8.     va_start( argList, pstrFormat );
  9.     // wvsprintf不支持浮点格式,所以换成_vstprintf,by 2013/12/02
  10.     // 使用_vstprintf_s安全一点
  11.     int iRet = ::_vstprintf_s( szBuffer, sizeof(szBuffer)/sizeof(TCHAR), sFormat, argList );
  12.     va_end( argList );
  13.     Assign( szBuffer );
  14.     return iRet;
  15. }

8.2、在格式化CStdString对象时遇到的崩溃问题 

        我们在项目中格式化CStdString对象中的字符串时就遇到了一个很典型的问题,甚至引发了崩溃。我们看如下的一段示例代码:

  1. CStdString strLog;
  2. CStdString strName;
  3. CStdString strId;
  4. // ....   获取名称和Id的代码省略
  5. // 将strName和strId中的字符串格式化
  6. strLog.Format(_T("strName: %s, strId: %s"),strName,strId );

就是这句格式化的代码有问题!咋一看上去,好像没有问题,但从CStdString类的成员变量构成以及函数调用时参数是通过栈传递的角度,就会发现问题了!       

       CStdString类有两个成员变量:m_pstr和m_szBuffer数组,如下所示:

  1. class DIRECTUI_API CStdString
  2. {
  3. public:
  4.     enum { MAX_LOCAL_STRING_LEN = 16 };
  5.     CStdString();                                    // 初始化字符串类
  6.     CStdString( const TCHAR ch );
  7.     // 该类中间部分的代码全部省略
  8. protected:
  9.     LPTSTR m_pstr;                                // 字符指针
  10.     TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN];        // 字符缓冲区
  11. };

对于有问题的Format这句代码:

strLog.Format(_T("strName: %s, strId: %s"),strName,strId );

在执行进Format这个接口之前,传入的参数strName和strId内存中的内容压到栈上,通过栈传递给被调用函数Format的,所以会把CStdString类的两个成员变量m_pstr和m_szBuffer数组内存中的内容压到栈上,而有两个参数strName和strId,相当于要将4个变量的内存压到栈上,而前面的格式化符号只有两个,目的是要将strName和strId中存放的字符串进行格式化的。

       格式化函数Format内部(函数的更底部)会依次根据解析出来的格式化符,依次到栈上取出格式化符对应的变量的值,去格式化。因为strName将两个成员变量m_pstr和m_szBuffer数组内存中内容压到栈上,所以在解析出第二个%s后到栈上拿数据,就错位了,就不是想要拿的strId中存放的字符串了!

       要搞懂这个问题,首先要了解函数调用时参数的值是如何通过栈内存传递给被调用函数的,以及格式化函数内存时如何通过解析出来的格式化符到栈上拿数据的。

       关于函数调用的栈分布,可以查看我之前写的文章:

C++函数调用栈分布详解/chenlycly/article/details/121001096      关于格式化相关的项目实战问题分析实例,可以查看我之前写的文章:

将string类对象中的内容格式化到字符串buffer中时遇到的异常崩溃分析/chenlycly/article/details/126211718UINT64整型数据在格式化时使用了不匹配的格式化符%d导致其他参数无法打印的问题排查/chenlycly/article/details/132549186       这个问题的解决办法很简单,当一个类中包含不止一个成员时,应该明确类中的哪个数据成员要参与格式化!对于本例中的CStdString类对象,要格式化类中存放的字符串,直接引用类中的成员变量m_pstr即可,可以调用CStdString::GetData获取即可,所以修改后的代码为:

strLog.Format(_T("strName: %s, strId: %s"),strName.GetData(),strId.GetData() );

9、常用布局CVerticalLayOutUI、CHorizontalLayoutUI和CTileLayoutUI的使用技巧

9.1、常用布局特性说明

       除了基本控件之外,duilib为了方便大家对界面控件元素进行排列布局,提供了几个通用的布局,即垂直布局CVerticalLayOutUI、水平布局CHorizontalLayoutUI和平铺布局CTileLayoutUI等。

       这些布局均继承于容器类CContainerUI,CContainerUI中抽象出了一些共性的东西。我们在编写xml去构建UI界面时,主要使用这几个布局去排布窗口中的控件去实现界面的排布,其中垂直布局CVerticalLayOutUI和水平布局CVerticalLayOutUI最为常用。不同的布局,其内部的控件排布方法是不同的(具体的控件排布控制可以查看布局类的SetPos接口),要根据界面的构成,划分出不同的区域,选择不同的布局。

       在垂直布局CVerticalLayOutUI中,控件是在垂直方向上排布的,如下所示:


       在水平布局CVerticalLayOutUI中,控件默认是在水平方向上排布的,如下所示:

       在平铺布局CTileLayoutUI中,控件默认是一个方格一个方格排布的,比如企业微信的表情窗口中一个个表情格:

        在实际编写界面xml时,基本都要在布局中嵌套布局,即布局中还有子布局,需要根据界面效果,灵活使用这些常用的布局! 

9.2、使用布局去构建UI界面的实例

       在实现UCD设计的UI界面时,需要对界面进行区域划分,划分出多个区域,每个区域中的控件怎么依赖所属的布局进行排布,以QQ登录界面为例:

讲解一下如何进行区域划分,如何使用布局实现区域中的控件排布。

       将上述界面划分成若干个区域,区域内再去控制控件的排放,选区域的原则是区域内方便使用布局去控制具体控件的排布。将整个界面先分成3大区域:水平布局1、水平布局2和水平布局3,这三个区域内部可以直接控制控件的排放,因为这三个区域正还是垂直方向上排布的,所以最外层使用垂直布局CVerticalLayOutUI。

怎么划分区域,也是要看经验,做的多了,就熟悉了!

       对于水平布局1,内部有三小块,左边固定位置的QQ图标(可设置固定宽度),右边是相对右边界固定的位置(设置固定宽度),中间这个区域可以使用占位符控件(除了左边和右边的固定宽度之外,余下的都留给占位符),这样这三小块在水平布局1内水平方向排布的,中间区域是占位符区域,可以直接使用水平布局的自动排布。

        对于水平布局2,水平方向上可以分三个区域,中间区域是固定宽度的头像区域,两边可以做成占位符区域,所以利用水平布局的自动排布,中间区域设置固定宽度,余下的宽度被左右两边的占位符平分,这样头像区域就自动居中了。

        对于水平布局3,也是水平方向上分出3大块,中间固定宽度的一块以及左右两边的占位符区域,这样中间区域设置固定宽度,余下宽度被两边的占位符区域平分,这样中间区域就自动居中了。然后,中间的区域又可以嵌套布局,根据控件的摆放位置,可以设置成垂直布局,让控件在竖直方向上做排布控制,就可以了!

        对于CVerticalLayOutUI、CHorizontalLayoutUI和CTileLayoutUI等布局,其内部排布控件的方法,查看这些类的SetPos接口就可以插看到。

10、为了给字体设置提高文字清晰度的ClearType属性,_WIN32_WINNT宏的值从0x500修改成0x501,导致ToolTip窗口不显示的问题

       为什么要将_WIN32_WINNT宏的值从0x500修改成0x501呢?当时我们的软件在设置高DPI的电脑上运行,系统会对软件界面进行放大,放大后字体会比较模糊,为了将文字显示的更清晰一点,给绘制文字的字体设置ClearType属性,这样绘制出来的文字会更有棱角一点,会清晰一些。操作系统也支持设置ClearType选项,如下所示:

       如何给字体设置ClearType属性呢?我们在使用LOGFONT结构体创建字体时,直接给LOGFONT结构体中的lfQuality字段设置CLEARTYPE_QUALITY值即可,如下所示:

       跳转到CLEARTYPE_QUALITY宏的定义处:

可以看到只有当定义的NT版本达到0x501,才定义这个CLEARTYPE_QUALITY宏,但我们的directui工程中将_WIN32_WINNT宏值定义为0x500,于是为了支持CLEARTYPE_QUALITY宏,直接_WIN32_WINNT的值由0x500改成0x501。

       结果测试发现,程序中所有的ToolTip窗口都不显示了,就与这个将_WIN32_WINNT的值由0x500改成0x501有关!此问题具体的细节以及解决的办法,可以参见我之前写的文章:

当_WIN32_WINNT大于0x500时,ToolTip窗口不显示问题排查/chenlycly/article/details/139565872