⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atllabel.h

📁 一个提示软件
💻 H
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
//
// Original CLabel Class written by Norm Almond
//
/////////////////////////////////////////////////////////////////////////////
//
// 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.5a
//
// A. Port to ATL / WTL (Rashid Thadha) 10/12/2000
// 
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(ATL_LABEL_H)
#define ATL_LABEL_H

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// ATLLabel.h : header file
//

#define	NM_LINKCLICK	WM_APP + 0x200
#if (WINVER < 0x0500)
__declspec(selectany) struct
{
	enum { cxWidth = 32, cyHeight = 32 };
	int xHotSpot;
	int yHotSpot;
	unsigned char arrANDPlane[cxWidth * cyHeight / 8];
	unsigned char arrXORPlane[cxWidth * cyHeight / 8];
} _AtlLabel_CursorData = 
{
	5, 0, 
	{
		0xF9, 0xFF, 0xFF, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0xFF, 0xFF, 0xFF, 
		0xF0, 0xFF, 0xFF, 0xFF, 0xF0, 0x3F, 0xFF, 0xFF, 0xF0, 0x07, 0xFF, 0xFF, 0xF0, 0x01, 0xFF, 0xFF, 
		0xF0, 0x00, 0xFF, 0xFF, 0x10, 0x00, 0x7F, 0xFF, 0x00, 0x00, 0x7F, 0xFF, 0x00, 0x00, 0x7F, 0xFF, 
		0x80, 0x00, 0x7F, 0xFF, 0xC0, 0x00, 0x7F, 0xFF, 0xC0, 0x00, 0x7F, 0xFF, 0xE0, 0x00, 0x7F, 0xFF, 
		0xE0, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0xFF, 0xFF, 0xF0, 0x00, 0xFF, 0xFF, 0xF8, 0x01, 0xFF, 0xFF, 
		0xF8, 0x01, 0xFF, 0xFF, 0xF8, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	},
	{
		0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 
		0x06, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0xC0, 0x00, 0x00, 0x06, 0xD8, 0x00, 0x00, 
		0x06, 0xDA, 0x00, 0x00, 0x06, 0xDB, 0x00, 0x00, 0x67, 0xFB, 0x00, 0x00, 0x77, 0xFF, 0x00, 0x00, 
		0x37, 0xFF, 0x00, 0x00, 0x17, 0xFF, 0x00, 0x00, 0x1F, 0xFF, 0x00, 0x00, 0x0F, 0xFF, 0x00, 0x00, 
		0x0F, 0xFE, 0x00, 0x00, 0x07, 0xFE, 0x00, 0x00, 0x07, 0xFE, 0x00, 0x00, 0x03, 0xFC, 0x00, 0x00, 
		0x03, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	}
};
#endif //(WINVER < 0x0500)

// =============================================================================================
// CLabel window

class CLabel :	public CWindowImpl<CLabel, CStatic>
			                   
{
// Construction
public:
	static enum FlashType {None, Text, Background };
	static enum Type3D { Raised, Sunken};
	static enum BackFillMode { Normal, Gradient };

	BEGIN_MSG_MAP(CLabel)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_TIMER, OnTimer)
		MESSAGE_HANDLER(WM_SYSCOLORCHANGE, OnSysColorChange)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
		MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
	END_MSG_MAP()

	// =============================================================================================
	// Function name	: CLabel::CLabel
	// Description	    : Default contructor
	// Return type		: 
	// =============================================================================================
	CLabel::CLabel() :	m_bPaintLabel(true), m_hBackBrush(NULL), m_crHiColor(0), m_crLoColor(0),
						m_bTimer(FALSE), m_bState(FALSE), m_bTransparent(FALSE), m_bLink(TRUE),
						m_hCursor(NULL), m_Type(None), m_bFont3d(FALSE), m_bNotifyParent(FALSE),
						m_bToolTips(FALSE), m_bRotation(FALSE), m_fillmode(Normal), m_hFont(NULL)
	{
		m_crText = GetSysColor(COLOR_WINDOWTEXT);
		m_cr3DHiliteColor =	RGB(255,255,255);		
		m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	}

	// =============================================================================================
	// Function name	: ~CLabel
	// Description	    : Destructor
	// Return type		: virtual 
	// =============================================================================================
	virtual ~CLabel()
	{
		if(m_hFont != NULL)
			::DeleteObject(m_hFont);
		if (m_hwndBrush)
			::DeleteObject(m_hwndBrush);

		// Stop Checking complaining
		if (m_hBackBrush)
			::DeleteObject(m_hBackBrush);
		
#if (WINVER < 0x0500)
		// It was created, not loaded, so we have to destroy it
		if(m_hCursor != NULL)
			::DestroyCursor(m_hCursor);
#endif //(WINVER < 0x0500)
	}

	// =============================================================================================
	// Function name	: SetBkColor
	// Description	    : Sets background color
	// Return type		: Reference to 'this' object
	// Argument         : COLORREF crBkgnd
	// Argument         : COLORREF crBkgndHigh = 0
	// Argument         : BackFillMode mode = Normal
	// =============================================================================================
	virtual CLabel& SetBkColor(COLORREF crBkgnd, COLORREF crBkgndHigh = 0, BackFillMode mode = Normal)
	{
		m_crLoColor = crBkgnd;
		m_crHiColor = crBkgndHigh;

		m_fillmode = mode;

		if (m_hBackBrush)
			::DeleteObject(m_hBackBrush);
		
		if (m_fillmode == Normal)
			m_hBackBrush = ::CreateSolidBrush(crBkgnd);

		UpdateSurface();

		return *this;
	}

	// =============================================================================================
	// Function name	: SetTextColor
	// Description	    : Sets the text color
	// Return type		: Reference to 'this' object
	// Argument         : COLORREF crText
	// =============================================================================================
	virtual CLabel& SetTextColor(COLORREF crText)
	{
		m_crText = crText;		
		UpdateSurface();
		
    return *this;
	}

	// =============================================================================================
	// Function name	: SetText
	// Description	    : Short cut to set window text - caption - label
	// Return type		: Reference to 'this' object
	// Argument         : const CString& strText
	// =============================================================================================
	virtual CLabel& SetText(const CString& strText)
	{
		SetWindowText(strText);
		UpdateSurface();

		return *this;
	}

	// =============================================================================================
	// Function name	: SetFontBold
	// Description	    : Sets the font ot bold 
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bBold
	// =============================================================================================
	virtual CLabel& SetFontBold(BOOL bBold)
	{	

		m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
		ReconstructFont();
		UpdateSurface();
		return *this;
	}
	
	// =============================================================================================
	// Function name	: SetFontName
	// Description	    : Sets the fonts face name
	// Return type		: Reference to 'this' object
	// Argument         : const CString& strFont
	// Argument         : BYTE byCharSet = ANSI_CHARSET
	// =============================================================================================
	virtual CLabel& SetFontName(const CString& strFont, BYTE byCharSet = ANSI_CHARSET)
	{	
		m_lf.lfCharSet = byCharSet;

		_tcscpy(m_lf.lfFaceName,strFont);
		ReconstructFont();
		UpdateSurface();

		return *this;
	}
	
	// =============================================================================================
	// Function name	: SetFontUnderline
	// Description	    : Sets font underline attribue
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// =============================================================================================
	virtual CLabel& SetFontUnderline(BOOL bSet)
	{	
		m_lf.lfUnderline = bSet;
		ReconstructFont();
		UpdateSurface();

		return *this;
	}

	// =============================================================================================
	// Function name	: SetFontItalic
	// Description	    : Sets font italic attribute 
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// =============================================================================================
	virtual CLabel& SetFontItalic(BOOL bSet)
	{
		m_lf.lfItalic = bSet;
		ReconstructFont();
		UpdateSurface();

		return *this;	
	}

	// =============================================================================================
	// Function name	: SetFontSize
	// Description	    : Sets the font size 
	// Return type		: Reference to 'this' object
	// Argument         : int nSize
	// =============================================================================================
	virtual CLabel& SetFontSize(int nSize)
	{
		CFont cf;
		LOGFONT lf;

		cf.CreatePointFont(nSize * 10, m_lf.lfFaceName);
		cf.GetLogFont(&lf);

		m_lf.lfHeight = lf.lfHeight;
		m_lf.lfWidth  = lf.lfWidth;

		ReconstructFont();
		UpdateSurface();

		return *this;
	}

	// =============================================================================================
	// Function name	: SetSunken
	// Description	    : Sets sunken effect on border
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// =============================================================================================
	virtual CLabel& SetSunken(BOOL bSet)
	{

		if (!bSet)
			ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
		else
			ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
			
		return *this;	
	}

	// =============================================================================================
	// Function name	: SetBorder
	// Description	    : Toggles the border on/off
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// =============================================================================================
	virtual CLabel& SetBorder(BOOL bSet)
	{

		if (!bSet)
			ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
		else
			ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
			
		return *this;	
	}

	// =============================================================================================
	// Function name	: SetTransparent
	// Description	    : Sets the Label window to be transpaent
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// =============================================================================================
	virtual CLabel& SetTransparent(BOOL bSet)
	{

		m_bTransparent = bSet;
		ModifyStyleEx(0,WS_EX_TRANSPARENT); // Fix for transparency
		UpdateSurface();

		return *this;
	}

	// =============================================================================================
	// Function name	: FlashText
	// Description	    : As the function states
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bActivate
	// =============================================================================================
	virtual CLabel& FlashText(BOOL bActivate)
	{
		if (m_bTimer)
			KillTimer(1);

		if (bActivate)
		{
			m_bState = FALSE;			
			m_bTimer = TRUE;			
			SetTimer(1,500);
			m_Type = Text;
		}
		else
			m_Type = None; // Fix

		return *this;
	}

	// =============================================================================================
	// Function name	: FlashBackground
	// Description	    : As the function states
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bActivate
	// =============================================================================================
	virtual CLabel& FlashBackground(BOOL bActivate)
	{

		if (m_bTimer)
			KillTimer(1);

		if (bActivate)
		{
			m_bState = FALSE;

			m_bTimer = TRUE;
			SetTimer(1,500);

			m_Type = Background;
		}

		return *this;
	}

	// =============================================================================================
	// Function name	: SetLink
	// Description	    : Indicates the string is a link
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bLink
	// Argument         : BOOL bNotifyParent
	// =============================================================================================
	virtual CLabel& SetLink(BOOL bLink,BOOL bNotifyParent)
	{

		m_bLink = bLink;
		m_bNotifyParent = bNotifyParent;

		if (bLink)
			ModifyStyle(0,SS_NOTIFY);
		else
			ModifyStyle(SS_NOTIFY,0);

		return *this;
	}

	// =============================================================================================
	// Function name	: SetLinkCursor
	// Description	    : Sets the internet browers link
	// Return type		: Reference to 'this' object
	// =============================================================================================
	virtual CLabel& SetLinkCursor()
	{
    // create or load a cursor
#if (WINVER >= 0x0500)
		m_hCursor = ::LoadCursor(NULL, IDC_HAND);
#else
		m_hCursor = ::CreateCursor(_Module.GetModuleInstance(), _AtlLabel_CursorData.xHotSpot, _AtlLabel_CursorData.yHotSpot, _AtlLabel_CursorData.cxWidth, _AtlLabel_CursorData.cyHeight, _AtlLabel_CursorData.arrANDPlane, _AtlLabel_CursorData.arrXORPlane);
#endif //!(WINVER >= 0x0500)
		ATLASSERT(m_hCursor != NULL);
		return *this;
	}

	// =============================================================================================
	// Function name	: SetFont3D
	// Description	    : Sets the 3D attribute of the font.
	// Return type		: Reference to 'this' object
	// Argument         : BOOL bSet
	// Argument         : Type3D type=Raised
	// =============================================================================================
	virtual CLabel& SetFont3D(BOOL bSet,Type3D type=Raised)
	{

		m_bFont3d = bSet;
		m_3dType = type;
		UpdateSurface();


		return *this;
	}

	// =============================================================================================
	// Function name	: SetRotationAngle
	// Description	    : Sets the rotation angle for the current font.
	// Return type		: Reference to 'this' object
	// Argument         : UINT nAngle
	// Argument         : BOOL bRotation
	// =============================================================================================
	virtual CLabel& 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...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -