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

📄 bcgpcolorbar.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	int nColors = m_colors.GetSize ();

	if (!m_bIsTearOff || IsFloating () || bVertDock || m_nNumRowsHorz <= 0)
	{
		nNumColumns = !m_bIsTearOff || IsFloating () || m_nNumColumnsVert <= 0 ?
			m_nNumColumns : m_nNumColumnsVert;

		if (nNumColumns <= 0)
		{
			nNumColumns = (int) (sqrt ((double) nColors)) + 1;
		}

		nNumRows = nColors / nNumColumns;
		if ((nColors % nNumColumns) != 0)
		{
			nNumRows ++;
		}
	}
	else	// Horz dock
	{
		nNumRows = m_nNumRowsHorz;
		nNumColumns = nColors / nNumRows;

		if ((nColors % nNumRows) != 0)
		{
			nNumColumns ++;
		}
	}

	return CSize (nNumColumns, nNumRows);
}
//**************************************************************************************
int CBCGPColorBar::GetExtraHeight (int nNumColumns) const
//
// Calculate additional height required by the misc. elements such
// as "Other" button, document colors, e.t.c
//
{
	int nExtraHeight = 0;

	if (!m_strAutoColor.IsEmpty ())
	{
		nExtraHeight += m_nRowHeight;
	}
	else if (!m_strOtherColor.IsEmpty ())
	{
		nExtraHeight += m_nVertMargin;
	}

	if (!m_strOtherColor.IsEmpty ())
	{
		nExtraHeight += m_nRowHeight;
	}

	if (!m_strDocColors.IsEmpty () && !m_lstDocColors.IsEmpty () &&
		(m_bShowDocColorsWhenDocked || IsFloating ()))
	{
		int nDocColorRows = m_lstDocColors.GetCount () / nNumColumns;
		if ((m_lstDocColors.GetCount () % nNumColumns) != 0)
		{
			nDocColorRows++;
		}

		nExtraHeight += m_nRowHeight + nDocColorRows * m_BoxSize.cy + 2 * SEPARATOR_SIZE + m_nVertMargin;
	}

	return nExtraHeight;
}

BEGIN_MESSAGE_MAP(CBCGPColorBar, CBCGPPopupMenuBar)
	//{{AFX_MSG_MAP(CBCGPColorBar)
	ON_WM_CREATE()
	ON_WM_QUERYNEWPALETTE()
	ON_WM_PALETTECHANGED()
	ON_WM_NCCALCSIZE()
	ON_WM_NCPAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBCGPColorBar message handlers

void CBCGPColorBar::SetDocumentColors (LPCTSTR lpszCaption,
						CList<COLORREF,COLORREF>& lstDocColors,
						BOOL bShowWhenDocked)
{
	m_strDocColors = lpszCaption == NULL ? _T("") : lpszCaption;

	if (m_lstDocColors.GetCount () == lstDocColors.GetCount ())
	{
		BOOL bChanged = FALSE;

		POSITION posCur = m_lstDocColors.GetHeadPosition ();
		POSITION posNew = lstDocColors.GetHeadPosition ();

		while (posCur != NULL && posNew != NULL)
		{
			if (m_lstDocColors.GetNext (posCur) !=
				lstDocColors.GetNext (posNew))
			{
				bChanged = TRUE;
				break;
			}
		}

		if (!bChanged)
		{
			return;
		}
	}

	m_lstDocColors.RemoveAll ();
	m_lstDocColors.AddTail (&lstDocColors);

	m_bShowDocColorsWhenDocked = bShowWhenDocked;

	Rebuild ();
	AdjustLayout ();
}
//**************************************************************************************
void CBCGPColorBar::ContextToSize (BOOL bSquareButtons, BOOL bCenterButtons)
{
	ASSERT (GetSafeHwnd () != NULL);

	CRect rectClient;
	GetClientRect (rectClient);

	// First, adjust height:
	int nCurrHeight = CalcSize (TRUE).cy;
	int yDelta = nCurrHeight < rectClient.Height () ? 1 : -1;

	while ((nCurrHeight = CalcSize (TRUE).cy) != rectClient.Height ())
	{
		if (yDelta < 0)
		{
			if (nCurrHeight < rectClient.Height ())
			{
				break;
			}
		}
		else if (nCurrHeight > rectClient.Height ())
		{
			m_BoxSize.cy -= yDelta;
			m_nRowHeight = m_BoxSize.cy * 3 / 2;
			break;
		}

		m_BoxSize.cy += yDelta;
		m_nRowHeight = m_BoxSize.cy * 3 / 2;
	}

	// Now, adjust width:
	int nCurrWidth = CalcSize (TRUE).cx;
	int xDelta = nCurrWidth < rectClient.Width () ? 1 : -1;

	while ((nCurrWidth = CalcSize (TRUE).cx) != rectClient.Width ())
	{
		if (xDelta < 0)
		{
			if (nCurrWidth < rectClient.Width ())
			{
				break;
			}
		}
		else if (nCurrWidth > rectClient.Width ())
		{
			m_BoxSize.cy -= xDelta;
			break;
		}

		m_BoxSize.cx += xDelta;
	}

	m_BoxSize.cx--;
	m_BoxSize.cy--;

	if (bSquareButtons)
	{
		m_BoxSize.cx = m_BoxSize.cy = min (m_BoxSize.cx, m_BoxSize.cy);
		m_nRowHeight = m_BoxSize.cy * 3 / 2;
	}

	if (bCenterButtons)
	{
		// Finaly, calculate offset to center buttons area:
		CSize size = CalcSize (TRUE);

		m_nHorzOffset = (rectClient.Width () - size.cx) / 2;
		m_nVertOffset = (rectClient.Height () - size.cy) / 2;
	}
	else
	{
		m_nHorzOffset = m_nVertOffset = 0;
	}

	AdjustLocations ();
}
//**************************************************************************************
void CBCGPColorBar::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp) 
{
	if (m_bIsTearOff)
	{
		CBCGPToolBar::OnNcCalcSize(bCalcValidRects, lpncsp);
	}
	else
	{
		CBCGPPopupMenuBar::OnNcCalcSize(bCalcValidRects, lpncsp);
	}
}
//**************************************************************************************
void CBCGPColorBar::OnNcPaint() 
{
	if (m_bIsTearOff)
	{
		CBCGPToolBar::OnNcPaint();
	}
	else
	{
		CBCGPPopupMenuBar::OnNcPaint();
	}
}
//**************************************************************************************
int CBCGPColorBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CBCGPPopupMenuBar::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_BoxSize = GetMenuImageSize ();
	m_BoxSize.cx ++;
	m_BoxSize.cy ++;

	m_bLeaveFocus = FALSE;
	m_nRowHeight = m_BoxSize.cy * 3 / 2;
	Rebuild ();

	if (m_pParentBtn != NULL)
	{
		SetCapture ();
		m_pParentBtn->m_bCaptured = FALSE;
	}
	else if (m_pWndPropList != NULL)
	{
		SetCapture ();
	}

	return 0;
}
//*************************************************************************************
void CBCGPColorBar::Rebuild ()
{
	if (GetSafeHwnd () == NULL)
	{
		return;
	}

	RemoveAllButtons ();

	BOOL bAlreadySelected = FALSE;
	if (!m_strAutoColor.IsEmpty ())	// Automatic
	{
		InsertButton (
			new CColorButton (m_ColorAutomatic,
				TRUE, FALSE, m_strAutoColor,
				m_ColorSelected == (COLORREF) -1));

		if (!bAlreadySelected)
		{
			bAlreadySelected = (m_ColorSelected == (COLORREF) -1);
		}
	}
	
	for (int i = 0; i < m_colors.GetSize (); i ++)
	{
		InsertButton (
			new CColorButton (m_colors [i], FALSE, FALSE, NULL, m_ColorSelected == m_colors [i]));

		if (!bAlreadySelected)
		{
			bAlreadySelected = (m_ColorSelected == m_colors [i]);
		}
	}
	
	if (!m_strDocColors.IsEmpty () && !m_lstDocColors.IsEmpty ())
	{
		InsertSeparator ();
		InsertButton (new CColorButton (m_strDocColors, TRUE));	// Label

		for (POSITION pos = m_lstDocColors.GetHeadPosition (); pos != NULL;)
		{
			COLORREF color = m_lstDocColors.GetNext (pos);

			InsertButton (
				new CColorButton (color,
					FALSE, FALSE, NULL,
					!bAlreadySelected && m_ColorSelected == color,
					TRUE));
		}
	}

	if (!m_strOtherColor.IsEmpty ())	// Other color button
	{
		InsertSeparator ();
		InsertButton (
			new CColorButton ((COLORREF) -1,FALSE, TRUE, m_strOtherColor));
		InsertButton (
			new CColorButton (m_ColorSelected,
				FALSE, FALSE, NULL,
				!bAlreadySelected,
				FALSE, TRUE));
	}
}

class CBCGPColorCCmdUI : public CCmdUI
{
public:
	CBCGPColorCCmdUI();

public: // re-implementations only
	virtual void Enable(BOOL bOn);
	BOOL m_bEnabled;
};

CBCGPColorCCmdUI::CBCGPColorCCmdUI()
{
	m_bEnabled = TRUE;  // assume it is enabled
}
//*************************************************************************************
void CBCGPColorCCmdUI::Enable(BOOL bOn)
{
	m_bEnabled = bOn;
	m_bEnableChanged = TRUE;
}
//*************************************************************************************
void CBCGPColorBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
	ASSERT_VALID (this);

	if (m_nCommandID == 0 || m_nCommandID == (UINT)-1)
	{
		CBCGPPopupMenuBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);
		return;
	}

	CBCGPColorCCmdUI state;
	state.m_pOther = this;
	state.m_nIndexMax = 1;
	state.m_nID = m_nCommandID;

	BOOL bIsEnabled = FALSE;
	if (pTarget->OnCmdMsg (m_nCommandID, CN_UPDATE_COMMAND_UI, &state, NULL))
	{
		bIsEnabled = state.m_bEnabled;
	}
	else if (bDisableIfNoHndler && !state.m_bEnableChanged)
	{
		AFX_CMDHANDLERINFO info;
		info.pTarget = NULL;

		bIsEnabled = pTarget->OnCmdMsg (m_nCommandID, CN_COMMAND, &state, &info);
	}

	if (bIsEnabled != m_bIsEnabled)
	{
		m_bIsEnabled = bIsEnabled;

		for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL;)
		{
			CColorButton* pColorButton = DYNAMIC_DOWNCAST (CColorButton, m_Buttons.GetNext (pos));
			if (pColorButton != NULL)
			{
				pColorButton->m_nStyle &= ~TBBS_DISABLED;
				if (!bIsEnabled)
				{
					pColorButton->m_nStyle |= TBBS_DISABLED;
				}
			}
		}

		Invalidate ();
		UpdateWindow ();
	}

	CBCGPPopupMenuBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);
}
//*************************************************************************************
void CBCGPColorBar::DoPaint (CDC* pDC)
{
    CPalette* pOldPalette = SelectPalette (pDC);

	CBCGPPopupMenuBar::DoPaint (pDC);

    if (pOldPalette != NULL)
	{
        pDC->SelectPalette (pOldPalette, FALSE);
	}
}
//*************************************************************************************
BOOL CBCGPColorBar::OnQueryNewPalette() 
{
    Invalidate();    
	UpdateWindow ();
	return CBCGPPopupMenuBar::OnQueryNewPalette();
}
//*************************************************************************************
void CBCGPColorBar::OnPaletteChanged(CWnd* pFocusWnd) 
{
	CBCGPPopupMenuBar::OnPaletteChanged(pFocusWnd);
	
    if (pFocusWnd->GetSafeHwnd() != GetSafeHwnd())
	{
        Invalidate();
		UpdateWindow ();
	}
}
//*************************************************************************************
BOOL CBCGPColorBar::OnSendCommand (const CBCGPToolbarButton* pButton)
{
	if (m_pParentBtn != NULL || m_pWndPropList != NULL)
	{
		ReleaseCapture ();
	}

	COLORREF color = (COLORREF) -1;

	CBCGPColorMenuButton* pColorMenuButton = NULL;

	CBCGPPopupMenu* pParentMenu = DYNAMIC_DOWNCAST (CBCGPPopupMenu, GetParent ());
	if (pParentMenu != NULL)
	{
		pColorMenuButton = DYNAMIC_DOWNCAST (CBCGPColorMenuButton, pParentMenu->GetParentButton ());
	}

	CColorButton* pColorButton = DYNAMIC_DOWNCAST (CColorButton, pButton);
	if (pColorButton == NULL)
	{
		ASSERT (FALSE);
	}
	else if (pColorButton->m_bIsLabel)
	{
		return FALSE;
	}
	else if (pColorButton->m_bIsOther)
	{
		SetInCommand ();

		if (pParentMenu != NULL)
		{
			pParentMenu->ShowWindow	(SW_HIDE);
		}

		HWND hwnd = GetSafeHwnd ();

		InvalidateRect (pButton->Rect ());
		UpdateWindow ();

		// Invoke color dialog:
		if (!OpenColorDialog (m_ColorSelected == (COLORREF)-1 ?
			m_ColorAutomatic : m_ColorSelected, color))
		{
			if (!::IsWindow (hwnd))
			{
				return TRUE;
			}

			SetInCommand (FALSE);

			if (m_pParentBtn != NULL || m_pWndPropList != NULL)
			{
				GetParent ()->SendMessage (WM_CLOSE);
			}
			else if (pColorMenuButton != NULL)
			{
				InvokeMenuCommand (0, pColorMenuButton);
			}
			else if (BCGCBProGetTopLevelFrame (this) != NULL)
			{
				BCGCBProGetTopLevelFrame (this)->SetFocus ();
			}

			return TRUE;
		}

		if (!::IsWindow (hwnd))
		{
			return TRUE;
		}

		SetInCommand (FALSE);
	}
	else if (pColorButton->m_bIsAutomatic)
	{
		color = (COLORREF) -1;
	}
	else
	{
		color = pColorButton->m_Color;
	}

	if (pColorMenuButton != NULL)
	{
		pColorMenuButton->SetColor (color);
		InvokeMenuCommand (pColorMenuButton->m_nID, pColorMenuButton);
	}
	else if (m_pParentBtn != NULL)
	{
		m_pParentBtn->UpdateColor (color);
		GetParent ()->SendMessage (WM_CLOSE);
	}
	else if (m_pWndPropList != NULL)
	{
		m_pWndPropList->UpdateColor (color);
		GetParent ()->SendMessage (WM_CLOSE);
	}
	else
	{
		ASSERT (m_nCommandID != 0);

		SetColor (color);

		CObList listButtons;
		if (CBCGPToolBar::GetCommandButtons (m_nCommandID, listButtons) > 0)
		{
			for (POSITION pos = listButtons.GetHeadPosition (); pos != NULL;)
			{
				CBCGPColorMenuButton* pButton = 
					DYNAMIC_DOWNCAST (CBCGPColorMenuButton, listButtons.GetNext (pos));
				if (pButton != NULL)
				{
					pButton->SetColor (color, FALSE);
				}
			}
		}

		CBCGPColorMenuButton::SetColorByCmdID (m_nCommandID, color);
		GetOwner()->SendMessage (WM_COMMAND, m_nCommandID);    // send command

		if (BCGCBProGetTopLevelFrame (this) != NULL)
		{
			BCGCBProGetTopLevelFrame (this)->SetFocus ();
		}
	}

	return TRUE;
}
//*************************************************************************************
BOOL CBCGPColorBar::Create(
			CWnd*		pParentWnd,
			DWORD		dwStyle,
			UINT		nID,
			CPalette*	pPalette/* = NULL*/,
			int			nColumns/* = 0*/,
			int			nRowsDockHorz/* = 0*/,
			int			nColDockVert/* = 0*/)
{

⌨️ 快捷键说明

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