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

📄 a16label.cpp

📁 自定义控件
💻 CPP
字号:
// A16A16Label.cpp : implementation file
//

#include "stdafx.h"
#include "Resource.h"
#include "A16Label.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BEGIN_MESSAGE_MAP(CA16Label, CStatic)
	//{{AFX_MSG_MAP(CA16Label)
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	ON_WM_SETCURSOR()
	ON_WM_PAINT()
	ON_WM_SYSCOLORCHANGE()
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////
CA16Label::CA16Label()
{
	m_crText = GetSysColor(COLOR_WINDOWTEXT);

// 1.1
	m_hBackBrush = NULL;


	::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);

	m_font.CreateFontIndirect(&m_lf);
	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_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::~CA16Label
//
// Description:		
//////////////////////////////////////////////////////////////////////////
CA16Label::~CA16Label()
{
	// Clean up
	m_font.DeleteObject();
	::DeleteObject(m_hwndBrush);
	::DeleteObject(m_hBackBrush);
	
}


//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::ReconstructFont
//
// Description:		Helper function to build font after it was changed

//////////////////////////////////////////////////////////////////////////
void CA16Label::ReconstructFont()
{
	m_font.DeleteObject();
	BOOL bCreated = m_font.CreateFontIndirect(&m_lf);

	ASSERT(bCreated);
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::OnPaint
//
// Description:		Handles all the drawing code for the A16Label
//////////////////////////////////////////////////////////////////////////

void CA16Label::OnPaint() 
{
	CPaintDC dc(this); // device context for painting


	CRect rc;
	GetClientRect(rc);
	CString strText;
	GetWindowText(strText);

	///////////////////////////////////////////////////////
	//
	// Set up for double buffering...
	//
	CDC* pDCMem;

	if (!m_bTransparent)
	{
		pDCMem = new CDC;
		pDCMem->CreateCompatibleDC(&dc);
		CBitmap bmp;
		bmp.CreateCompatibleBitmap(&dc,rc.Width(),rc.Height());
		pDCMem->SelectObject(&bmp);
	}
	else
	{
		pDCMem = &dc;
	}


	COLORREF crText = pDCMem->SetTextColor(m_crText);
	CFont *pOldFont = pDCMem->SelectObject(&m_font);


	// Fill in backgound if not transparent
	if (!m_bTransparent)
	{
		CBrush br;
		
		if (m_bState && m_Type == Background)
		{
			if (m_hBackBrush != NULL)
				br.Attach(m_hBackBrush);
			else
				br.Attach(m_hwndBrush);
		}
		else
		{
			if (m_hBackBrush != NULL)
				br.Attach(m_hBackBrush);
			else
				br.Attach(m_hwndBrush);
		}
				
		pDCMem->FillRect(rc,&br);
		br.Detach();
	}
	

	UINT nMode = pDCMem->SetBkMode(TRANSPARENT);

	// If the text is flashing turn the text color on
	// then to the color of the window background.

	LOGBRUSH lb;
	ZeroMemory(&lb,sizeof(lb));
	::GetObject(m_hBackBrush,sizeof(lb),&lb);


	// Something to do with flashing
	if (!m_bState && m_Type == Text)
		pDCMem->SetTextColor(lb.lbColor);


	DWORD dwFlags = 0;

	if (GetStyle() & SS_LEFT)
		dwFlags = DT_LEFT;

	if (GetStyle() & SS_RIGHT)
		dwFlags = DT_RIGHT;

	// If the text centered make an assumtion that
	// the will want to center verticly as well
	if (GetStyle() & SS_CENTERIMAGE)
	{
		dwFlags = DT_CENTER;

		// Apply 
		if (strText.Find("\r\n") == -1)
		{
			dwFlags |= DT_VCENTER;

			// And because DT_VCENTER only works with single lines
			dwFlags |= DT_SINGLELINE; 
		}
	}


	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(RGB(255,255,255));

			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);
		delete pDCMem;
	}
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::OnTimer
//
// Description:		Used in conjunction with 'FLASH' functions

//////////////////////////////////////////////////////////////////////////
void CA16Label::OnTimer(UINT nIDEvent) 
{
	m_bState = !m_bState;

	InvalidateRect(NULL,TRUE);
	UpdateWindow();
	
	CStatic::OnTimer(nIDEvent);
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::OnSetCursor
//
// Description:		Used in conjunction with 'LINK' function
//////////////////////////////////////////////////////////////////////////
BOOL CA16Label::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if (m_hCursor)
	{
		::SetCursor(m_hCursor);
		return TRUE;
	}

	return CStatic::OnSetCursor(pWnd, nHitTest, message);
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::OnLButtonDown
//
// Description:		Called when a link is click on
//////////////////////////////////////////////////////////////////////////
void CA16Label::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (m_bNotifyParent)
	{
		CString strLink;

		GetWindowText(strLink);
		ShellExecute(NULL,"open",strLink,NULL,NULL,SW_SHOWNORMAL);
	}
	else
	{
		// To use notification in parent window
		// Respond to a OnNotify in parent and disassemble the message
		//
		NMHDR nm;

		nm.hwndFrom = GetSafeHwnd();
		nm.idFrom  = GetDlgCtrlID();
		nm.code = NM_LINKCLICK;
		GetParent()->SendMessage(WM_NOTIFY,nm.idFrom,(LPARAM) &nm);
	}
		
	CStatic::OnLButtonDown(nFlags, point);
}


//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetText
//
// Description:		Short cut to set window text - caption - A16Label

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetText(const CString& strText)
{
	SetWindowText(strText);
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetTextColor
//
// Description:		Sets the text color 

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetTextColor(COLORREF crText)
{
	m_crText = crText;
	RedrawWindow();
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFontBold
//
// Description:		Sets the font ot bold 

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontBold(BOOL bBold)
{	
	m_lf.lfWeight = bBold ? FW_BOLD : FW_NORMAL;
	ReconstructFont();
	RedrawWindow();
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFontUnderline
//
// Description:		Sets font underline attribue

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontUnderline(BOOL bSet)
{	
	m_lf.lfUnderline = bSet;
	ReconstructFont();
	RedrawWindow();
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFontItalic
//
// Description:		Sets font italic attribute

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontItalic(BOOL bSet)
{
	m_lf.lfItalic = bSet;
	ReconstructFont();
	RedrawWindow();
	return *this;	
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetSunken
//
// Description:		Sets sunken effect on border

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetSunken(BOOL bSet)
{
	if (!bSet)
		ModifyStyleEx(WS_EX_STATICEDGE,0,SWP_DRAWFRAME);
	else
		ModifyStyleEx(0,WS_EX_STATICEDGE,SWP_DRAWFRAME);
		
	return *this;	
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetBorder

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetBorder(BOOL bSet)
{
	if (!bSet)
		ModifyStyle(WS_BORDER,0,SWP_DRAWFRAME);
	else
		ModifyStyle(0,WS_BORDER,SWP_DRAWFRAME);
		
	return *this;	
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFontSize
//
// Description:		Sets the font size

//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontSize(int nSize)
{
	nSize*=-1;
	m_lf.lfHeight = nSize;
	ReconstructFont();
	RedrawWindow();
	return *this;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetBkColor
//
// Description:		Sets background color
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetBkColor(COLORREF crBkgnd)
{
	if (m_hBackBrush)
		::DeleteObject(m_hBackBrush);
	
	m_hBackBrush = ::CreateSolidBrush(crBkgnd);

	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFontName
//
// Description:		Sets the fonts face name
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFontName(const CString& strFont)
{
	m_lf.lfCharSet = 134/*CHINESEBIG5_CHARSET*/;
	strcpy(m_lf.lfFaceName,strFont);
	ReconstructFont();
	RedrawWindow();
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::FlashText
//
// Description:		As the function states
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::FlashText(BOOL bActivate)
{
	if (m_bTimer)
		KillTimer(1);

	if (bActivate)
	{
		m_bState = FALSE;
		
		m_bTimer = TRUE;
		
		SetTimer(1,500,NULL);

		m_Type = Text;
	}
	else
		m_Type = None; // Fix

	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::FlashBackground
//
// Description:		As the function states
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::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;
}


//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetLink
//
// Description:		Indicates the string is a link
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::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:		CA16Label::SetLinkCursor
//
// Description:		Sets the internet browers link
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetLinkCursor(HCURSOR hCursor)
{
	m_hCursor = hCursor;
	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetTransparent
//
// Description:		Sets the A16Label window to be transpaent
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetTransparent(BOOL bSet)
{
	m_bTransparent = bSet;
	InvalidateRect(NULL,TRUE);
	UpdateWindow();

	return *this;
}

//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetFont3D
//
// Description:		Sets the 3D attribute of the font.
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::SetFont3D(BOOL bSet,Type3D type)
{
	m_bFont3d = bSet;
	m_3dType = type;
	RedrawWindow();

	return *this;
}

void CA16Label::OnSysColorChange() 
{
//	CStatic::OnSysColorChange();
	if (m_hwndBrush)
		::DeleteObject(m_hwndBrush);

	m_hwndBrush = ::CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	
	RedrawWindow();
		
}



//////////////////////////////////////////////////////////////////////////
//
// Function:		CA16Label::SetRotationAngle
//
// Description:		Sets the 3D attribute of the font.
//////////////////////////////////////////////////////////////////////////
CA16Label& CA16Label::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();
	
	RedrawWindow();

	return *this;
}

⌨️ 快捷键说明

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