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

📄 toolbarex.cpp

📁 The ITU-T(Telecommunication Standardization Sector)is a permanent organ of the International Telecom
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		{
			ASSERT(eIconOptions==ioLargeIcons);
			CSize  sizeLargeImage(nCount *m_szImageLarge.cx,m_szImageLarge.cy);
			hBitmap= AfxLoadSysColorBitmapSize(m_hInstImageWell, m_hRsrcImageWell,sizeLargeImage);
		}

		ASSERT(hBitmap);
		SetBitmap(hBitmap);

		m_hInstImageWell = hInstImageWell; // SetBitmap resets these
		m_hRsrcImageWell = hRsrcImageWell; // SetBitmap resets these
		return TRUE;
	}


	return FALSE;
}

void CToolBarEx::OnSysColorChange() 
{
	m_clrBtnShadow=::GetSysColor(COLOR_BTNSHADOW);
	m_clrBtnHilight=::GetSysColor(COLOR_BTNHILIGHT);
	m_clrBtnFace  = ::GetSysColor(COLOR_BTNFACE);
	SetIconOptions(m_eIconOptions);	//load bitmap again
}

void CToolBarEx::OnSave( NMHDR* pNMHDR, LRESULT* pResult )
{
    NMTBSAVE* lpnmtb = ( NMTBSAVE* )pNMHDR;
    if ( lpnmtb->iItem == -1 )
    {
        lpnmtb->cbData  += sizeof( DWORD ) * 2;
        lpnmtb->pData    = ( LPDWORD )::GlobalAlloc( GMEM_FIXED, lpnmtb->cbData );  //freed by Toolbar ( Not sure)
        lpnmtb->pCurrent = lpnmtb->pData;

        *lpnmtb->pCurrent++ = (DWORD)m_eIconOptions;
        *lpnmtb->pCurrent++ = (DWORD)m_eTextOptions;
    }

    *pResult = 0;
}

void CToolBarEx::OnRestore( NMHDR* pNMHDR, LRESULT* pResult )
{
    NMTBRESTORE* lpnmtb = ( NMTBRESTORE* )pNMHDR;

    if ( lpnmtb->iItem == -1 )
    {
        lpnmtb->cButtons = ( lpnmtb->cbData - sizeof( DWORD ) * 2 ) / lpnmtb->cbBytesPerRecord;
        lpnmtb->pCurrent = lpnmtb->pData;

        m_eIconOptions=(IconOptions)*lpnmtb->pCurrent++ ;
        m_eTextOptions=(TextOptions)*lpnmtb->pCurrent++ ;
    }

    *pResult = 0;
}


BOOL CToolBarEx::ShowContextMenu(CPoint point)
{

	if( GetToolBarCtrl().GetButtonCount() && m_ToolBarInfo.GetSize())
	{
		CPoint pt( point );
		ClientToScreen( &pt );
		
		// load and display popup menu
		CMenu popupMenu;
		VERIFY(popupMenu.CreatePopupMenu());

		CFrameWnd * pFrame=(CFrameWnd *)AfxGetMainWnd();

		if (pFrame && pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd)) )
		{
			ASSERT_VALID(pFrame);
			POSITION pos = pFrame->m_listControlBars.GetHeadPosition();

			while (pos != NULL)
			{
				const CControlBar* pBar = (CControlBar*)pFrame->m_listControlBars.GetNext(pos);
				ASSERT(pBar != NULL);
				
				if (pBar && 
					pBar->IsKindOf(RUNTIME_CLASS(CToolBar))) // Put any other type

				{
					ASSERT_VALID(pBar);
					CString strName;
					pBar->GetWindowText(strName);
					popupMenu.AppendMenu( (pBar->GetStyle()&WS_VISIBLE?MF_CHECKED:MF_UNCHECKED)|MF_STRING,
											pBar->GetDlgCtrlID(),
											strName);
				}
			}
			
			//Add a Seperator
			popupMenu.AppendMenu(MF_SEPARATOR) ;
		}

		
		//Finally a Customize
		popupMenu.AppendMenu(MF_STRING ,ID_CUSTOMIZE_BAR, STR_CUSTOMIZE);

		int nResult = popupMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL|TPM_RETURNCMD,pt.x, pt.y, this );
	
		switch(nResult)
		{
			case 0:		//Nothing clicked  on menu
				break;
			case ID_CUSTOMIZE_BAR  :
				// open the customization dialog.
				Customize();
			break;
			default:
				{
					ASSERT_VALID(pFrame);
					if (pFrame && pFrame->IsKindOf(RUNTIME_CLASS(CFrameWnd)) )
					{	
						pFrame->OnBarCheck(nResult);
					}
				}
		}
		return TRUE;
	}


	return FALSE;

}

void CToolBarEx::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	ASSERT_VALID(pWnd);
	CPoint pt(point);

	if (WindowFromPoint(pt)!=this) return;

	ScreenToClient(&pt);
	if (!ShowContextMenu(pt))
		CToolBar::OnContextMenu(pWnd,point);
}

void CToolBarEx::Customize()
{
	GetToolBarCtrl().Customize();
}

void CToolBarEx::OnInitCustomize( NMHDR* /*pNMHDR*/, LRESULT* pResult )
{
    *pResult = TBNRF_HIDEHELP;  // help button  is hidden      
}

void CToolBarEx::OnNcPaint() 
{
	if( GetToolBarCtrl().GetStyle()& TBSTYLE_FLAT ) 
	{
		// get window DC that is clipped to the non-client area
		CWindowDC dc(this);
		CRect rectClient;
		GetClientRect(rectClient);
		CRect rectWindow;
		GetWindowRect(rectWindow);
		ScreenToClient(rectWindow);
		rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
		dc.ExcludeClipRect(rectClient);

		// draw borders in non-client area
		rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);

		Draw3DBorders(&dc, rectWindow);

		dc.IntersectClipRect(rectWindow);

		// erase parts not drawn
		SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);

		DrawGripper(&dc,rectWindow);
	} 
	else
	{
		CToolBar::OnNcPaint();
	}
}

void CToolBarEx :: Draw3DBorders(CDC * pDC, CRect & rect) 
{
	ASSERT_VALID(this);
	ASSERT_VALID(pDC);

	if( GetToolBarCtrl().GetStyle()& TBSTYLE_FLAT ) 
	{
		DWORD dwStyle = m_dwStyle;
		if (!(dwStyle & CBRS_BORDER_ANY))
			return;

		COLORREF clr = (m_dwStyle & CBRS_BORDER_3D) ? m_clrBtnHilight : m_clrBtnShadow;
		if(m_dwStyle & CBRS_BORDER_LEFT)
			pDC->FillSolidRect(0, 0, 1, rect.Height() - 1, clr);
		if(m_dwStyle & CBRS_BORDER_TOP)
			pDC->FillSolidRect(0, 0, rect.Width()-1 , 1, clr);
		if(m_dwStyle & CBRS_BORDER_RIGHT)
			pDC->FillSolidRect(rect.right, 1, -1, rect.Height() - 1, m_clrBtnShadow);
		if(m_dwStyle & CBRS_BORDER_BOTTOM)
			pDC->FillSolidRect(0, rect.bottom, rect.Width()-1, -1, m_clrBtnShadow);

		// if undockable toolbar at top of frame, apply special formatting to mesh
		// properly with frame menu
		if(!m_pDockContext) 
		{
			pDC->FillSolidRect(0,0,rect.Width(),1,m_clrBtnShadow);
			pDC->FillSolidRect(0,1,rect.Width(),1,m_clrBtnHilight);
		}

		if (dwStyle & CBRS_BORDER_LEFT)
			++rect.left;
		if (dwStyle & CBRS_BORDER_TOP)
			++rect.top;
		if (dwStyle & CBRS_BORDER_RIGHT)
			--rect.right;
		if (dwStyle & CBRS_BORDER_BOTTOM)
			--rect.bottom;
	} 
	else
	{
		DrawBorders(pDC, rect);
	}


}
	 

/////////////////////////////////////////////////////////////////////////////
// CDockBarEx

CDockBarEx::CDockBarEx()
{
	// TODO: add construction code here.
}

CDockBarEx::~CDockBarEx()
{
	// TODO: add destruction code here.
}

IMPLEMENT_DYNAMIC(CDockBarEx, CDockBar)

BEGIN_MESSAGE_MAP(CDockBarEx, CDockBar)
	//{{AFX_MSG_MAP(CDockBarEx)				  
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDockBarEx message handlers

CSize CDockBarEx::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
	ASSERT_VALID(this);

	CSize sizeFixed = CControlBar::CalcFixedLayout(bStretch, bHorz);

	// get max size
	CSize sizeMax;
	if (!m_rectLayout.IsRectEmpty())
	{
		CRect rect = m_rectLayout;
		CalcInsideRect(rect, bHorz);
		sizeMax = rect.Size();
	}
	else
	{
		CRect rectFrame;
		CFrameWnd* pFrame = GetParentFrame();
		pFrame->GetClientRect(&rectFrame);
		sizeMax = rectFrame.Size();
	}

	// prepare for layout
	AFX_SIZEPARENTPARAMS layout;
	layout.hDWP = m_bLayoutQuery ?
		NULL : ::BeginDeferWindowPos(m_arrBars.GetSize());
	int cxBorder = 2, cyBorder = 2;
	CPoint pt(-cxBorder, -cyBorder);
	int nWidth = 0;

	BOOL bWrapped = FALSE;

	// layout all the control bars
	for (int nPos = 0; nPos < m_arrBars.GetSize(); nPos++)
	{
		CControlBar* pBar = GetDockedControlBar(nPos);
		void* pVoid = m_arrBars[nPos];

		if (pBar != NULL)
		{
			if(pBar->IsKindOf(RUNTIME_CLASS(CToolBarEx)))
			{
			  if (pBar->GetStyle() & TBSTYLE_FLAT)
				cxBorder = cyBorder = 0;
			  else
				cxBorder = cyBorder = 2;
			}
			else
				cxBorder = cyBorder = 2;

			if (pBar->IsVisible())
			{
				// get ideal rect for bar
				DWORD dwMode = 0;
				if ((pBar->m_dwStyle & CBRS_SIZE_DYNAMIC) &&
					(pBar->m_dwStyle & CBRS_FLOATING))
					dwMode |= LM_HORZ | LM_MRUWIDTH;
				else if (pBar->m_dwStyle & CBRS_ORIENT_HORZ)
					dwMode |= LM_HORZ | LM_HORZDOCK;
				else
					dwMode |=  LM_VERTDOCK;

				CSize sizeBar = pBar->CalcDynamicLayout(-1, dwMode);

				CRect rect(pt, sizeBar);

				// get current rect for bar
				CRect rectBar;
				pBar->GetWindowRect(&rectBar);
				ScreenToClient(&rectBar);

				if (bHorz)
				{
					// Offset Calculated Rect out to Actual
					if (rectBar.left > rect.left && !m_bFloating)
						rect.OffsetRect(rectBar.left - rect.left, 0);

					// If ControlBar goes off the right, then right justify
					if (rect.right > sizeMax.cx && !m_bFloating)
					{
						int x = rect.Width() - cxBorder;
						x = max(sizeMax.cx - x, pt.x);
						rect.OffsetRect(x - rect.left, 0);
					}

					// If ControlBar has been wrapped, then left justify
					if (bWrapped)
					{
						bWrapped = FALSE;
						rect.OffsetRect(-(rect.left + cxBorder), 0);
					}
					// If ControlBar is completely invisible, then wrap it
					else if ((rect.left >= (sizeMax.cx - cxBorder)) &&
						(nPos > 0) && (m_arrBars[nPos - 1] != NULL))
					{
						m_arrBars.InsertAt(nPos, (CObject*)NULL);
						pBar = NULL; pVoid = NULL;
						bWrapped = TRUE;
					}
					if (!bWrapped)
					{
						if (rect != rectBar)
						{
							if (!m_bLayoutQuery &&
								!(pBar->m_dwStyle & CBRS_FLOATING))
							{
								pBar->m_pDockContext->m_rectMRUDockPos = rect;
							}
							AfxRepositionWindow(&layout, pBar->m_hWnd, &rect);
						}
						pt.x = rect.left + sizeBar.cx - cxBorder;
						nWidth = max(nWidth, sizeBar.cy);
					}
				}
				else
				{
					// Offset Calculated Rect out to Actual
					if (rectBar.top > rect.top && !m_bFloating)
						rect.OffsetRect(0, rectBar.top - rect.top);

					// If ControlBar goes off the bottom, then bottom justify
					if (rect.bottom > sizeMax.cy && !m_bFloating)
					{
						int y = rect.Height() - cyBorder;
						y = max(sizeMax.cy - y, pt.y);
						rect.OffsetRect(0, y - rect.top);
					}

					// If ControlBar has been wrapped, then top justify
					if (bWrapped)
					{
						bWrapped = FALSE;
						rect.OffsetRect(0, -(rect.top + cyBorder));
					}
					// If ControlBar is completely invisible, then wrap it
					else if ((rect.top >= (sizeMax.cy - cyBorder)) &&
						(nPos > 0) && (m_arrBars[nPos - 1] != NULL))
					{
						m_arrBars.InsertAt(nPos, (CObject*)NULL);
						pBar = NULL; pVoid = NULL;
						bWrapped = TRUE;
					}
					if (!bWrapped)
					{
						if (rect != rectBar)
						{
							if (!m_bLayoutQuery &&
								!(pBar->m_dwStyle & CBRS_FLOATING))
							{
								pBar->m_pDockContext->m_rectMRUDockPos = rect;
							}
							AfxRepositionWindow(&layout, pBar->m_hWnd, &rect);
						}
						pt.y = rect.top + sizeBar.cy - cyBorder;
						nWidth = max(nWidth, sizeBar.cx);
					}
				}
			}
			if (!bWrapped)
			{
				// handle any delay/show hide for the bar
				pBar->RecalcDelayShow(&layout);
			}
		}
		if (pBar == NULL && pVoid == NULL && nWidth != 0)
		{
			// end of row because pBar == NULL
			if (bHorz)
			{
				pt.y += nWidth - cyBorder;
				sizeFixed.cx = max(sizeFixed.cx, pt.x);
				sizeFixed.cy = max(sizeFixed.cy, pt.y);
				pt.x = -cxBorder;
			}
			else
			{
				pt.x += nWidth - cxBorder;
				sizeFixed.cx = max(sizeFixed.cx, pt.x);
				sizeFixed.cy = max(sizeFixed.cy, pt.y);
				pt.y = -cyBorder;
			}
			nWidth = 0;
		}
	}
	if (!m_bLayoutQuery)
	{
		// move and resize all the windows at once!
		if (layout.hDWP == NULL || !::EndDeferWindowPos(layout.hDWP))
			TRACE0("Warning: DeferWindowPos failed - low system resources.\n");
	}

	// adjust size for borders on the dock bar itself
	CRect rect;
	rect.SetRectEmpty();
	CalcInsideRect(rect, bHorz);

	if ((!bStretch || !bHorz) && sizeFixed.cx != 0)
		sizeFixed.cx += -rect.right + rect.left;
	if ((!bStretch || bHorz) && sizeFixed.cy != 0)
		sizeFixed.cy += -rect.bottom + rect.top;

	return sizeFixed;
}



// dwDockBarMap
const DWORD dwDockBarMap[4][2] =
{
	{ AFX_IDW_DOCKBAR_TOP,      CBRS_TOP    },
	{ AFX_IDW_DOCKBAR_BOTTOM,   CBRS_BOTTOM },
	{ AFX_IDW_DOCKBAR_LEFT,     CBRS_LEFT   },
	{ AFX_IDW_DOCKBAR_RIGHT,    CBRS_RIGHT  },
};


void FrameEnableDocking(CFrameWnd * pFrame, DWORD dwDockStyle) 
{
	ASSERT_VALID(pFrame);

	// must be CBRS_ALIGN_XXX or CBRS_FLOAT_MULTI only
	ASSERT((dwDockStyle & ~(CBRS_ALIGN_ANY|CBRS_FLOAT_MULTI)) == 0);

	pFrame->EnableDocking(dwDockStyle);

	for (int i = 0; i < 4; i++) {
		if (dwDockBarMap[i][1] & dwDockStyle & CBRS_ALIGN_ANY) {
			CDockBar* pDock = (CDockBar*)pFrame->GetControlBar(dwDockBarMap[i][0]);
			
			// make sure the dock bar is of correct type
			if( pDock == 0 || ! pDock->IsKindOf(RUNTIME_CLASS(CDockBarEx)) ) {
				BOOL bNeedDelete = ! pDock->m_bAutoDelete;
				pDock->m_pDockSite->RemoveControlBar(pDock);
				pDock->m_pDockSite = 0;	// avoid problems in destroying the dockbar
				pDock->DestroyWindow();
				if( bNeedDelete )
					delete pDock;
				pDock = 0;
			}

			if( pDock == 0 ) {
				pDock = new CDockBarEx;
				ASSERT_VALID(pDock);
				if ((!pDock) || (!pDock->Create(pFrame,
					WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_CHILD|WS_VISIBLE |
						dwDockBarMap[i][1], dwDockBarMap[i][0]))) {
					AfxThrowResourceException();
				}
			}
		}
	}
}


BOOL CToolBarEx::MarkDefaultState()
{

	 CToolBarCtrl & tbCtrl = GetToolBarCtrl();

		 //Set intial Visible info
	for (int i=0;i<m_ToolBarInfo.GetSize();i++)
	{
		if  ( (m_ToolBarInfo[i].tbButton.fsStyle & TBSTYLE_SEP) == 0) 	//   seperator	?

		{	 // Not a seperator
			 if  (tbCtrl.CommandToIndex(m_ToolBarInfo[i].tbButton.idCommand)==-1)	// button exists on toolbar
			{
				m_ToolBarInfo[i].bInitiallyVisible=FALSE;
			}
			else
			{
				m_ToolBarInfo[i].bInitiallyVisible=TRUE;
			}
		}
		else
		{
			if (m_ToolBarInfo[i].tbButton.idCommand>0)
			{
				if (tbCtrl.CommandToIndex(m_ToolBarInfo[i].tbButton.idCommand)==-1)
				{
					m_ToolBarInfo[i].bInitiallyVisible=FALSE;
				}
				else
				{
					m_ToolBarInfo[i].bInitiallyVisible=TRUE;
				}
			}
			else
			{
					m_ToolBarInfo[i].bInitiallyVisible=TRUE;	// simple seperatores are visible
			}
		}
	}
	return TRUE;
}

⌨️ 快捷键说明

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