cartonbutn.cpp

来自「一个长度信息管理系统。主要包含了界面编程和对文件操作。」· C++ 代码 · 共 344 行

CPP
344
字号
// Cartonbutn.cpp : implementation file
//

#include "stdafx.h"
//#include "cartoonbutton.h"
#include "Cartonbutn.h"
#include "Cartonbutngroup.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CCartonbutn, CButton)
//{{AFX_MSG_MAP(CCartonbutn)
ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCartonbutn message handlers
// CCartonbutn

CCartonbutn::CCartonbutn()
{
	m_ButtonState		= BUTTON_OFF;
	m_bMouseTracking	= FALSE;
	m_pOwnerGroup = NULL;
	m_ButtonType = 1;
}

CCartonbutn::~CCartonbutn()
{
}

void CCartonbutn::OnMouseMove(UINT nFlags, CPoint point) 
{   
	
		RelayEvent(WM_MOUSEMOVE,(WPARAM)nFlags,MAKELPARAM(LOWORD(point.x),LOWORD(point.y)));
	CButton::OnMouseMove(nFlags, point);
// TODO: Add your message handler code here and/or call default
// 1. Mouse has moved and we are not tracking this button, or 
// 2. mouse has moved and the cursor was not above this window
// == Is equivalent to WM_MOUSEENTER (for which there is no message)

	if((!m_bMouseTracking || GetCapture()!=this) && (m_ButtonState == BUTTON_OFF))
	{
		OnMouseEnter();
	}
	else 
	{
		if(m_ButtonState == BUTTON_OVER)
		{
			CRect rc;
			GetClientRect(&rc);
			if(!rc.PtInRect(point))	// The mouse cursor is no longer above this button
				OnMouseLeave();
		}
	}

}


void CCartonbutn::OnMouseEnter(void)
{
	// We are now tracking the mouse, OVER this button
	m_bMouseTracking = TRUE;
	m_ButtonState = BUTTON_OVER;
	
	// Ensure that mouse input is sent to the button
	SetCapture();
	Invalidate();
	UpdateWindow();
}



void CCartonbutn::OnMouseLeave(void)
{
	// We are not tracking the mouse, this button is OFF.
	m_ButtonState = BUTTON_OFF;
	m_bMouseTracking = FALSE;
	
	// Release mouse capture from the button and restore normal mouse input
	Invalidate();
	UpdateWindow();
	ReleaseCapture();
}




BUTTON_STATE CCartonbutn::SetButtonState(BUTTON_STATE nState) 
{
	BUTTON_STATE nOldState = (BUTTON_STATE)GetCheck();
	
	m_ButtonState = nState;
	switch(m_ButtonState)
	{
	case BUTTON_ON:
		EnableWindow(TRUE);
		SetState(BUTTON_ON);
		if (m_pOwnerGroup)
		{
			m_pOwnerGroup->ButtonClicked(this);
		}
		break;
	case BUTTON_GREYED:
		EnableWindow(FALSE);
		break;
	case BUTTON_OVER:
		EnableWindow(TRUE);
		SetState(BUTTON_OVER);
		break;
	default:
		EnableWindow(TRUE);
		SetState(BUTTON_OFF);
		m_ButtonState = BUTTON_OFF;
		break;
	}
	return(nOldState);
}


void CCartonbutn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your code to draw the specified item
	CDC			memDC;
	CBitmap*	pOld=NULL;
	CBitmap*	pBitmap=NULL;
	CDC*		pDC;
	CRect		rc;
	int			iSaveDC;
	
	pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
	memDC.CreateCompatibleDC(pDC);
	VERIFY(pDC);
	iSaveDC=pDC->SaveDC();
	rc.CopyRect(&lpDrawItemStruct->rcItem);
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));// Black text color
	
	switch(m_ButtonState)
	{
	case BUTTON_ON:
		pBitmap=&m_bmpButtonDown;
		break;
	case BUTTON_OVER:
		pBitmap=&m_bmpButtonFocussed;
		break;
	case BUTTON_GREYED:
		pBitmap=&m_bmpButtonDisabled;
		break;
	default:
		pBitmap=&m_bmpButtonUp;
		break;
	}
	
	CString		strTitle;
	GetWindowText(strTitle);
	
	if (pBitmap->m_hObject)
	{
		CRect	Buttonrt;

		GetClientRect(&Buttonrt);

		
		BITMAP	bmpInfo;			

		CSize	size;
		
		// Text
		size = pDC->GetTextExtent(strTitle);
		
		
		// Draw bitmap
		if(!pBitmap->GetBitmap(&bmpInfo))
			return;

		CRect	rcBitmap(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight);
		rcBitmap.OffsetRect(size.cx+5,0);

		pOld=memDC.SelectObject((CBitmap*) pBitmap);
		if (pOld==NULL) 
			return; //Destructors will clean up
		
		if(!pDC->StretchBlt(0, 0, Buttonrt.Width(), Buttonrt.Height(), &memDC, 0, 0,bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY))			return;
		memDC.SelectObject(pOld);	
		if(memDC==NULL)
			return;
	}
	
	CRect	rcText(rc);
	UINT nFormat = DT_CENTER;
	
	if(m_ButtonState == BUTTON_GREYED)
	{
		rcText.OffsetRect(1,1);
		pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
		pDC->DrawText(strTitle,rcText,nFormat);
		rcText.OffsetRect(-1,-1);
		pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
		pDC->DrawText(strTitle,rcText,nFormat);
	} 
	else
		pDC->DrawText(strTitle,rcText,nFormat);
	
	pDC->RestoreDC(iSaveDC);
}


BOOL CCartonbutn::LoadBitmaps(UINT nBitmapUp, UINT nBitmapDown, 
							  UINT nBitmapFocus, UINT nBitmapDisabled)
{
	return LoadBitmaps(MAKEINTRESOURCE(nBitmapUp),
		MAKEINTRESOURCE(nBitmapDown),
		MAKEINTRESOURCE(nBitmapFocus),
		MAKEINTRESOURCE(nBitmapDisabled));
}



BOOL CCartonbutn::LoadBitmaps(LPCSTR lpszBitmapUp, LPCSTR lpszBitmapDown, 
							  LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled)
{
	BOOL bAllLoaded=TRUE;

	//Delete old ones
	m_bmpButtonDown.DeleteObject();
	m_bmpButtonFocussed.DeleteObject();
	m_bmpButtonUp.DeleteObject();
	m_bmpButtonDisabled.DeleteObject();
	
	if (!m_bmpButtonUp.LoadBitmap(lpszBitmapUp))
	{
		TRACE0("Failed to load up bitmap of bitmap button\n");
		return FALSE;
	}
	
	if (lpszBitmapDown!=NULL)
	{
		if (!m_bmpButtonDown.LoadBitmap(lpszBitmapDown))
		{
			TRACE0("Failed to load down bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}
	
	if (lpszBitmapFocus!=NULL)
	{
		if (!m_bmpButtonFocussed.LoadBitmap(lpszBitmapFocus))
		{
			TRACE0("Failed to load focussed bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}
	
	if (lpszBitmapDisabled!=NULL)
	{
		if (!m_bmpButtonDisabled.LoadBitmap(lpszBitmapDisabled))
		{
			TRACE0("Failed to load disabled bitmap of bitmap button\n");
			return bAllLoaded=FALSE;
		}
	}	
	return bAllLoaded;

}



void CCartonbutn::PreSubclassWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
//	LoadBitmaps(IDB_RBU, IDB_RBD, IDB_RBF, IDB_RBX);
	CButton::PreSubclassWindow();
}
/*
void CCartonbutn::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_ButtonType == 0)
		SetButtonState(BUTTON_ON);	
	else
		SetButtonState(BUTTON_OFF);	
	CButton::OnLButtonUp(nFlags, point);
    if (m_pOwnerGroup)
	{
		m_pOwnerGroup->ButtonClicked(this);
	}


}
*/
void CCartonbutn::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//if(m_ButtonType == 1)
	//	SetButtonState(BUTTON_ON);	
	
    if (m_pOwnerGroup)
	{
		m_pOwnerGroup->ButtonClicked(this);
	}
	SetButtonState(BUTTON_OFF);	
	CButton::OnLButtonDown(nFlags, point);

}



void CCartonbutn::SetToolTipText(CString s)
{
	if(m_tooltip.m_hWnd==NULL){
		if(m_tooltip.Create(this))	//first assignment
			if(m_tooltip.AddTool(this, (LPCTSTR)s))
				m_tooltip.Activate(1);
	} else {
		m_tooltip.UpdateTipText((LPCTSTR)s,this);
	}
}
void CCartonbutn::RelayEvent(UINT message, WPARAM wParam, LPARAM lParam)
{
// This function will create a MSG structure, fill it in a pass it to
// the ToolTip control, m_ttip.  Note that we ensure the point is in window
// coordinates (relative to the control's window).
	if(NULL != m_tooltip.m_hWnd){
		MSG msg;
		msg.hwnd = m_hWnd;
		msg.message = message;
		msg.wParam = wParam;
		msg.lParam = lParam;
		msg.time = 0;
		msg.pt.x = LOWORD(lParam);
		msg.pt.y = HIWORD(lParam);

		m_tooltip.RelayEvent(&msg);
	}
}

⌨️ 快捷键说明

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