📄 toolbarex.cpp
字号:
// ToolBarEx.cpp : implementation file
//
#include "stdafx.h"
#include "CustomTBar.h"
#include "ToolBarEx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CToolBarEx
CToolBarEx::CToolBarEx()
{
m_nSavedCount = 0;
m_pSavedButtons = NULL;
}
CToolBarEx::~CToolBarEx()
{
delete[] m_pSavedButtons;
}
BEGIN_MESSAGE_MAP(CToolBarEx, CToolBar)
//{{AFX_MSG_MAP(CToolBarEx)
ON_WM_CONTEXTMENU()
ON_COMMAND(IDM_CUSTOM, OnCustomize)
ON_NOTIFY_REFLECT(TBN_BEGINADJUST, OnBeginAdjust)
ON_NOTIFY_REFLECT(TBN_ENDADJUST, OnEndAdjust)
ON_NOTIFY_REFLECT(TBN_GETBUTTONINFO, OnGetButtonInfo)
ON_NOTIFY_REFLECT(TBN_QUERYDELETE, OnQueryDelete)
ON_NOTIFY_REFLECT(TBN_QUERYINSERT, OnQueryInsert)
ON_NOTIFY_REFLECT(TBN_RESET, OnReset)
ON_NOTIFY_REFLECT(TBN_TOOLBARCHANGE, OnToolBarChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CToolBarEx message handlers
void CToolBarEx::OnContextMenu(CWnd* pWnd, CPoint point)
{
CMenu menu;
menu.LoadMenu(IDR_CONTEXTMENU);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON|TPM_LEFTALIGN,point.x,
point.y,this,0);
}
void CToolBarEx::OnCustomize()
{
GetToolBarCtrl().Customize();
}
BOOL CToolBarEx::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style|=CCS_ADJUSTABLE|TBSTYLE_ALTDRAG;
return CToolBar::PreCreateWindow(cs);
}
void CToolBarEx::GetButtons(int& nSavedCount, TBBUTTON*& pSavedButtons)
{
//删除以前状态
delete[] pSavedButtons;
pSavedButtons = NULL;
nSavedCount = 0;
//获取当前状态
int nButtonCount = GetToolBarCtrl().GetButtonCount();
pSavedButtons = new TBBUTTON[nButtonCount];
for (int i = 0; i < nButtonCount; i++)
GetToolBarCtrl().GetButton(i, &pSavedButtons[i]);
nSavedCount = nButtonCount;
}
void CToolBarEx::SetButtons(int nSavedCount, TBBUTTON* pSavedButtons)
{
//移去所有按钮
int nButtonCount = GetToolBarCtrl().GetButtonCount();
while (nButtonCount--)
GetToolBarCtrl().DeleteButton(0);
//添加当前应置按钮
GetToolBarCtrl().AddButtons(nSavedCount, pSavedButtons);
}
BOOL CToolBarEx::LoadToolBar(UINT nIDResource)
{
BOOL bResult = CToolBar::LoadToolBar(nIDResource);
//保存按钮信息
GetButtons(m_nSavedCount, m_pSavedButtons);
return bResult;
}
void CToolBarEx::OnBeginAdjust(NMHDR* pNMHDR, LRESULT* pResult)
{
//此信息用于重置
GetButtons(m_nSavedCount, m_pSavedButtons);
}
void CToolBarEx::OnEndAdjust(NMHDR* pNMHDR, LRESULT* pResult)
{
//当关闭自定义对话框时
delete[] m_pSavedButtons;
m_pSavedButtons = NULL;
}
void CToolBarEx::OnGetButtonInfo(NMHDR* pNMHDR, LRESULT* pResult)
{
//获取按钮的Tip作为对话框中的名称
TBNOTIFY *pNotify = (TBNOTIFY*)pNMHDR;
//当pNotify->iItem与m_nSavedCount相等时,说明GetButtonInfo已完成
if (pNotify->iItem >= m_nSavedCount)
{
*pResult = FALSE;
return;
}
//获取一个Button信息
pNotify->tbButton = m_pSavedButtons[pNotify->iItem];
//获得关于该Button的Command ID信息
CString buffer;
buffer.LoadString(pNotify->tbButton.idCommand);
CString descript;
//获取ToolTip信息
AfxExtractSubString(descript, buffer, 2);
//若无ToolTip信息
if (descript.IsEmpty())
AfxExtractSubString(descript, buffer, 1);
//对pNotify->pszText附值
lstrcpyn(pNotify->pszText, descript, pNotify->cchText);
*pResult = TRUE;
}
void CToolBarEx::OnQueryDelete(NMHDR* pNMHDR, LRESULT* pResult)
{
//不由系统控制
*pResult = TRUE;
}
void CToolBarEx::OnQueryInsert(NMHDR* pNMHDR, LRESULT* pResult)
{
//不由系统控制
*pResult = TRUE;
}
void CToolBarEx::OnReset(NMHDR* pNMHDR, LRESULT* pResult)
{
SetButtons(m_nSavedCount, m_pSavedButtons);
//改变ToolBar尺寸
OnToolBarChange(pNMHDR, pResult);
*pResult = TRUE;
}
void CToolBarEx::OnToolBarChange(NMHDR* pNMHDR, LRESULT* pResult)
{
//改变ToolBar尺寸
GetParentFrame()->RecalcLayout();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -