Duilib实现GroupBox控件
转载:http://blog.csdn.net/asd313346541/article/details/47055113
原作者的源码上说:右边线和下边线显示不出来:
后来经过调试研究测试猜测应该是没有给控件设置borderround属性,后来设置后出来效果:
最后放上源码(稍微做了修改):
.h文件
- #ifndef _UIGROUPBOX_H_
- #define _UIGROUPBOX_H_
- #define GROUPBOX_TEXT_OFFSET 40 //定义GroupBox中的Text相对于左边的偏移
- class CGroupBoxUI :public CContainerUI
- {
- public:
- CGroupBoxUI();
- ~CGroupBoxUI();
- virtual LPCTSTR GetClass() const;
- virtual LPVOID GetInterface(LPCTSTR pstrName);
- virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
- void PaintText(HDC hDC);
- void PaintBorder(HDC hDC);
- void SetTextColor(DWORD dwTextColor);
- void SetFont(int iFont);
- void SetShowHtml(bool bShowHtml);
- private:
- DWORD m_dwTextColor; ///字休颜色
- int m_iFont; ///字体号,大小
- bool m_bShowHtml; ///是否显示HTML代码
- int m_iTextWidth; ///Text文字宽
- int m_iTextHeigh; ///Text文字高
- };
- #endif//_UIGROUPBOX_H_
.cpp文件
- #include "stdafx.h"
- #include "UIGroupBox.h"
- CGroupBoxUI::CGroupBoxUI() :m_iFont(-), m_bShowHtml(false)
- {
- }
- CGroupBoxUI::~CGroupBoxUI()
- {
- }
- /// @return LPCTSTR 返回控件类名
- /// @note 本函数返回控件类,格式为LPCTSTR
- LPCTSTR CGroupBoxUI::GetClass() const
- {
- return TEXT("GroupBoxUI");
- }
- /// @return LPVOID类型
- /// @note 获取接口
- LPVOID CGroupBoxUI::GetInterface(LPCTSTR pstrName)
- {
- if (_tcscmp(pstrName, TEXT("GroupBox")) == )
- {
- return static_cast<CGroupBoxUI*>(this);
- }
- return CContainerUI::GetInterface(pstrName);
- }
- /// 设置控件属性
- /// @param pstrname 欲设置的属性名称,LPCTSTR类型
- /// @param pstrValue 欲设置的属性值,LPCTSTR类型
- /// @see CControlUI::SetAttribute()
- /// @note 重载基类,增加部分基类没有的属性
- void CGroupBoxUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
- {
- if (_tcscmp(pstrName, _T("font")) == ) SetFont(_ttoi(pstrValue));
- else if (_tcscmp(pstrName, _T("textcolor")) == )
- {
- if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
- LPTSTR pstr = NULL;
- DWORD clrColor = _tcstoul(pstrValue, &pstr, );
- SetTextColor(clrColor);
- }
- else if (_tcscmp(pstrName, _T("showhtml")) == )
- SetShowHtml(_tcscmp(pstrValue, _T("true")) == );
- else CControlUI::SetAttribute(pstrName, pstrValue);
- }
- /// 设置控件文字颜色
- /// @param dwTextColor 欲设置的文字颜色
- /// @note 设置文字颜色,并立即刷新
- void CGroupBoxUI::SetTextColor(DWORD dwTextColor)
- {
- m_dwTextColor = dwTextColor;
- Invalidate();
- }
- /// 设置控件字体
- /// @param iFont 欲设置的字体号
- /// @note 设置字体,并立即刷新
- void CGroupBoxUI::SetFont(int iFont)
- {
- m_iFont = iFont;
- Invalidate();
- }
- void CGroupBoxUI::SetShowHtml(bool bShowHtml)
- {
- if (m_bShowHtml == bShowHtml) return;
- m_bShowHtml = bShowHtml;
- Invalidate();
- }
- /// 关键部分
- void CGroupBoxUI::PaintText(HDC hDC)
- {
- //如果没有设置字体颜色,则用默认设置
- if (m_dwTextColor == )
- {
- m_dwTextColor = m_pManager->GetDefaultFontColor();
- }
- RECT rc; //文字输出位置
- rc = GetPos();
- rc.left = rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET; //这个地方采用了硬编码的方式,不知道怎么计算文字应该放的位置
- HFONT hOldFont = (HFONT)::SelectObject(hDC, m_pManager->GetFont(m_iFont));
- Gdiplus::Graphics graphics(hDC);
- Gdiplus::Font font(hDC);
- graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintSystemDefault);
- graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
- graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
- Gdiplus::RectF rectF((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top));
- Gdiplus::SolidBrush brush(Gdiplus::Color(, GetBValue(m_dwTextColor), GetGValue(m_dwTextColor), GetRValue(m_dwTextColor)));
- Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic();
- Gdiplus::RectF bounds;
- graphics.MeasureString(m_sText, -, &font, rectF, &stringFormat, &bounds);
- // MeasureString存在计算误差,这里加一像素
- rc.bottom = rc.top + (long)bounds.Height + ; //这两句是从UIRender.cpp中DrawText()中拷出来的,不知道意义何在
- rc.right = rc.left + (long)bounds.Width + ;
- m_iTextWidth = (int)bounds.Width;
- m_iTextHeigh = (int)bounds.Height;
- graphics.DrawString(m_sText, -, &font, rectF, &stringFormat, &brush);
- ::SelectObject(hDC, hOldFont);
- }
- void CGroupBoxUI::PaintBorder(HDC hDC)
- {
- RECT rc = GetPos(); //画框框时的位置
- rc.top += (m_iTextHeigh * ) / ; //最顶部的线移到Text的中下部
- int nSize = m_nBorderSize;
- //DWORD dwPenColor = m_dwBorderColor;
- Gdiplus::Graphics graphics(hDC);
- //消除锯齿
- graphics.SetSmoothingMode(SmoothingModeHighQuality);
- //const Gdiplus::Pen pen(Gdiplus::Color::Red, 1.0f);
- DWORD dwPenColor = GetAdjustColor(m_dwBorderColor);
- ASSERT(::GetObjectType(hDC) == OBJ_DC || ::GetObjectType(hDC) == OBJ_MEMDC);
- HPEN hPen = ::CreatePen(PS_SOLID | PS_INSIDEFRAME, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
- HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
- ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
- //定位四个圆角的位置
- RECT rcTopLeftCorner = { rc.left, rc.top, rc.left + * m_cxyBorderRound.cx, rc.top + * m_cxyBorderRound.cy };
- RECT rcTopRightCorner = { rc.right - * m_cxyBorderRound.cx, rc.top, rc.right, rc.top + * m_cxyBorderRound.cy };
- RECT rcBottomLeftCorner = { rc.left, rc.bottom - * m_cxyBorderRound.cy, rc.left + * m_cxyBorderRound.cx, rc.bottom };
- RECT rcBottomRightCorner = { rc.right - * m_cxyBorderRound.cx, rc.bottom - * m_cxyBorderRound.cy, rc.right, rc.bottom };
- //画四个圆角
- const Gdiplus::Pen pen(Gdiplus::Color(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)), (float)m_nBorderSize);
- graphics.DrawArc(&pen, rcTopLeftCorner.left, rcTopLeftCorner.top, rcTopLeftCorner.right - rcTopLeftCorner.left, rcTopLeftCorner.bottom - rcTopLeftCorner.top, 180.0f, 90.0f);//左上角
- graphics.DrawArc(&pen, rcTopRightCorner.left, rcTopRightCorner.top, rcTopRightCorner.right - rcTopRightCorner.left, rcTopRightCorner.bottom - rcTopRightCorner.top, 270.0f,90.0f);//右上角
- graphics.DrawArc(&pen, rcBottomLeftCorner.left, rcBottomLeftCorner.top, rcBottomLeftCorner.right - rcBottomLeftCorner.left, rcBottomLeftCorner.bottom - rcBottomLeftCorner.top, 90.0f, 90.0f);//左下角
- graphics.DrawArc(&pen, rcBottomRightCorner.left, rcBottomRightCorner.top, rcBottomRightCorner.right - rcBottomRightCorner.left-, rcBottomRightCorner.bottom - rcBottomRightCorner.top-, 0.0f, 90.0f);//右下角
- //画线----GDI
- MoveToEx(hDC, rc.left, rc.top + m_cxyBorderRound.cy, NULL); //左边线
- LineTo(hDC, rc.left, rc.bottom - m_cxyBorderRound.cy);
- MoveToEx(hDC, rc.left + m_cxyBorderRound.cx, rc.top, NULL); //上第一条线
- LineTo(hDC, rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET - , rc.top); //-5 是为了给Text增加左边间距
- MoveToEx(hDC, rc.left + m_cxyBorderRound.cx + GROUPBOX_TEXT_OFFSET + m_iTextWidth + , rc.top, NULL); //上第二条线,+5是为了给Text增加右边间距
- LineTo(hDC, rc.right - m_cxyBorderRound.cx, rc.top);
- MoveToEx(hDC, rc.right, rc.top + m_cxyBorderRound.cy, NULL); //右边线
- LineTo(hDC, rc.right, rc.bottom - m_cxyBorderRound.cy);
- MoveToEx(hDC, rc.left + m_cxyBorderRound.cx, rc.bottom, NULL); //下边线
- LineTo(hDC, rc.right - m_cxyBorderRound.cx, rc.bottom);
- ::SelectObject(hDC, hOldPen);
- ::DeleteObject(hPen);
- }
xml布局:
- <GroupBox text="测试" textcolor="#FF989898" float="true" pos="125,55,0,0" width="256" height="80" bordercolor="#FF989898" bordersize="1" borderround="4,4" font="1"/>
在窗口中自定义控件:
- CControlUI* CFrameWnd::CreateControl(LPCTSTR pstrClass)
- {
- if (_tcscmp(pstrClass,_T("GroupBox")) == )
- {
- return new CGroupBoxUI;
- }
- return NULL;
- }
源码demo:GroupBox控件
Duilib实现GroupBox控件的更多相关文章
- 改进duilib的richedit控件的部分功能
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41208207 如果要使用透明异形窗体功能,首先要改进duilib库让他本 ...
- duilib修复ActiveXUI控件bug,以支持flash透明动态背景
转载请说明原出处,谢谢~~ 昨天在QQ控件里和同学说起QQ2013登陆窗体的开发,从界面角度考虑,单单一个登陆界面是很容易做出来的.腾讯公司为了 防止各种盗号行为可谓煞费苦心,QQ2013采用了动态背 ...
- 将webkit内核封装为duilib的浏览器控件
转载请说明出处,谢谢~~ 原本的duilib是自带浏览器控件的,但是使用了IE内核,我在做仿酷狗音乐播放器时,在右侧乐库要用到浏览器控件,而我使用自带的IE控件却发现了不少缺点,这也是duilib一直 ...
- winform groupbox控件放到窗体中间位置
1. 在Form中放一个控件,让其在启动时始终居中 int gLeft = this.Width / 2 - groupControl1.Width / 2; int gTop = this.Heig ...
- C#之菜单控件、主窗体打开子窗体、GroupBox控件使用
一.背景 一年前有学习过C#,但没有在项目中去实际做APP,重新捡起来应用到项目中.我同事本来做好一个CANOPEN设备管理的界面,由于近期搜索了别人的开发的界面,我觉得有很多东西要重新安排,以及我已 ...
- 扩展GroupBox控件
1.GroupBox的边框颜色可以自行设置: 2.GroupBox可以设置边框的为圆角: 3.设置GroupBox标题在控件中的位置. 4.设置GroupBox标题的字体和颜色. 具体实现步骤Pane ...
- WinForm GroupBox控件重绘外观
private void groupBoxFun_Paint(PaintEventArgs e, GroupBox groupBox){ e.Graphics.Clear(groupBox.BackC ...
- duilib 绘制IP控件
在使用duilib时,发现本来的库并没有提供IP控件,如是自己想到绘制IP控件,控件的绘制不难,首先赋值UIEdit的两个文件,命名为UIIPEdit,更改完成后,便可以进行修改绘制IP控件. 绘制难 ...
- duilib 增加gif控件(基于gdi+,可控制播放暂停,自动设置大小)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42502081 因为项目需要我需要给duilib增加一个gif控件,目前已 ...
随机推荐
- JQuery获取和设置Select选项常用方法总结
1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&quo ...
- oracle数据库的归档模式
1:开发环境和测试环境中,数据库的日志模式和自动归档模式一般都是不设置的,这样有利于系统应用的调整,也免的生成大量的归档日志文件将磁盘空间大量的消耗. 2:生产环境时,将其设置为日志模式并自动归档就相 ...
- 夺命雷公狗---DEDECMS----12dedecms全局标签的使用以及嵌套标签的使用
在网站开发中,在很多页面可能会使用到同一个变量,比如路径网站信息等,所以我们可以用全局变量来使用. 默认的放在: 进去里面看下就会发现很多的常量都是在这里定义的: 我们在实际开发的时候可以将我们在多个 ...
- SQL——存储过程
1. 为什么使用存储过程 应用程序通过T-SQL语句到服务器的过程是不安全的. 1) 数据不安全 2)每次提交SQL代码都要经过语法编译后在执行,影响应用程序的运行性能 3) 网络流量大 2. 什么是 ...
- 【crunch bang】tint2配置2
# Tint2 config file # Background definitions # ID 1 rounded = 0 border_width = 0 background_color = ...
- 使用QTP对Flight的登录界面进行测试
一.测试用例设计 现在使用QTP对案例程序进行测试, 设计测试用例的要求为: 用户名长度大于等于6个字符 必须为字母[o-z,O-Z]和数字[0-9]组成 不能为空,空格或者特殊字符 正确的密码为:M ...
- TVideoGrabber的使用(一)捕捉摄像头
使用TVideoGrabber捕捉摄像头,相当容易,只需几句代码即可解决问题,首先我们新建一个工程,然后从控件面板上拉取一个 TVideoGrabber控件到窗体中,然后再在窗体上放置四个Button ...
- MySQL OnlineDDL
参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...
- Spring的javaMail邮件发送(带附件)
项目中经常用到邮件功能,在这里简单的做一下笔记,方便日后温习. 首先需要在配置文件jdbc.properties添加: #------------ Mail ------------ mail.smt ...
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件
之前,我们已经介绍了 jQuery UI 库,CSS 框架.下面,我们将学习这些有增强可视化效果,高度可配置的用户交互组件. Tab 的特性是,点击 tab 后,会高亮该 tab,并显示他的关联con ...