| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /**************************************************************************************************
- 版本所有(C), 2020-2022, 北京普达迪泰
- ***************************************************************************************************
- 文 件 名: Label.cpp
- 功能描述: 自定义UI上的文本标签
- 版 本 号: 1.00初版
- 作 者: HYF
- 生成日期; 2021-12-20
- 最近修改:
- 函数列表:
- 修改历史:
- 1.日 期 :2021-12-20
- 作 者 : HYF
- 修改内容 : 创建文件
- **************************************************************************************************/
- #include "pch.h"
- #include "Resource.h"
- #include "Label.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CLabel
- CLabel::CLabel()
- {
- m_crText = RGB(112,112, 112);
- m_hBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
- ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);
- strcpy_s(m_lf.lfFaceName, _T("微软雅黑"));
- m_font.CreateFontIndirect(&m_lf);
- m_bTimer = FALSE;
- m_bState = FALSE;
- m_bLink = TRUE;
- m_hCursor = NULL;
- m_Type = None;
- m_bTrans = TRUE;
- m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
- }
- CLabel::~CLabel()
- {
- m_font.DeleteObject();
- ::DeleteObject(m_hBrush);
- }
- CLabel& CLabel::SetText(const CString& strText)
- {
- RedrawWindow();
- SetWindowText(strText);
- return *this;
- }
- CLabel& CLabel::SetTextColor(COLORREF crText)
- {
- m_crText = crText;
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetFontBold(BOOL bBold)
- {
- m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
- ReconstructFont();
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetFontUnderline(BOOL bSet)
- {
- m_lf.lfUnderline = bSet;
- ReconstructFont();
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetFontItalic(BOOL bSet)
- {
- m_lf.lfItalic = bSet;
- ReconstructFont();
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetSunken(BOOL bSet)
- {
- if (!bSet)
- ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
- else
- ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
- return *this;
- }
- CLabel& CLabel::SetBorder(BOOL bSet)
- {
- if (!bSet)
- ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
- else
- ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
- return *this;
- }
- CLabel& CLabel::SetFontSize(int nSize)
- {
- nSize*=-1;
- m_lf.lfHeight = nSize;
- ReconstructFont();
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetTransparent(BOOL bTrans)
- {
- m_bTrans = bTrans;
- RedrawWindow();
- return *this;
- }
- CLabel& CLabel::SetBkColor(COLORREF crBkgnd)
- {
- if (m_hBrush)
- ::DeleteObject(m_hBrush);
- m_hBrush = ::CreateSolidBrush(crBkgnd);
- return *this;
- }
- CLabel& CLabel::SetFontName(const CString& strFont)
- {
- strcpy_s(m_lf.lfFaceName,strFont);
- ReconstructFont();
- RedrawWindow();
- return *this;
- }
- BEGIN_MESSAGE_MAP(CLabel, CStatic)
- //{{AFX_MSG_MAP(CLabel)
- ON_WM_CTLCOLOR_REFLECT()
- ON_WM_TIMER()
- ON_WM_LBUTTONDOWN()
- ON_WM_SETCURSOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLabel message handlers
- HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- if (CTLCOLOR_STATIC == nCtlColor)
- {
- pDC->SelectObject(&m_font);
- pDC->SetTextColor(m_crText);
- if (m_bTrans)
- pDC->SetBkMode(TRANSPARENT);
- }
- if (m_Type == Background)
- {
- if (!m_bState)
- return m_hwndBrush;
- }
- return (HBRUSH)::GetStockObject(NULL_BRUSH);
- }
- void CLabel::ReconstructFont()
- {
- m_font.DeleteObject();
- BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
- ASSERT(bCreated);
- }
- CLabel& CLabel::FlashText(BOOL bActivate)
- {
- if (m_bTimer)
- {
- SetWindowText(m_strText);
- KillTimer(1);
- }
- if (bActivate)
- {
- GetWindowText(m_strText);
- m_bState = FALSE;
-
- m_bTimer = TRUE;
- SetTimer(1,500,NULL);
- m_Type = Text;
- }
- return *this;
- }
- CLabel& CLabel::FlashBackground(BOOL bActivate)
- {
- if (m_bTimer)
- KillTimer(1);
- if (bActivate)
- {
- m_bState = FALSE;
- m_bTimer = TRUE;
- SetTimer(1,500,NULL);
- m_Type = Background;
- }
- return *this;
- }
- void CLabel::OnTimer(UINT_PTR nIDEvent)
- {
- m_bState = !m_bState;
- switch (m_Type)
- {
- case Text:
- if (m_bState)
- SetWindowText(_T(""));
- else
- SetWindowText(m_strText);
- break;
- case Background:
- InvalidateRect(NULL,FALSE);
- UpdateWindow();
- break;
- }
-
- CStatic::OnTimer(nIDEvent);
- }
- CLabel& CLabel::SetLink(BOOL bLink)
- {
- m_bLink = bLink;
- if (bLink)
- ModifyStyle(0,SS_NOTIFY);
- else
- ModifyStyle(SS_NOTIFY,0);
- return *this;
- }
- void CLabel::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CString strLink;
- GetWindowText(strLink);
- ShellExecute(NULL, _T("open"),strLink,NULL,NULL,SW_SHOWNORMAL);
-
- CStatic::OnLButtonDown(nFlags, point);
- }
- BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if (m_hCursor)
- {
- ::SetCursor(m_hCursor);
- return TRUE;
- }
- return CStatic::OnSetCursor(pWnd, nHitTest, message);
- }
- CLabel& CLabel::SetLinkCursor(HCURSOR hCursor)
- {
- m_hCursor = hCursor;
- return *this;
- }
|