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

📄 gamehighbutton.cpp

📁 Visual C++网络游戏建模与实现(第2版)
💻 CPP
字号:
// JQButton.cpp : implementation file
//

#include "stdafx.h"
#include "GameHighButton.h"
#include "baseFunction.h"
#include "resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGameHighButton

CGameHighButton::CGameHighButton()
{
	_IsFocus	=FALSE;

	_IsDown		=FALSE;

	clrFocus	=RGB(0, 0, 255);

	hCursor		=NULL;

	clrBk		=RGB(123, 178, 206);
}

CGameHighButton::~CGameHighButton()
{
	if(NULL != hCursor)
	{
		::DestroyCursor(hCursor);
		hCursor	=NULL;
	}
}


BEGIN_MESSAGE_MAP(CGameHighButton, CButton)
	//{{AFX_MSG_MAP(CGameHighButton)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGameHighButton message handlers

void CGameHighButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	DrawHallStyleButton(lpDrawItemStruct);
}

void CGameHighButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(!this->IsWindowEnabled())	
		return;
	this->_IsDown	=FALSE;
	Invalidate();
	CButton::OnLButtonUp(nFlags, point);
}

void CGameHighButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(!this->IsWindowEnabled())	
		return;

	this->_IsDown	=TRUE;
	Invalidate();
	CButton::OnLButtonDown(nFlags, point);
}

void CGameHighButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(!this->IsWindowEnabled())	
		return;

	POINT	_point;
	::GetCursorPos(&_point);
	
	RECT	rect;
	this->GetWindowRect(&rect);
	if (::PtInRect( &rect, _point) &&
		::WindowFromPoint(_point) == this ->GetSafeHwnd())
	{
		this->_IsFocus	=TRUE;
		if(this->GetSafeHwnd() != ::GetCapture())
		{
			this->Invalidate();
			this->SetCapture();
		}
	}
	else
	{
		this->_IsFocus	=FALSE;
		if(this->GetSafeHwnd() == ::GetCapture())
		{
			this->Invalidate();
			ReleaseCapture();
		}
		
	}
	CButton::OnMouseMove(nFlags, point);
}

void CGameHighButton::DrawHallStyleButton(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	ASSERT(lpDrawItemStruct);
	
	if(NULL == lpDrawItemStruct)
	{
		return ;
	}

//	this->ModifyStyleEx(NULL, WS_EX_TRANSPARENT, 0);
	
	RECT	rect;
	memset(&rect, 0, sizeof(RECT));
	::CopyRect(&rect, &(lpDrawItemStruct->rcItem));

	HDC	hDC	=lpDrawItemStruct->hDC;
	if(NULL == hDC)
		return;
	CDC*	pDC	=NULL;
	pDC	=pDC->FromHandle(hDC);
	if(NULL == pDC ||
		NULL == pDC->GetSafeHdc())
	{
		return ;
	}
	
	CBrush	m_Brush;
	m_Brush.CreateSolidBrush(clrBk);
	if(NULL != m_Brush.GetSafeHandle())
	{
		pDC->FillRect(&rect, &m_Brush);
	}
	m_Brush.DeleteObject();

	CString	strTitle;
	strTitle.Empty();
	this->GetWindowText(strTitle);

	if(!strTitle.IsEmpty())
	{
		::SetBkMode(hDC, TRANSPARENT);
		if(this->IsWindowEnabled())
		{
			HFONT	hFont		=NULL;
			HFONT	hOldFont	=NULL;	
			LOGFONT	lgFont;
			memset(&lgFont, 0, sizeof(LOGFONT));
			if(IsWin9x())
			{
				lgFont.lfHeight	=15;
			}
			else
			{
				lgFont.lfHeight	=14;
			}
			
			if(_IsFocus)
			{
				lgFont.lfUnderline	=TRUE;
			}
			

			
			if(_IsFocus)
			{
				::SetTextColor(hDC, clrFocus);	
			}

			hFont=::CreateFontIndirect(&lgFont);
			hOldFont	=(HFONT)::SelectObject(hDC, hFont);

			::DrawText(hDC, strTitle, strTitle.GetLength(), &rect, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
			if(NULL != hFont && NULL != hOldFont)
			{
				::SelectObject(hDC, hOldFont);
				::DeleteObject(hFont);
			}
		}
		else
		{
			
			rect.left	+=1;
			rect.right	+=1;
			rect.top	+=1;
			rect.bottom	+=1;
			::SetTextColor(hDC, GetSysColor(COLOR_3DHILIGHT));
			::DrawText(hDC, strTitle, strTitle.GetLength(), &rect, DT_VCENTER | DT_SINGLELINE | DT_CENTER);

			rect.left	-=1;
			rect.right	-=1;
			rect.top	-=1;
			rect.bottom	-=1;
		
			::SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
			::DrawText(hDC, strTitle, strTitle.GetLength(), &rect, DT_VCENTER | DT_SINGLELINE);				
		}
	}
}

BOOL CGameHighButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if( NULL == hCursor)
		hCursor=::LoadCursor(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_MYHAND));
	if(hCursor)
		::SetCursor(hCursor);
	return 1;
}

void CGameHighButton::SetBkColor(COLORREF clr)
{
	clrBk	=clr;
}

⌨️ 快捷键说明

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