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

📄 label.cpp

📁 这是一个有关网络游戏平台编程中的一些例子
💻 CPP
字号:
// Label.cpp : implementation file
//

#include "stdafx.h"
#include "Label.h"
#include "resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLabel

CLabel::CLabel()
{
	clrBk			=::GetSysColor(COLOR_3DFACE);
	clrText			=::GetSysColor(COLOR_WINDOWTEXT);

	memset(&lgTextFont, 0, sizeof(LOGFONT));

	bFontBold		=FALSE;

	bHyperLinkMode	=FALSE;

	hCursor			=::LoadCursor(::AfxGetInstanceHandle(), 
		MAKEINTRESOURCE(IDC_MYHAND));
	hOldCursor		=NULL;
}

CLabel::~CLabel()
{
	if(hCursor)
	{
		::DestroyCursor(hCursor);
	}
}


BEGIN_MESSAGE_MAP(CLabel, CStatic)
	//{{AFX_MSG_MAP(CLabel)
	ON_WM_PAINT()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLabel message handlers

void CLabel::SetTextColor(COLORREF clr)
{	
	clrText		=clr;
}

void CLabel::SetBkClolor(COLORREF clr)
{
	clrBk		=clr;
}

void CLabel::SetLabelFont(LPLOGFONT lpfnt)
{
	ASSERT(lpfnt);
	if(NULL != lpfnt)
	{
		memcpy(&lgTextFont, lpfnt, sizeof(LOGFONT));
	}
}

void CLabel::SetFontBold(BOOL fontbold)
{
	bFontBold	=fontbold;
}

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

	HBITMAP	hBmp	=this->GetBitmap();
	if(hBmp)
	{	//Draw Bmp
		CBitmap*	pBmp =NULL;
		pBmp	=pBmp->FromHandle(hBmp);
		if(pBmp && pBmp->GetSafeHandle())
		{
			BITMAP	bmpsize;
			memset(&bmpsize, 0, sizeof(BITMAP));

			pBmp->GetBitmap(&bmpsize);

			CDC	memdc;
			memdc.CreateCompatibleDC(&dc);
			if(memdc.GetSafeHdc())
			{
				CBitmap*	pOldBmp	=memdc.SelectObject(pBmp);
				dc.BitBlt(0, 0, bmpsize.bmWidth, bmpsize.bmHeight, 
					&memdc, 0, 0, SRCCOPY);
				memdc.SelectObject(pOldBmp);
			}
			memdc.DeleteDC();
		}
		return ;
	}

	RECT	rect;
	this->GetClientRect(&rect);

	HBRUSH	hBrush	=::CreateSolidBrush(clrBk);
	::FillRect(dc.GetSafeHdc(), &rect, hBrush);
	::DeleteObject(hBrush);
	
	if(lgTextFont.lfWidth == 0 &&
		lgTextFont.lfHeight == 0)	//字体没有由使用者设置
	{	//使用默认字体
		CFont*	pFont	=this->GetFont();
		if(pFont)
		{
			pFont->GetLogFont(&lgTextFont);
		}
	}

	dc.SetTextColor(clrText);
	dc.SetBkMode(TRANSPARENT);

	if(IsFontBold())
	{
		lgTextFont.lfWeight		=FW_BOLD;
	}

	if(IsHyperLinkMode())
	{
		lgTextFont.lfUnderline	=TRUE;
		dc.SetTextColor(RGB(0, 0, 255));
	}
	else
	{
		lgTextFont.lfUnderline	=FALSE;
	}

	HFONT	hFont	=::CreateFontIndirect(&lgTextFont);
	if(NULL == hFont)
		return;
	HFONT	hOldFont=(HFONT)::SelectObject(dc.GetSafeHdc(), hFont);


	CString		strText;
	this->GetWindowText(strText);
	DWORD	dwStyle	=this->GetStyle();
	if((dwStyle & SS_LEFT) == SS_LEFT)
	{
		dc.DrawText(strText, strText.GetLength(), &rect, DT_VCENTER | DT_SINGLELINE);
	}
	else
		if((dwStyle & SS_CENTER) == SS_CENTER)
	{
		dc.DrawText(strText, strText.GetLength(), 
			&rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	}

	::SelectObject(dc.GetSafeHdc(), hOldFont);
	::DeleteObject(hFont);
}

BOOL CLabel::IsFontBold()
{
	return bFontBold;
}

void CLabel::SetHyperLinkMode(BOOL bmode)
{
	bHyperLinkMode	=bmode;
}

BOOL CLabel::IsHyperLinkMode()
{
	return bHyperLinkMode;
}

BOOL CLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{	
	return CStatic::OnSetCursor(pWnd, nHitTest, message);
}

void CLabel::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CStatic::OnLButtonDown(nFlags, point);
}

void CLabel::OnLButtonUp(UINT nFlags, CPoint point) 
{
	CStatic::OnLButtonUp(nFlags, point);
}

void CLabel::OnClicked() 
{
	if(IsHyperLinkMode())
	{
		CWnd*	pWnd	=this->GetParent();
		if(pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
		{
			pWnd->SendMessage(WM_LABELCLK, 0, (LPARAM)::GetWindowLong(this->GetSafeHwnd(), 
									GWL_ID));
		}
	}
}

void CLabel::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(!IsHyperLinkMode())
		return;

	if(MK_LBUTTON == nFlags)
	{
		OnClicked(); 
		return;
	}

	if(FALSE == IsWindowEnabled())
		return ;
	POINT	_point;
	::GetCursorPos(&_point);

	RECT	rect;
	this->GetWindowRect(&rect);
	if (::PtInRect( &rect, _point) &&
		::WindowFromPoint(_point) == this ->GetSafeHwnd())
	{	//鼠标移进了标签
		if(this->GetSafeHwnd() != ::GetCapture())
		{
			this->SetCapture();
			CWnd*	pWnd	=this->GetParent();
			if(NULL != pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
			{
				pWnd->PostMessage(WM_LABELMOVEIN, 0, 0);
			}

			if(hCursor)
			{
				hOldCursor	=::SetCursor(hCursor);
			}
		}
	}
	else
	{
		if(this->GetSafeHwnd() == ::GetCapture())
		{
			//鼠标移出了标签
			ReleaseCapture();
			CWnd*	pWnd	=this->GetParent();
			if(NULL != pWnd && ::IsWindow(pWnd->GetSafeHwnd()))
			{
				pWnd->PostMessage(WM_LABELMOVEOUT, 0, 0);
			}
			if(hOldCursor)
			{
				::SetCursor(hOldCursor);
			}
		}
	}

	CStatic::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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