转载: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. 夺命雷公狗---DEDECMS----12dedecms全局标签的使用以及嵌套标签的使用

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

  2. 【sinatra】设置默认的端口

    加入 set :port, 8888 #默认4567

  3. php 获取mac地址

    <?php  /**   * 获取机器网卡的物理(MAC)地址* 目前支持WIN/LINUX系统   * 编辑: www.jbxue.com**/  class MacAddInfo {     ...

  4. 财务比率:ROE, 净利润增长率、毛利率、市盈率、PEG

    净资产收益率是判断资产回报率指标,是最重要的财务数据 净利润增长率企业成长的参考指标 净利率企业获利能力的指标 毛利率企业获利能力的指标 资产负债率企业偿还债务的能力,也是重要的风险指标之其他还要根据 ...

  5. Android上传头像代码,相机,相册,裁剪

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  6. Nginx防蜘蛛爬虫处理

    假定一个场景:某个网站它可能不希望被网络爬虫抓取,例如测试环境不希望被抓取,以免对用户造成误导,那么需要在该网站中申明,本站不希望被抓取.有如下方法: 方法一:修改nginx.conf,禁止网络爬虫的 ...

  7. ubuntu修改文件权限记录

    查看文件权限的命令: 在终端输入: ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些: -rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表 ...

  8. Delphi 的各版本定义,用于预编译参数中,避免忘记备忘之

    DELPHI的版本宏: VER80 - Delphi 1 VER90 - Delphi 2 VER100 - Delphi 3 VER120 - Delphi 4 VER130 - Delphi 5 ...

  9. 由于 add 运算符中“Chinese_PRC_CI_AS”和“Chinese_PRC_CS_AS_WS”之间的排序规则冲突

    有一个字段的排序规则是 Chinese_PRC_CS_AS_WS,字符串连接的时候报错. 处理方案 cast(columnName as varbinary) 即可

  10. php中替换函数主要用的几个函数strtr(),str_repalce()。

    php中替换函数主要有strtr(),str_repalce()这两个函数,今天介绍下他们的区别和用法, 先来看看这个php字符串替换函数 strtr()的两种用法: strtr(string,fro ...