转载: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. Android 仿土巴兔选择效果

    1,前两天在群里看到有人在讨论土巴兔的选择装修风格的效果,自己也想实现,果断百度一下,有些好的文章,就花了些时间来分析了下,先看看别人土巴兔原装的功能 2,可以看到,基本上可以使用一个vviewpag ...

  2. visualvm添加远程管理-centos

    VisualVM连接远程服务器有两种方式:JMX和jstatd,两种方式都不能完美支持所有功能,例如JMX不支持VisualGC,jstatd不支持CPU监控,实际使用可同时配置上并按需选用. 1.修 ...

  3. PAT乙级 1017. A除以B (20)

    1017. A除以B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求计算A/B,其中A是不超过 ...

  4. windows下gvim与gcc的一键环境的搭建

    此处略去在windows中配置gcc的方法.默认你已经能够在命令提示符下直接使用gcc了. 其实就是写了一个vimscript的.vim文件,然后在 _vimrc 中使用source命令引用进来. 以 ...

  5. 终于有SpringMvc与Struts2的对比啦(转)

    本文转自:http://www.itsource.cn/web/news/5/20150318/370.html 目前企业中使用SpringMvc的比例已经远远超过Struts2,那么两者到底有什么区 ...

  6. python DB.fetchall()--获取数据库所有记录列表

    查询到的数据格式为列表: 多个元素的列表:

  7. 将edit ctrL弄的像个dos

    case WM_CTLCOLOREDIT: { HWND hShellText = GetDlgItem(hDlg,IDC_TXT_SHELL); if (hShellText == (HWND)lP ...

  8. Can't create/write to file '/tmp/#sql_887d_0.MYD' (Errcode: 17)

    lsof |grep "#sql_887d_0.MYD" 如果没有被占用就可以删掉 . https://wordpress.org/support/topic/cant-creat ...

  9. DataGuard主备归档存在gap的处理办法

    DataGuard主备之间可能由于网络等原因,造成备库和主库之间的归档日志不一致,这样就产生了gap. 解决gap的步骤: 1.在备库获得gap的详细信息 2.将需要的归档日志从主库拷贝到备库 3.备 ...

  10. Json常见问题

    1.创建包含 JSON 语法的 JavaScript 字符串: var txt = '{ "employees" : [' + '{ "firstName":& ...