📄 xpgroupbox.cpp
字号:
// XPGroupBox.cpp : implementation file
//
#include "stdafx.h"
#include "XPGroupBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CXPGroupBox Version 1.0
//
//
// Thanks to: NT ALMOND's class CLable.
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CXPGroupBox,CButton);
/////////////////////////////////////////////////////////////////////////////
// CXPGroupBox
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::CXPGroupBox
//
// Description: Default contructor
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXPGroupBox::CXPGroupBox()
{
m_strTitle = _T("");
m_clrBorder = ::GetSysColor(COLOR_3DSHADOW);
m_clrClientBackground = ::GetSysColor(COLOR_BTNFACE);
m_clrTitleText = ::GetSysColor(COLOR_WINDOWTEXT);
m_clrTitleBackground = ::GetSysColor(COLOR_BTNFACE);
m_nType = XPGB_FRAME;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::~CXPGroupBox
//
// Description: Default destructor
// Note: Thanks John A. Johnson
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
// Jack Jin 2003-12-03 1.01 fix paint problem with help of John A. Johnson
//////////////////////////////////////////////////////////////////////////
CXPGroupBox::~CXPGroupBox()
{
}
BEGIN_MESSAGE_MAP(CXPGroupBox, CButton)
//{{AFX_MSG_MAP(CXPGroupBox)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXPGroupBox message handlers
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::OnPaint
//
// Description: Handles all the drawing code for the label
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//////////////////////////////////////////////////////////////////////////
void CXPGroupBox::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rectClient;
GetClientRect(rectClient);
// Defalte Rect
rectClient.DeflateRect(1,1);
// Get Text Rect
CSize sizeText;
CRect rectText, rectFrame;
CRect rectTitle, rectContent;
CFont *pOldFont = dc.SelectObject(&m_font);
// get Text if need
if ( m_strTitle.IsEmpty() )
{
GetWindowText(m_strTitle);
if ( ! m_strTitle.IsEmpty() )
m_strTitle = _T(" ") + m_strTitle + _T(" ");
}
if ( ! m_strTitle.IsEmpty() )
{
sizeText = dc.GetTextExtent(m_strTitle);
}
else
{
sizeText.cx = 0;
sizeText.cy = 0;
}
if ( m_nType == XPGB_FRAME ) // Frame style
{
// Calculate Text Rect
rectText.top = rectClient.top;
rectText.left = rectClient.left + 10;
rectText.bottom = rectText.top + sizeText.cy;
rectText.right = rectText.left + sizeText.cx;
// Calculate Frame rect
rectFrame.left = rectClient.left;
rectFrame.top = rectClient.top + sizeText.cy/2;
rectFrame.right = rectClient.right;
rectFrame.bottom = rectFrame.top + rectClient.Height() - sizeText.cy/2;
// Draw Frame border
CPen penFrame;
CBrush brushBKFrame(m_clrTitleBackground);
penFrame.CreatePen(PS_SOLID, 1, m_clrBorder);
CPen* pOldPen = dc.SelectObject(&penFrame);
CBrush* pOldBrush = (CBrush*)dc.SelectStockObject(NULL_BRUSH);
dc.RoundRect(rectFrame, CPoint(10,10));
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
dc.IntersectClipRect(rectText);
dc.FillSolidRect(rectText, m_clrTitleBackground);
}
else // Windows Style
{
// Calculate Title size
rectTitle.top = rectClient.top;
rectTitle.left = rectClient.left ;
rectTitle.right = rectClient.right;
rectTitle.bottom = rectClient.top + sizeText.cy + 4;
// Draw Title round rect
CPen penFrame;
CBrush brushBKTitle(m_clrTitleBackground);
CBrush brushBKContent(m_clrClientBackground);
penFrame.CreatePen(PS_SOLID, 2, m_clrBorder);
CPen* pOldPen = dc.SelectObject(&penFrame);
CBrush* pOldBrush = dc.SelectObject(&brushBKTitle);
dc.RoundRect(rectClient, CPoint(10, 10));
dc.SelectObject(pOldBrush);
// Draw content area
rectContent.left = rectClient.left;
rectContent.top = rectClient.top + sizeText.cy + 4;
rectContent.right = rectClient.right;
rectContent.bottom = rectContent.top + rectClient.Height() - sizeText.cy - 4;
pOldBrush = dc.SelectObject(&brushBKContent);
dc.Rectangle(rectContent);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
rectText.top = rectTitle.top + 2;
rectText.left = rectTitle.left + 2 ;
rectText.bottom = rectText.top + sizeText.cy;
rectText.right = rectText.left + sizeText.cx ;
}
COLORREF clrOldText = dc.SetTextColor(m_clrTitleText);
UINT nMode = dc.SetBkMode(TRANSPARENT);
dc.DrawText(m_strTitle, &rectText, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_NOCLIP ); //DT_END_ELLIPSIS);
// restore DC
dc.SetBkMode(nMode);
dc.SetTextColor(clrOldText);
dc.SelectObject(pOldFont);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::Create
//
// Description: Modify the groupbox windows style
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//////////////////////////////////////////////////////////////////////////
BOOL CXPGroupBox::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
dwStyle |= BS_ICON;
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::PreCreateWindow
//
// Description: Modify the groupbox windows style
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//////////////////////////////////////////////////////////////////////////
BOOL CXPGroupBox::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
cs.style |= BS_ICON;
return CButton::PreCreateWindow(cs);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::PreSubclassWindow
//
// Description: Modify the groupbox windows style
//
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//////////////////////////////////////////////////////////////////////////
void CXPGroupBox::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CButton::PreSubclassWindow();
//modified the style to avoid text overlap when press tab
ModifyStyle(0, BS_ICON);
// Get Defalut Font
CFont* cf = GetFont();
if(cf !=NULL)
{
cf->GetObject(sizeof(m_lf),&m_lf);
}
else
{
GetObject(GetStockObject(SYSTEM_FONT),sizeof(m_lf),&m_lf);
}
ReconstructFont();
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXPGroupBox::ReconstructFont
//
// Description: Construct font
//
//
// Name Date Version Comments
// Jack Jin 2003-12-03 1.0 Origin
//////////////////////////////////////////////////////////////////////////
void CXPGroupBox::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
ASSERT(bCreated);
}
//////////////////////////////////////////////////////////////////////////
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -