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

📄 menubutton.cpp

📁 EVC++按钮编程
💻 CPP
字号:
// MenuButton.cpp : implementation file
//

#include "stdafx.h"
#include "BtnMenu.h"
#include "MenuButton.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMenuButton

CMenuButton::CMenuButton()
{
	nState=0;
	m_Selected=TRUE;
	b_ClickFlag=FALSE;
	m_bMouseOnButton	= FALSE;
	//初始化mouseleave的事件处理时间捕捉
	bSetMenu=FALSE;
	bMouse=FALSE;

}

CMenuButton::~CMenuButton()
{
}


BEGIN_MESSAGE_MAP(CMenuButton, CButton)
	//{{AFX_MSG_MAP(CMenuButton)
		ON_WM_LBUTTONDOWN()
		ON_WM_LBUTTONUP()
		ON_WM_MOUSEMOVE()
	ON_WM_KILLFOCUS()
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMenuButton message handlers

void CMenuButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	//获取标识该按钮的DC
	CDC * pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
	
	CRect captionRect=lpDrawItemStruct->rcItem;
	
//	CString sTitle;
//	GetWindowText(sTitle);
	
//	DrawText(pDC->m_hDC,_T("kandy"),5,captionRect,DT_LEFT);
	
	//显示位图
	CRect cltRect,wndRect;
	GetClientRect(cltRect);
	
	GetWindowRect(&wndRect);
	GetParent()->ScreenToClient(&wndRect);
	//::StretchBlt()
	//nState=0;
	switch(nState)
	{
	case 0:	
		SelectObject(m_hHdc,m_hbitmap);
		::BitBlt(pDC->m_hDC, 0, 0, cltRect.Width(), cltRect.Height(), m_hHdc, 0, 0, SRCCOPY);
		break;
	case 1://up
		SelectObject(m_hHdc,m_hbitmapUp);
		::BitBlt(pDC->m_hDC, 0, 0, cltRect.Width(), cltRect.Height(), m_hHdc, 0, 0, SRCCOPY);
		pDC->Draw3dRect(cltRect, ::GetSysColor(COLOR_CAPTIONTEXT), ::GetSysColor(COLOR_BTNSHADOW));
		break;
	case 2://down
		if(m_Selected)
		{
			SelectObject(m_hHdc,m_hbitmapDown);
			::BitBlt(pDC->m_hDC, 0, 0, cltRect.Width(), cltRect.Height(), m_hHdc, 0, 0, SRCCOPY);
			pDC->Draw3dRect(cltRect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_HIGHLIGHTTEXT));
			
		}
		else
		{
			//::BitBlt(pDC->m_hDC, 0, 0, cltRect.Width(), cltRect.Height(), m_hHdc, 0, 0, SRCCOPY);
		}
		break;
	case 3:
		
		break;
	}
}

void CMenuButton::PreSubclassWindow()
{
	CButton::PreSubclassWindow();
}


// void CMenuButton::SetBtnBitmap(HBITMAP bitmap)
// {
// 	//m_hbitmap =::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nBitmapResources));
// 	ASSERT(bitmap!=NULL);
// 	m_hbitmap=bitmap;
// 	m_hHdc=::CreateCompatibleDC(NULL);
// 	SelectObject(m_hHdc,m_hbitmap);
// }

void CMenuButton::SetBtnBitmap(UINT nBitmapResources,UINT nBitmapResourcesUp,UINT nBitmapResourceDown)
{
	m_hbitmap =::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nBitmapResources));
	m_hbitmapUp=::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nBitmapResourcesUp));
	m_hbitmapDown=::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(nBitmapResourceDown));

	ASSERT(m_hbitmapDown!=NULL);
	m_hHdc=::CreateCompatibleDC(NULL);
	
}

void CMenuButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	nState=1;
	//Invalidate();
	
	CButton::OnLButtonUp(nFlags,point);
}

void CMenuButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	nState=2;	
	

	CWnd*				wndUnderMouse = NULL;
	CWnd*				wndActive = this;
	
	CButton::OnMouseMove(nFlags, point);
	
	ClientToScreen(&point);
	wndUnderMouse = WindowFromPoint(point);
	
	// If the mouse enter the button with the left button pressed then do nothing
	if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
	
	
	if (!bMouse)
	{
		nState=1;
	}
	

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

void CMenuButton::OnMouseMove(UINT nFlags, CPoint point)
{	

}

void CMenuButton::OnKillFocus(CWnd* pNewWnd) 
{
	CButton::OnKillFocus(pNewWnd);
	
	// TODO: Add your message handler code here
	nState=0;
}

void CMenuButton::OnClicked() 
{
	// TODO: Add your control notification handler code here
	SetFocus();
}

⌨️ 快捷键说明

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