转载:http://blog.csdn.net/asd313346541/article/details/47055113

原作者的源码上说:右边线和下边线显示不出来:

后来经过调试研究测试猜测应该是没有给控件设置borderround属性,后来设置后出来效果:

最后放上源码(稍微做了修改):

.h文件

  1. #ifndef _UIGROUPBOX_H_
  2. #define _UIGROUPBOX_H_
  3.  
  4. #define GROUPBOX_TEXT_OFFSET 40 //定义GroupBox中的Text相对于左边的偏移
  5.  
  6. class CGroupBoxUI :public CContainerUI
  7. {
  8. public:
  9. CGroupBoxUI();
  10.  
  11. ~CGroupBoxUI();
  12.  
  13. virtual LPCTSTR GetClass() const;
  14.  
  15. virtual LPVOID GetInterface(LPCTSTR pstrName);
  16.  
  17. virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
  18.  
  19. void PaintText(HDC hDC);
  20.  
  21. void PaintBorder(HDC hDC);
  22.  
  23. void SetTextColor(DWORD dwTextColor);
  24.  
  25. void SetFont(int iFont);
  26.  
  27. void SetShowHtml(bool bShowHtml);
  28.  
  29. private:
  30. DWORD m_dwTextColor; ///字休颜色
  31. int m_iFont; ///字体号,大小
  32. bool m_bShowHtml; ///是否显示HTML代码
  33. int m_iTextWidth; ///Text文字宽
  34. int m_iTextHeigh; ///Text文字高
  35. };
  36.  
  37. #endif//_UIGROUPBOX_H_

.cpp文件

  1. #include "stdafx.h"
  2. #include "UIGroupBox.h"
  3.  
  4. CGroupBoxUI::CGroupBoxUI() :m_iFont(-), m_bShowHtml(false)
  5. {
  6.  
  7. }
  8.  
  9. CGroupBoxUI::~CGroupBoxUI()
  10. {
  11. }
  12.  
  13. /// @return LPCTSTR 返回控件类名
  14. /// @note 本函数返回控件类,格式为LPCTSTR
  15. LPCTSTR CGroupBoxUI::GetClass() const
  16. {
  17. return TEXT("GroupBoxUI");
  18. }
  19.  
  20. /// @return LPVOID类型
  21. /// @note 获取接口
  22. LPVOID CGroupBoxUI::GetInterface(LPCTSTR pstrName)
  23. {
  24. if (_tcscmp(pstrName, TEXT("GroupBox")) == )
  25. {
  26. return static_cast<CGroupBoxUI*>(this);
  27. }
  28.  
  29. return CContainerUI::GetInterface(pstrName);
  30. }
  31.  
  32. /// 设置控件属性
  33. /// @param pstrname 欲设置的属性名称,LPCTSTR类型
  34. /// @param pstrValue 欲设置的属性值,LPCTSTR类型
  35. /// @see CControlUI::SetAttribute()
  36. /// @note 重载基类,增加部分基类没有的属性
  37. void CGroupBoxUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  38. {
  39. if (_tcscmp(pstrName, _T("font")) == ) SetFont(_ttoi(pstrValue));
  40. else if (_tcscmp(pstrName, _T("textcolor")) == )
  41. {
  42. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  43. LPTSTR pstr = NULL;
  44. DWORD clrColor = _tcstoul(pstrValue, &pstr, );
  45. SetTextColor(clrColor);
  46. }
  47. else if (_tcscmp(pstrName, _T("showhtml")) == )
  48. SetShowHtml(_tcscmp(pstrValue, _T("true")) == );
  49.  
  50. else CControlUI::SetAttribute(pstrName, pstrValue);
  51. }
  52.  
  53. /// 设置控件文字颜色
  54. /// @param dwTextColor 欲设置的文字颜色
  55. /// @note 设置文字颜色,并立即刷新
  56. void CGroupBoxUI::SetTextColor(DWORD dwTextColor)
  57. {
  58. m_dwTextColor = dwTextColor;
  59. Invalidate();
  60. }
  61.  
  62. /// 设置控件字体
  63. /// @param iFont 欲设置的字体号
  64. /// @note 设置字体,并立即刷新
  65. void CGroupBoxUI::SetFont(int iFont)
  66. {
  67. m_iFont = iFont;
  68. Invalidate();
  69. }
  70.  
  71. void CGroupBoxUI::SetShowHtml(bool bShowHtml)
  72. {
  73. if (m_bShowHtml == bShowHtml) return;
  74.  
  75. m_bShowHtml = bShowHtml;
  76. Invalidate();
  77. }
  78.  
  79. /// 关键部分
  80. void CGroupBoxUI::PaintText(HDC hDC)
  81. {
  82. //如果没有设置字体颜色,则用默认设置
  83. if (m_dwTextColor == )
  84. {
  85. m_dwTextColor = m_pManager->GetDefaultFontColor();
  86. }
  87.  
  88. RECT rc; //文字输出位置
  89. rc = GetPos();
  90.  
  91. rc.left = rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET; //这个地方采用了硬编码的方式,不知道怎么计算文字应该放的位置
  92.  
  93. HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont));
  94. Gdiplus::Graphics graphics(hDC);
  95. Gdiplus::Font font(hDC);
  96. graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintSystemDefault);
  97. graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
  98. graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
  99. Gdiplus::RectF rectF((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top));
  100. Gdiplus::SolidBrush brush(Gdiplus::Color(, GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
  101.  
  102. Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic();
  103. Gdiplus::RectF bounds;
  104. graphics.MeasureString(m_sText, -, &font, rectF, &stringFormat, &bounds);
  105.  
  106. // MeasureString存在计算误差,这里加一像素
  107. rc.bottom = rc.top + (long)bounds.Height + ; //这两句是从UIRender.cpp中DrawText()中拷出来的,不知道意义何在
  108. rc.right = rc.left + (long)bounds.Width + ;
  109.  
  110. m_iTextWidth = (int)bounds.Width;
  111. m_iTextHeigh = (int)bounds.Height;
  112.  
  113. graphics.DrawString(m_sText, -, &font, rectF, &stringFormat, &brush);
  114.  
  115. ::SelectObject(hDC, hOldFont);
  116. }
  117.  
  118. void CGroupBoxUI::PaintBorder(HDC hDC)
  119. {
  120. RECT rc = GetPos(); //画框框时的位置
  121.  
  122. rc.top += (m_iTextHeigh * ) / ; //最顶部的线移到Text的中下部
  123.  
  124. int nSize = m_nBorderSize;
  125. //DWORD dwPenColor = m_dwBorderColor;
  126. Gdiplus::Graphics graphics(hDC);
  127.  
  128. //消除锯齿
  129. graphics.SetSmoothingMode(SmoothingModeHighQuality);
  130. //const Gdiplus::Pen pen(Gdiplus::Color::Red, 1.0f);
  131.  
  132. DWORD dwPenColor = GetAdjustColor(m_dwBorderColor);
  133. ASSERT(::GetObjectType(hDC) == OBJ_DC || ::GetObjectType(hDC) == OBJ_MEMDC);
  134. HPEN hPen = ::CreatePen(PS_SOLID | PS_INSIDEFRAME, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
  135. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  136. ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
  137.  
  138. //定位四个圆角的位置
  139. RECT rcTopLeftCorner = { rc.left, rc.top, rc.left + * m_cxyBorderRound.cx, rc.top + * m_cxyBorderRound.cy };
  140. RECT rcTopRightCorner = { rc.right - * m_cxyBorderRound.cx, rc.top, rc.right, rc.top + * m_cxyBorderRound.cy };
  141. RECT rcBottomLeftCorner = { rc.left, rc.bottom - * m_cxyBorderRound.cy, rc.left + * m_cxyBorderRound.cx, rc.bottom };
  142. RECT rcBottomRightCorner = { rc.right - * m_cxyBorderRound.cx, rc.bottom - * m_cxyBorderRound.cy, rc.right, rc.bottom };
  143.  
  144. //画四个圆角
  145. const Gdiplus::Pen pen(Gdiplus::Color(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)), (float)m_nBorderSize);
  146. graphics.DrawArc(&pen, rcTopLeftCorner.left, rcTopLeftCorner.top, rcTopLeftCorner.right - rcTopLeftCorner.left, rcTopLeftCorner.bottom - rcTopLeftCorner.top, 180.0f, 90.0f);//左上角
  147. graphics.DrawArc(&pen, rcTopRightCorner.left, rcTopRightCorner.top, rcTopRightCorner.right - rcTopRightCorner.left, rcTopRightCorner.bottom - rcTopRightCorner.top, 270.0f,90.0f);//右上角
  148. graphics.DrawArc(&pen, rcBottomLeftCorner.left, rcBottomLeftCorner.top, rcBottomLeftCorner.right - rcBottomLeftCorner.left, rcBottomLeftCorner.bottom - rcBottomLeftCorner.top, 90.0f, 90.0f);//左下角
  149. graphics.DrawArc(&pen, rcBottomRightCorner.left, rcBottomRightCorner.top, rcBottomRightCorner.right - rcBottomRightCorner.left-, rcBottomRightCorner.bottom - rcBottomRightCorner.top-, 0.0f, 90.0f);//右下角
  150.  
  151. //画线----GDI
  152. MoveToEx(hDC, rc.left, rc.top + m_cxyBorderRound.cy, NULL); //左边线
  153. LineTo(hDC, rc.left, rc.bottom - m_cxyBorderRound.cy);
  154.  
  155. MoveToEx(hDC, rc.left + m_cxyBorderRound.cx, rc.top, NULL); //上第一条线
  156. LineTo(hDC, rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET - , rc.top); //-5 是为了给Text增加左边间距
  157.  
  158. MoveToEx(hDC, rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET + m_iTextWidth + , rc.top, NULL); //上第二条线,+5是为了给Text增加右边间距
  159. LineTo(hDC, rc.right - m_cxyBorderRound.cx, rc.top);
  160.  
  161. MoveToEx(hDC, rc.right, rc.top + m_cxyBorderRound.cy, NULL); //右边线
  162. LineTo(hDC, rc.right, rc.bottom - m_cxyBorderRound.cy);
  163.  
  164. MoveToEx(hDC, rc.left + m_cxyBorderRound.cx, rc.bottom, NULL); //下边线
  165. LineTo(hDC, rc.right - m_cxyBorderRound.cx, rc.bottom);
  166.  
  167. ::SelectObject(hDC, hOldPen);
  168. ::DeleteObject(hPen);
  169. }

xml布局:

  1. <GroupBox text="测试" textcolor="#FF989898" float="true" pos="125,55,0,0" width="256" height="80" bordercolor="#FF989898" bordersize="1" borderround="4,4" font="1"/>

在窗口中自定义控件:

  1. CControlUI* CFrameWnd::CreateControl(LPCTSTR pstrClass)
  2. {
  3. if (_tcscmp(pstrClass,_T("GroupBox")) == )
  4. {
  5. return new CGroupBoxUI;
  6. }
  7. return NULL;
  8. }

源码demo:GroupBox控件

Duilib实现GroupBox控件的更多相关文章

  1. 改进duilib的richedit控件的部分功能

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...

  2. duilib修复ActiveXUI控件bug,以支持flash透明动态背景

    转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...

  3. 将webkit内核封装为duilib的浏览器控件

    转载请说明出处,谢谢~~ 原本的duilib是自带浏览器控件的,但是使用了IE内核,我在做仿酷狗音乐播放器时,在右侧乐库要用到浏览器控件,而我使用自带的IE控件却发现了不少缺点,这也是duilib一直 ...

  4. winform groupbox控件放到窗体中间位置

    1. 在Form中放一个控件,让其在启动时始终居中 int gLeft = this.Width / 2 - groupControl1.Width / 2; int gTop = this.Heig ...

  5. C#之菜单控件、主窗体打开子窗体、GroupBox控件使用

    一.背景 一年前有学习过C#,但没有在项目中去实际做APP,重新捡起来应用到项目中.我同事本来做好一个CANOPEN设备管理的界面,由于近期搜索了别人的开发的界面,我觉得有很多东西要重新安排,以及我已 ...

  6. 扩展GroupBox控件

    1.GroupBox的边框颜色可以自行设置: 2.GroupBox可以设置边框的为圆角: 3.设置GroupBox标题在控件中的位置. 4.设置GroupBox标题的字体和颜色. 具体实现步骤Pane ...

  7. WinForm GroupBox控件重绘外观

    private void groupBoxFun_Paint(PaintEventArgs e, GroupBox groupBox){ e.Graphics.Clear(groupBox.BackC ...

  8. duilib 绘制IP控件

    在使用duilib时,发现本来的库并没有提供IP控件,如是自己想到绘制IP控件,控件的绘制不难,首先赋值UIEdit的两个文件,命名为UIIPEdit,更改完成后,便可以进行修改绘制IP控件. 绘制难 ...

  9. duilib 增加gif控件(基于gdi+,可控制播放暂停,自动设置大小)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42502081 因为项目需要我需要给duilib增加一个gif控件,目前已 ...

随机推荐

  1. JQuery获取和设置Select选项常用方法总结

    1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&quo ...

  2. oracle数据库的归档模式

    1:开发环境和测试环境中,数据库的日志模式和自动归档模式一般都是不设置的,这样有利于系统应用的调整,也免的生成大量的归档日志文件将磁盘空间大量的消耗. 2:生产环境时,将其设置为日志模式并自动归档就相 ...

  3. 夺命雷公狗---DEDECMS----12dedecms全局标签的使用以及嵌套标签的使用

    在网站开发中,在很多页面可能会使用到同一个变量,比如路径网站信息等,所以我们可以用全局变量来使用. 默认的放在: 进去里面看下就会发现很多的常量都是在这里定义的: 我们在实际开发的时候可以将我们在多个 ...

  4. SQL——存储过程

    1. 为什么使用存储过程 应用程序通过T-SQL语句到服务器的过程是不安全的. 1) 数据不安全 2)每次提交SQL代码都要经过语法编译后在执行,影响应用程序的运行性能 3) 网络流量大 2. 什么是 ...

  5. 【crunch bang】tint2配置2

    # Tint2 config file # Background definitions # ID 1 rounded = 0 border_width = 0 background_color = ...

  6. 使用QTP对Flight的登录界面进行测试

    一.测试用例设计 现在使用QTP对案例程序进行测试, 设计测试用例的要求为: 用户名长度大于等于6个字符 必须为字母[o-z,O-Z]和数字[0-9]组成 不能为空,空格或者特殊字符 正确的密码为:M ...

  7. TVideoGrabber的使用(一)捕捉摄像头

    使用TVideoGrabber捕捉摄像头,相当容易,只需几句代码即可解决问题,首先我们新建一个工程,然后从控件面板上拉取一个 TVideoGrabber控件到窗体中,然后再在窗体上放置四个Button ...

  8. MySQL OnlineDDL

    参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...

  9. Spring的javaMail邮件发送(带附件)

    项目中经常用到邮件功能,在这里简单的做一下笔记,方便日后温习. 首先需要在配置文件jdbc.properties添加: #------------ Mail ------------ mail.smt ...

  10. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件

    之前,我们已经介绍了 jQuery UI 库,CSS 框架.下面,我们将学习这些有增强可视化效果,高度可配置的用户交互组件. Tab 的特性是,点击 tab 后,会高亮该 tab,并显示他的关联con ...