转载: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控件的更多相关文章

  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. Linux 配置双机SSH信任

    一.实现原理 使用一种被称为"公私钥"认证的方式来进行ssh登录."公私钥"认证方式简单的解释是: 首先在客户端上创建一对公私钥(公钥文件:~/.ssh/id_ ...

  2. 【cube】SSAS(分析服务)优化手册

    优化前注意事项 1.数据质量.数据仓库中维度表的数据质量要严格保证.比如店仓的大区.省区.管理城市等属性和店仓代码没有一对多的上下级关系,这样在设计店仓维度时,就无法建立有效的层次结构.这会拖慢查询时 ...

  3. :nth-child()和:nth-of-type(n)区别

    ele:nth-child(n) 父元素下第n个元素且这个元素为ele ele:nth-of-type(n) 指父元素下第n个ele元素

  4. 取客户的银行帐号SQL

    SELECT ibybanks.bank_name, --银行 ibybanks.bank_branch_name, --分行 ibybanks.bank_account_num_electronic ...

  5. intall vs code in elementary os

    Open Terminal & Type Install Ubuntu Makesudo apt-get install ubuntu-make Microsoft Visual Studio ...

  6. 安装SQLServer2005错误无法在com+目录中安装和配置程序集

    无法在com+目录中安装和配置程序集c:\program files\Microsoft SQL Server\90\DTS\tasks\microsoft.sqlserver.MSMQTASK.DL ...

  7. 改Bug总结

    [1]屏蔽取舍法 屏蔽取舍,即所谓与问题无关的前后“语境”完全可以忽略,首先屏蔽掉,再根据问题复现路径查看问题发生的区间,然后逐近锁定“病灶”,确定需要修改的目标. [2]追溯原形法 追溯原形,即需要 ...

  8. SslUtil

    package com.eaju.util; import java.io.OutputStreamWriter;import java.net.URL;import java.net.URLConn ...

  9. 【python cookbook】【数据结构与算法】13.通过公共键对字典列表排序

    问题:想根据一个或多个字典中的值来对列表排序 解决方案:利用operator模块中的itemgetter()函数对这类结构进行排序是非常简单的. # Sort a list of a dicts on ...

  10. FormData对象实现文件Ajax上传

    后台: import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; imp ...