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

📄 tracklookbutton.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
// CTrackLookButton.cpp : implementation file
//

#include "stdafx.h"
#include "Netmsg.h"
#include "TrackLookButton.h"

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

#define RGB_BUTTON_BLACK    (GetSysColor(COLOR_WINDOWFRAME))
#define RGB_BUTTON_WHITE    (GetSysColor(COLOR_BTNHIGHLIGHT))
#define RGB_BUTTON_LIGHT    (GetSysColor(COLOR_BTNFACE))
#define RGB_BUTTON_DARK     (GetSysColor(COLOR_BTNSHADOW))

#define BORDER_CLEAR		0x0000L
#define BORDER_PUSHED		0x0001L
#define BORDER_NONPUSHED	0x0002L

/////////////////////////////////////////////////////////////////////////////
// CTrackLookButton

CTrackLookButton::CTrackLookButton()
{
	m_bMouseCaptured = FALSE;
	m_bLButtonDown = FALSE;
	m_bHasFocus = FALSE;
	m_bDisabled = FALSE;

	m_iNormal = NULL;
	m_iFocus = NULL;
	m_iDisabled = NULL;

	m_TextAlign = AlignLeft;
	m_nBorder = BORDER_CLEAR;
	m_bRaised = FALSE;

	m_TextOffset = 0;
	m_IconX = 0;
	m_IconY = 0;
}

CTrackLookButton::~CTrackLookButton()
{

}


BEGIN_MESSAGE_MAP(CTrackLookButton, CButton)
	//{{AFX_MSG_MAP(CTrackLookButton)
	ON_WM_MOUSEMOVE()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_CREATE()
	ON_WM_ENABLE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrackLookButton message handlers

void CTrackLookButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	CButton::OnMouseMove(nFlags, point);
	if (!m_bMouseCaptured || GetCapture() != this || m_nBorder == BORDER_CLEAR)
	{
		SetCapture();
		m_bMouseCaptured = TRUE;
		OnMouseEnter(nFlags, point);
	} 
	else
	{
		CRect rc;
		this->GetClientRect(&rc);
		if (!rc.PtInRect(point))
		{
			OnMouseLeave(nFlags, point);
			m_bMouseCaptured = FALSE;
			ReleaseCapture();
		}
	}
}

void CTrackLookButton::OnKillFocus(CWnd* pNewWnd) 
{
	m_nBorder = BORDER_CLEAR;
	m_bHasFocus = FALSE;
	m_bRaised = FALSE;
	CButton::OnKillFocus(pNewWnd);
	Invalidate();
	UpdateWindow();
}

void CTrackLookButton::OnSetFocus(CWnd* pOldWnd) 
{
	m_nBorder = m_bLButtonDown ? BORDER_PUSHED : BORDER_NONPUSHED;
	m_bHasFocus = TRUE;
	m_bRaised = TRUE;
	CButton::OnSetFocus(pOldWnd);
	Invalidate();
	UpdateWindow();
}

void CTrackLookButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	int	 iSaveDC;
	CDC* pDC;
	CBrush brush(RGB_BUTTON_LIGHT);
	CRect rc;
	CString	strTitle;
	UINT nFormat;
	HICON hIcon = NULL;
	
	pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	VERIFY(pDC);
	rc.CopyRect(&lpDrawItemStruct->rcItem);
	GetWindowText(strTitle);
	nFormat = DT_SINGLELINE;
	iSaveDC = pDC->SaveDC();
	
	switch (m_TextAlign)
	{
		case AlignAbove: nFormat |= DT_CENTER | DT_TOP;
			break;	
		case AlignBelow: nFormat |= DT_CENTER | DT_BOTTOM;
			break;
		case AlignLeft:
		case AlignRight: nFormat |= DT_LEFT | DT_VCENTER;
			break;
		default: ASSERT(FALSE); // should not be called;
	}
	
	
	pDC->SetBkMode(TRANSPARENT);
	Draw3DBorder(pDC, rc, m_nBorder);
	rc.InflateRect(-5, -2);
	
	if (m_bHasFocus)
	{
		hIcon = m_iFocus;
	}
	else
	{
		hIcon = m_iNormal;
	}
	
	if (m_bRaised)
	{
		hIcon = m_iFocus;
	} 
	
	if (m_bDisabled=(::GetWindowLong(m_hWnd, GWL_STYLE) & WS_DISABLED))
	{
		hIcon = m_iDisabled;
	}
	else if (m_bLButtonDown) rc.OffsetRect(1,1);
	
	CRect rcText(rc);
	
	if (m_iNormal)
	rcText.left = m_IconX + 9;

	if (!hIcon) 
		hIcon = m_iNormal;

	::DrawIconEx(pDC->GetSafeHdc(), 5, 5, hIcon, m_IconX, m_IconY, 0, 0, DI_NORMAL);
		
	if (m_bDisabled)
	{
		rcText.OffsetRect(1, 1);
		pDC->SetTextColor(RGB_BUTTON_WHITE);
		pDC->DrawText(strTitle, rcText, nFormat);
		rcText.OffsetRect(-1, -1);
		pDC->SetTextColor(RGB_BUTTON_DARK);
		pDC->DrawText(strTitle, rcText, nFormat);
	} 
	else
	{
		pDC->SetTextColor(GetColor(COL_NORMALTEXT));
		pDC->DrawText(strTitle, rcText, nFormat);
	}
	
	pDC->RestoreDC(iSaveDC);
}

void CTrackLookButton::OnMouseEnter(UINT nFlags, CPoint point)
{
	m_bLButtonDown = (nFlags & MK_LBUTTON);
	m_nBorder = m_bLButtonDown?BORDER_PUSHED:BORDER_NONPUSHED;
	m_bRaised = TRUE;
	Invalidate();
	UpdateWindow();
}

void CTrackLookButton::OnMouseLeave(UINT nFlags,CPoint point)
{
	m_nBorder = BORDER_CLEAR;
	m_bLButtonDown = FALSE;
	m_bRaised = FALSE;
	Invalidate();
	UpdateWindow();
}

void CTrackLookButton::Draw3DBorder(CDC* pDC,CRect rc,UINT nOptions)
{
	switch (nOptions)
	{
	case BORDER_CLEAR:
					//	pDC->Draw3dRect(rc,RGB_BUTTON_LIGHT,RGB_BUTTON_LIGHT);
						break;
	case BORDER_PUSHED:
						pDC->Draw3dRect(rc,RGB_BUTTON_DARK,RGB_BUTTON_WHITE);
						break;
	case BORDER_NONPUSHED:
						pDC->Draw3dRect(rc,RGB_BUTTON_WHITE,RGB_BUTTON_DARK);
						break;
	default:			break;
	}
}

void CTrackLookButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_bLButtonDown = TRUE;
	m_bRaised = FALSE;
	if (GetFocus()!=this)
	{
		this->SetFocus();
		return;
	}
	m_nBorder = BORDER_PUSHED;

	CButton::OnLButtonDown(nFlags,point);
	Invalidate();
	UpdateWindow();
}


void CTrackLookButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bLButtonDown = FALSE;
	m_bRaised = TRUE;
	if (GetFocus() != this)
	{
		
		this->SetFocus();
	
	} else
	{
		m_nBorder = BORDER_NONPUSHED;
		Invalidate();
		UpdateWindow();
	}
	
	GetParent()->SendMessage(WM_COMMAND,
				MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED),
				(LPARAM) m_hWnd);
}

void CTrackLookButton::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	m_bLButtonDown = TRUE;
	if (GetFocus() != this)
	{
		this->SetFocus();
		return;
	}
	m_nBorder = BORDER_PUSHED;

	Invalidate();
	UpdateWindow();
	CButton::OnLButtonDblClk(nFlags, point);	
	m_nBorder = BORDER_CLEAR;
}

int CTrackLookButton::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CButton::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_bDisabled = (lpCreateStruct->style & WS_DISABLED);
	return 0;
}

void CTrackLookButton::OnEnable(BOOL bEnable) 
{
	m_bDisabled =! bEnable;
	CButton::OnEnable(bEnable);
}

CTrackLookButton::TextAlign CTrackLookButton::GetTextAlignment() const
{
	return m_TextAlign;
}

void CTrackLookButton::SetTextAlignment(TextAlign nTextAlign)
{
	m_TextAlign = nTextAlign;
}

void CTrackLookButton::SetIcons(HICON iNormal, HICON iFocus, HICON iDisabled)
{
	m_iNormal = iNormal;
	m_iFocus = iFocus;
	m_iDisabled = iDisabled;
	Invalidate();
}

void CTrackLookButton::SetIconSize(int x, int y)
{
	m_IconX = x;
	m_IconY = y;
	Invalidate();
}

⌨️ 快捷键说明

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