📄 label.cpp
字号:
// Label.cpp : implementation file
//
#include "stdafx.h"
#include "Label.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CLabel, CStatic)
//{{AFX_MSG_MAP(CLabel)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_SYSCOLORCHANGE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLabel Version 1.2
//
// From now on I'll try to keep a log of fixes and enhancements...
//
// The new feature were added due to the response of people.
// All I ask is to all you programmers out there, is if you add, fix or
// enhance this code, sent me a copy and I'll send the copy on to www.codeproject.com
//
// Happy Software Engineer :)
//
// New features include:
//
// A. Support for 3D Fonts
// B. Support for background transparency
// C. More comments provided
// D. If alignment is 'centered' and the window text is seperated by '\r\n'
// the will be centered accordingly - requested by someone @ nasa ;)
// E. Support for font rotation.
// F. Respond to System Color Change
// G. OnPaint improved performance - using Double Buffering Technique
//
// Thanks to:
// Mark McDowell - For suggestion on 'Increasing the flexibility of "hypertext" setting...'
// Erich Ruth - For suggestion on 'Font Rotation'
//
/////////////////////////////////////////////////////////////////////////////
// CLabel Version 1.3
//
// A. Added SS_LEFTNOWORDWRAP to include wordwrap
// B. Fix repainting problem
// C. Fix SetBkColor
// D. Added SS_CENTER
// Thanks to:
// Marius - Added styling problem.
// Azing Vondeling & Broker - Spotting painting Problem.
// Mel Stober - Back Color & SS_CENTER
//
/////////////////////////////////////////////////////////////////////////////
// CLabel Version 1.4
//
// A. Fix to transparency mode
// B. Added new SetText3DHiliteColor to change the 3D Font face color - default is white.
//
// Thanks to:
// michael.groeger - Spotting Transparency with other controls bug.
//
//
/////////////////////////////////////////////////////////////////////////////
// CLabel Version 1.5
//
// A. Sanity handle check
// B. Support Interface Charset
// C. Check compilition with _UNICODE
// D. Fix hyperlink feature
// E. Support default Dialog Font
// F. Inclusion of SS_OWNERDRAW via control creation and subclassing
// G. Modification to Text aligmnent code
// H. New background gradient fill function
//
// Thanks to:
// Steve Kowald - Using null handles
// Alan Chan - Supporting International Windows
// Dieter Fauth - Request for default Dialog font
// Herb Illfelder - Text Alignment code
//
/////////////////////////////////////////////////////////////////////////////
// CLabel Version 1.6
// Jeroen Roosendaal - SetFont suggestion
// Laurent - Spotting SelectObject bugs
// Bernie - Fix PreCreateWindow bug
// Jignesh I. Patel - Added expanded tabs feature
// Jim Farmelant - Fix SetText crash
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::CLabel
//
// Description: Default contructor
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 26/08/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CLabel::CLabel()
{
m_crText = GetSysColor(COLOR_WINDOWTEXT);
// 1.1
m_hBackBrush = NULL;
m_crHiColor = 0;
m_crLoColor = 0;
m_bTimer = FALSE;
m_bState = FALSE;
m_bTransparent = FALSE;
m_Link = LinkNone;
m_hCursor = NULL;
m_Type = None;
m_bFont3d = FALSE;
m_bNotifyParent = FALSE;
m_bToolTips = FALSE;
m_bRotation = FALSE;
m_fillmode = Normal;
m_cr3DHiliteColor = RGB(255,255,255);
m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::~CLabel
//
// Description:
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 26/08/98 1.0 Origin
// NT ALMOND 15092000 1.5 Handle Check
//////////////////////////////////////////////////////////////////////////
CLabel::~CLabel()
{
// Clean up
m_font.DeleteObject();
::DeleteObject(m_hwndBrush);
// Stop Checking complaining
if (m_hBackBrush)
::DeleteObject(m_hBackBrush);
}
void CLabel::UpdateSurface()
{
CRect (rc);
GetWindowRect(rc);
RedrawWindow();
GetParent()->ScreenToClient(rc);
GetParent()->InvalidateRect(rc,TRUE);
GetParent()->UpdateWindow();
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::ReconstructFont
//
// Description: Helper function to build font after it was changed
//
// INPUTS:
//
// RETURNS:
//
// NOTES: PROTECTED
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 26/08/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
void CLabel::ReconstructFont()
{
m_font.DeleteObject();
BOOL bCreated = m_font.CreateFontIndirect(&m_lf);
ASSERT(bCreated);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::OnPaint
//
// Description: Handles all the drawing code for the label
//
// INPUTS:
//
// RETURNS:
//
// NOTES: Called by Windows... not by USER
// Probably needs tiding up a some point.
// Different states will require this code to be reworked.
//
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 22/10/98 1.0 Origin
// NT ALMOND 15092000 1.5 Handle Check
// NT ALMOND 15092000 1.5 Alignment mods
// NT ALMOND 15092000 1.5 Gradient Fill Mode
// NT ALMOND 02072002 1.6 Fill SelectObject bugs
// NT ALMOND 02072002 1.6 Added to expand tabs
//////////////////////////////////////////////////////////////////////////
void CLabel::OnPaint()
{
CPaintDC dc(this); // device context for painting
DWORD dwFlags = 0;
CRect rc;
GetClientRect(rc);
CString strText;
GetWindowText(strText);
CBitmap bmp;
///////////////////////////////////////////////////////
//
// Set up for double buffering...
//
CDC* pDCMem;
CBitmap* pOldBitmap = NULL;
if (!m_bTransparent)
{
pDCMem = new CDC;
pDCMem->CreateCompatibleDC(&dc);
bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
pOldBitmap = pDCMem->SelectObject(&bmp);
}
else
{
pDCMem = &dc;
}
UINT nMode = pDCMem->SetBkMode(TRANSPARENT);
COLORREF crText = pDCMem->SetTextColor(m_crText);
CFont *pOldFont = pDCMem->SelectObject(&m_font);
// Fill in backgound if not transparent
if (!m_bTransparent)
{
if (m_fillmode == Normal)
{
CBrush br;
if (m_hBackBrush != NULL)
br.Attach(m_hBackBrush);
else
br.Attach(m_hwndBrush);
pDCMem->FillRect(rc,&br);
br.Detach();
}
else // Gradient Fill
{
DrawGradientFill(pDCMem, &rc, m_crLoColor, m_crHiColor, 100);
}
}
// If the text is flashing turn the text color on
// then to the color of the window background.
LOGBRUSH lb;
ZeroMemory(&lb,sizeof(lb));
// Stop Checking complaining
if (m_hBackBrush)
::GetObject(m_hBackBrush,sizeof(lb),&lb);
// Something to do with flashing
if (!m_bState && m_Type == Text)
pDCMem->SetTextColor(lb.lbColor);
DWORD style = GetStyle();
switch (style & SS_TYPEMASK)
{
case SS_RIGHT:
dwFlags = DT_RIGHT | DT_WORDBREAK;
break;
case SS_CENTER:
dwFlags = SS_CENTER | DT_WORDBREAK;
break;
case SS_LEFTNOWORDWRAP:
dwFlags = DT_LEFT;
break;
default: // treat other types as left
case SS_LEFT:
dwFlags = DT_LEFT | DT_WORDBREAK;
break;
}
// Added to expand tabs...
if(strText.Find(_T('\t')) != -1)
dwFlags |= DT_EXPANDTABS;
// If the text centered make an assumtion that
// the will want to center verticly as well
if (style & SS_CENTERIMAGE)
{
dwFlags = DT_CENTER;
// Apply
if (strText.Find(_T("\r\n")) == -1)
{
dwFlags |= DT_VCENTER;
// And because DT_VCENTER only works with single lines
dwFlags |= DT_SINGLELINE;
}
}
//
// 3333 DDDDD
// 3 D D
// 33 D D E F X
// 3 D D
// 3333 DDDDD
//
//
if (m_bRotation)
{
int nAlign = pDCMem->SetTextAlign (TA_BASELINE);
CPoint pt;
GetViewportOrgEx (pDCMem->m_hDC,&pt) ;
SetViewportOrgEx (pDCMem->m_hDC,rc.Width() / 2, rc.Height() / 2, NULL) ;
pDCMem->TextOut (0, 0, strText) ;
SetViewportOrgEx (pDCMem->m_hDC,pt.x / 2, pt.y / 2, NULL) ;
pDCMem->SetTextAlign (nAlign);
}
else
{
pDCMem->DrawText(strText,rc,dwFlags);
if (m_bFont3d)
{
pDCMem->SetTextColor(m_cr3DHiliteColor);
if (m_3dType == Raised)
rc.OffsetRect(-1,-1);
else
rc.OffsetRect(1,1);
pDCMem->DrawText(strText,rc,dwFlags);
m_3dType;
}
}
// Restore DC's State
pDCMem->SetBkMode(nMode);
pDCMem->SelectObject(pOldFont);
pDCMem->SetTextColor(crText);
if (!m_bTransparent)
{
dc.BitBlt(0,0,rc.Width(),rc.Height(),pDCMem,0,0,SRCCOPY);
// continue DC restore
pDCMem->SelectObject ( pOldBitmap ) ;
delete pDCMem;
}
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::OnTimer
//
// Description: Used in conjunction with 'FLASH' functions
//
// INPUTS: Windows API
//
// RETURNS: Windows API
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 26/08/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
void CLabel::OnTimer(UINT nIDEvent)
{
m_bState = !m_bState;
UpdateSurface();
CStatic::OnTimer(nIDEvent);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CLabel::OnSetCursor
//
// Description: Used in conjunction with 'LINK' function
//
// INPUTS: Windows API
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -