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

📄 colortoolbar.cpp

📁 学生信息管理系统
💻 CPP
字号:
#include "stdafx.h"
#include "ColorToolBar.h"


/************************************************************************/ 
BEGIN_MESSAGE_MAP(CColorToolBar, CToolBar)
	//{{AFX_MSG_MAP(CColorToolBar)
	ON_NOTIFY_REFLECT(TBN_DROPDOWN, OnToolbarDropDown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



/************************************************************************/ 
CColorToolBar::CColorToolBar()
{
	m_bIsDropBtn = false;
}



/*================================================================ 
* 函数名:    Set
* 功能描述:   给工具条设置图象列表 (私有函数)
================================================================*/ 
BOOL CColorToolBar::SetImage(UINT ImageType, UINT uToolBar, int nBtnWidth)
{
	CImageList	ImageList;
	CBitmap		bmp;
	BITMAP		bm;
	
	if (!bmp.Attach(::LoadImage(AfxGetResourceHandle(),     //加载位图
								MAKEINTRESOURCE(uToolBar),
								IMAGE_BITMAP, 0, 0,
								LR_DEFAULTSIZE|LR_CREATEDIBSECTION)) ||
	    !bmp.GetBitmap(&bm))
	{
		return false;
	}

	CSize		cSize(bm.bmWidth, bm.bmHeight); //获得位图的尺寸
	int			nNbBtn	= cSize.cx/nBtnWidth;
	RGBTRIPLE*	rgb		= (RGBTRIPLE*)(bm.bmBits);
	COLORREF	rgbMask	= RGB(rgb[0].rgbtRed, rgb[0].rgbtGreen, rgb[0].rgbtBlue);
	
	if (!ImageList.Create(nBtnWidth, cSize.cy, ILC_COLOR24|ILC_MASK, nNbBtn, 0))  //创建图象列表
		return false;
	
	if (ImageList.Add(&bmp, rgbMask) == -1)
		return false;

	SendMessage(ImageType, 0, (LPARAM)ImageList.m_hImageList); //发送设置图象列表的消息

	ImageList.Detach(); 
	bmp.Detach();
	
	return TRUE;
}


/*================================================================ 
* 函数名:    SetColorToolBar
* 参数:      int nBtnWidth, UINT uBmpNormal, UINT uBmpHot, UINT uBmpDisable)
* 功能描述:   设置真彩位图(正常时,鼠标在上面时,不可时)
* 返回值:    BOOL
================================================================*/ 
BOOL CColorToolBar::SetColorToolBar(int nBtnWidth, UINT uBmpNormal, UINT uBmpHot, UINT uBmpDisable)
{
	if (! this->SetImage(TB_SETIMAGELIST, uBmpNormal, nBtnWidth))  //正常时
		return false;
	
	if (uBmpHot)             //鼠标移动到按扭上时
	{
		if (! this->SetImage(TB_SETHOTIMAGELIST, uBmpHot, nBtnWidth))
			return false;
	}
	
	if (uBmpDisable)  //不可用时
	{
		if (! this->SetImage(TB_SETDISABLEDIMAGELIST, uBmpDisable, nBtnWidth))  
			return false;
	}
	
	return TRUE;
}


/*================================================================ 
* 函数名:    SetDropButton
* 功能描述:   设置下拉按扭
================================================================*/ 
void CColorToolBar::SetDropButton(CWnd* pParent, UINT uButtonID, UINT uMenuID)
{
	if (!m_bIsDropBtn) 
	{
		GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_DRAWDDARROWS);
		m_bIsDropBtn = TRUE;
	}

	SetButtonStyle(CommandToIndex(uButtonID), TBSTYLE_DROPDOWN);

	DROP_INFO info;
	info.pParent	= pParent;
	info.uButtonID	= uButtonID;
	info.uMenuID	= uMenuID;

	m_DropInfoArray.Add(info);
}



/*================================================================ 
* 函数名:    OnToolbarDropDown
* 功能描述:   消息处理函数
================================================================*/ 
void CColorToolBar::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)
{
	for (int i = 0; i < m_DropInfoArray.GetSize(); i++) 
	{
		DROP_INFO info = m_DropInfoArray.GetAt(i);

		if (info.uButtonID == UINT(pnmtb->iItem)) 
		{
			CMenu menu;
			menu.LoadMenu(info.uMenuID);
			CMenu* pPopup = menu.GetSubMenu(0);
			
			CRect rc;
			SendMessage(TB_GETRECT, (WPARAM)pnmtb->iItem, (LPARAM)&rc);
			ClientToScreen(&rc);
			
			pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL,
				                   rc.left, rc.bottom, info.pParent, &rc);
			break;
		}
	}
}

⌨️ 快捷键说明

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