📄 label.cpp
字号:
//
// INPUTS: Handle of cursor
//
// RETURNS: Reference to 'this' object
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 26/08/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetLinkCursor(HCURSOR hCursor)
{
m_hCursor = hCursor;
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetTransparent
//
// Description: Sets the Label window to be transpaent
//
// INPUTS: True or false
//
// RETURNS: Reference to 'this' object
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 22/10/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetTransparent(BOOL bSet)
{
m_bTransparent = bSet;
ModifyStyleEx(0,WS_EX_TRANSPARENT); // Fix for transparency
UpdateSurface();
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetFont3D
//
// Description: Sets the 3D attribute of the font.
//
// INPUTS: True or false, Raised or Sunken
//
// RETURNS: Reference to 'this' object
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 22/10/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetFont3D(BOOL bSet,Type3D type)
{
m_bFont3d = bSet;
m_3dType = type;
UpdateSurface();
return *this;
}
void CXLabel::OnSysColorChange()
{
if (m_hwndBrush)
::DeleteObject(m_hwndBrush);
m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
UpdateSurface();
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetRotationAngle
//
// Description: Sets the rotation angle for the current font.
//
// INPUTS: Angle in Degress
//
// RETURNS: Reference to 'this' object
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 22/10/98 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetRotationAngle(UINT nAngle,BOOL bRotation)
{
// Arrrrh...
// Your looking in here why the font is rotating, aren't you?
// Well try setting the font name to 'Arial' or 'Times New Roman'
// Make the Angle 180 and set bRotation to true.
//
// Font rotation _ONLY_ works with TrueType fonts...
//
//
m_lf.lfEscapement = m_lf.lfOrientation = (nAngle * 10);
m_bRotation = bRotation;
ReconstructFont();
UpdateSurface();
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetText3DHiliteColor
//
// Description: Sets the 3D font hilite color
//
// INPUTS: Color
//
// RETURNS: Reference to 'this' object
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 17/07/00 1.0 Origin
//
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetText3DHiliteColor(COLORREF cr3DHiliteColor)
{
m_cr3DHiliteColor = cr3DHiliteColor;
UpdateSurface();
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::PreSubclassWindow
//
// Description: Assigns default dialog font
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 15092000 1.5 Origin
// NT ALMOND 02072002 1.6 Fix crash when GetFont returns NULL
//////////////////////////////////////////////////////////////////////////
void CXLabel::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
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: CXLabel::PreCreateWindow
//
// Description:
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 15092000 1.5 Origin
//////////////////////////////////////////////////////////////////////////
BOOL CXLabel::PreCreateWindow(CREATESTRUCT& cs)
{
return CStatic::PreCreateWindow(cs);
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::DrawGradientFill
//
// Description: Internal help function to gradient fill background
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 15092000 1.5 Origin
//////////////////////////////////////////////////////////////////////////
void CXLabel::DrawGradientFill(CDC* pDC, CRect* pRect, COLORREF crStart, COLORREF crEnd, int nSegments)
{
// Get the starting RGB values and calculate the incremental
// changes to be applied.
COLORREF cr;
int nR = GetRValue(crStart);
int nG = GetGValue(crStart);
int nB = GetBValue(crStart);
int neB = GetBValue(crEnd);
int neG = GetGValue(crEnd);
int neR = GetRValue(crEnd);
if(nSegments > pRect->Width())
nSegments = pRect->Width();
int nDiffR = (neR - nR);
int nDiffG = (neG - nG);
int nDiffB = (neB - nB);
int ndR = 256 * (nDiffR) / (max(nSegments,1));
int ndG = 256 * (nDiffG) / (max(nSegments,1));
int ndB = 256 * (nDiffB) / (max(nSegments,1));
nR *= 256;
nG *= 256;
nB *= 256;
neR *= 256;
neG *= 256;
neB *= 256;
int nCX = pRect->Width() / max(nSegments,1), nLeft = pRect->left, nRight;
pDC->SelectStockObject(NULL_PEN);
for (int i = 0; i < nSegments; i++, nR += ndR, nG += ndG, nB += ndB)
{
// Use special code for the last segment to avoid any problems
// with integer division.
if (i == (nSegments - 1))
nRight = pRect->right;
else
nRight = nLeft + nCX;
cr = RGB(nR / 256, nG / 256, nB / 256);
{
CBrush br(cr);
CBrush* pbrOld = pDC->SelectObject(&br);
pDC->Rectangle(nLeft, pRect->top, nRight + 1, pRect->bottom);
pDC->SelectObject(pbrOld);
}
// Reset the left side of the drawing rectangle.
nLeft = nRight;
}
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetFont
//
// Description: Sets font with LOGFONT structure
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 02072002 1.6 Origin
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetFont(LOGFONT lf)
{
CopyMemory(&m_lf, &lf, sizeof(m_lf));
ReconstructFont();
UpdateSurface();
return *this;
}
BOOL CXLabel::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetMailLink
//
// Description: Sets the label so it becomes Mail enabled
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 02072002 1.6 Origin
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetMailLink(BOOL bEnable, BOOL bNotifyParent)
{
if (bEnable)
m_Link = MailLink;
else
m_Link = LinkNone;
m_bNotifyParent = bNotifyParent;
if (m_Link != LinkNone)
ModifyStyle(0,SS_NOTIFY);
else
ModifyStyle(SS_NOTIFY,0);
return *this;
}
//////////////////////////////////////////////////////////////////////////
//
// Function: CXLabel::SetHyperLink
//
// Description: Sets the label so it becomes hyperlink enabled
//
// INPUTS:
//
// RETURNS:
//
// NOTES:
//
// MODIFICATIONS:
//
// Name Date Version Comments
// NT ALMOND 02072002 1.6 Origin
//////////////////////////////////////////////////////////////////////////
CXLabel& CXLabel::SetHyperLink(const CString& sLink)
{
m_sLink = sLink;
return *this;
}
void CXLabel::SetHandCursor()
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule)
{
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
void CXLabel::PositionWindow()
{
if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit)
return;
// Get the current window position
CRect WndRect, ClientRect;
GetWindowRect(WndRect);
GetClientRect(ClientRect);
ClientToScreen(ClientRect);
CWnd* pParent = GetParent();
if (pParent)
{
pParent->ScreenToClient(WndRect);
pParent->ScreenToClient(ClientRect);
}
// Get the size of the window text
CString strWndText;
GetWindowText(strWndText);
CDC* pDC = GetDC();
CFont* pOldFont = pDC->SelectObject(&m_font);
CSize Extent = pDC->GetTextExtent(strWndText);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
// Adjust for window borders
Extent.cx += WndRect.Width() - ClientRect.Width();
Extent.cy += WndRect.Height() - ClientRect.Height();
// Get the text justification via the window style
DWORD dwStyle = GetStyle();
// Recalc the window size and position based on the text justification
if (dwStyle & SS_CENTERIMAGE)
WndRect.DeflateRect(0, (WndRect.Height() - Extent.cy)/2);
else
WndRect.bottom = WndRect.top + Extent.cy;
if (dwStyle & SS_CENTER)
WndRect.DeflateRect((WndRect.Width() - Extent.cx)/2, 0);
else if (dwStyle & SS_RIGHT)
WndRect.left = WndRect.right - Extent.cx;
else // SS_LEFT = 0, so we can't test for it explicitly
WndRect.right = WndRect.left + Extent.cx;
// Move the window
SetWindowPos(NULL, WndRect.left, WndRect.top, WndRect.Width(), WndRect.Height(), SWP_NOZORDER);
}
void CXLabel::SetAutoSize(BOOL bAutoSize)
{
m_bAdjustToFit = bAutoSize;
if (::IsWindow(GetSafeHwnd()))
PositionWindow();
}
void CXLabel::SetCommand(DWORD comm, COLORREF color)
{
m_dwCommand = comm;
SetTransparent(TRUE);
SetFontUnderline(TRUE);
SetAutoSize(TRUE);
SetLink(TRUE,TRUE);
SetHandCursor();
SetTextColor(color);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -