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

📄 bcgpcolorbar.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	if (m_colors.GetSize () != NULL)
	{
		return CBCGPPopupMenuBar::Create (pParentWnd, dwStyle, nID);
	}

	m_nNumColumns = nColumns;
	m_nNumColumnsVert = nColDockVert;
	m_nNumRowsHorz = nRowsDockHorz;

	InitColors (pPalette, m_colors);
	return CBCGPPopupMenuBar::Create (pParentWnd, dwStyle, nID);
}
//************************************************************************************
BOOL CBCGPColorBar::CreateControl (
				CWnd*			pParentWnd,
				const CRect&	rect,
				UINT			nID,
				int				nColumns,
				CPalette*		pPalette/* = NULL*/
				)
{
	ASSERT_VALID (pParentWnd);

	EnableLargeIcons (FALSE);

	if (nColumns <= 0)
	{
		const int nColorsCount = (pPalette == NULL) ? 20 : pPalette->GetEntryCount ();
		ASSERT (nColorsCount > 0);

		// Optimal fill
		for (nColumns = nColorsCount; nColumns > 0; nColumns--)
		{
			int nCellSize = (rect.Width () - 2 * m_nHorzMargin) / nColumns;
			if (nCellSize == 0)
			{
				continue;
			}

			int nRows = nColorsCount / nColumns;
			if (nRows * nCellSize > rect.Height () - 2 * m_nVertMargin)
			{
				nColumns++;
				break;
			}
		}

		if (nColumns <= 0)
		{
			nColumns = -1;
		}
	}

	if (!Create (pParentWnd, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP, nID, pPalette, nColumns))
	{
		return FALSE;
	}

	SetBarStyle (
		GetBarStyle () & 
			~(CBRS_GRIPPER | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));

	CRect rectWnd = rect;
	MoveWindow (rectWnd);
	ContextToSize ();

	SetWindowPos (&wndTop, -1, -1, -1, -1,
		SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

	SetOwner (pParentWnd);
	SetCommandID (nID);

	// All commands will be routed via this dialog, not via the parent frame:
	SetRouteCommandsViaFrame (FALSE);
	return TRUE;
}
//*************************************************************************************
int CBCGPColorBar::InitColors (CPalette* pPalette, CArray<COLORREF, COLORREF>& arColors)
{
	int nColorsCount = (pPalette == NULL) ? 20 : pPalette->GetEntryCount ();
	arColors.SetSize (nColorsCount);

	if (pPalette == NULL)
	{
		// Use system palette:
		pPalette = CPalette::FromHandle ((HPALETTE) ::GetStockObject (DEFAULT_PALETTE));
		ASSERT_VALID (pPalette);
	}

	PALETTEENTRY palEntry;
	for (int i = 0; i < nColorsCount; i++)
	{
		pPalette->GetPaletteEntries (i, 1, &palEntry);
		arColors [i] = RGB (palEntry.peRed, palEntry.peGreen, palEntry.peBlue);
	}

	return nColorsCount;
}
//*************************************************************************************
void CBCGPColorBar::Serialize (CArchive& ar)
{
	CBCGPPopupMenuBar::Serialize (ar);

	if (ar.IsLoading ())
	{
		ar >> m_nNumColumns;
		ar >> m_nNumRowsHorz;
		ar >> m_nNumColumnsVert;
		ar >> m_ColorAutomatic;
		ar >> m_strAutoColor;
		ar >> m_strOtherColor;
		ar >> m_strDocColors;
		ar >> m_bIsTearOff;
		ar >> m_nCommandID;
		ar >> m_bStdColorDlg;

		int nColors = 0;
		ar >> nColors;

		m_colors.SetSize (nColors);

		for (int i = 0; i < nColors; i ++)
		{
			COLORREF color;
			ar >> color;
			
			m_colors [i] = color;
		}

		Rebuild ();
		AdjustLocations ();
	}
	else
	{
		ar << m_nNumColumns;
		ar << m_nNumRowsHorz;
		ar << m_nNumColumnsVert;
		ar << m_ColorAutomatic;
		ar << m_strAutoColor;
		ar << m_strOtherColor;
		ar << m_strDocColors;
		ar << m_bIsTearOff;
		ar << m_nCommandID;
		ar << m_bStdColorDlg;

		ar << m_colors.GetSize ();

		for (int i = 0; i < m_colors.GetSize (); i ++)
		{
			ar << m_colors [i];
		}
	}
}
//*************************************************************************************
void CBCGPColorBar::ShowCommandMessageString (UINT /*uiCmdId*/)
{
	GetOwner()->SendMessage (WM_SETMESSAGESTRING,
		m_nCommandID == (UINT) -1 ? AFX_IDS_IDLEMESSAGE : (WPARAM) m_nCommandID);
}
//*************************************************************************************
BOOL CBCGPColorBar::OpenColorDialog (const COLORREF colorDefault, COLORREF& colorRes)
{
	CBCGPColorMenuButton* pColorMenuButton = NULL;

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

	BOOL bResult = FALSE;

	if (m_bStdColorDlg)
	{
		CColorDialog dlg (colorDefault, CC_FULLOPEN | CC_ANYCOLOR);
		if (dlg.DoModal () == IDOK)
		{
			colorRes = dlg.GetColor ();
			bResult = TRUE;
		}
	}
	else
	{
		CBCGPColorDialog dlg (colorDefault);
		if (dlg.DoModal () == IDOK)
		{
			colorRes = dlg.GetColor ();
			bResult = TRUE;
		}
	}

	return bResult;
}
//*************************************************************************************
void CBCGPColorBar::EnableAutomaticButton (LPCTSTR lpszLabel, COLORREF colorAutomatic, BOOL bEnable)
{
	m_ColorAutomatic = colorAutomatic;
	m_strAutoColor = (!bEnable || lpszLabel == NULL) ? _T("") : lpszLabel;

	Rebuild ();
	AdjustLayout ();
}
//*************************************************************************************
void CBCGPColorBar::EnableOtherButton (LPCTSTR lpszLabel, BOOL bAltColorDlg, BOOL bEnable)
{
	m_bStdColorDlg = !bAltColorDlg;
	m_strOtherColor = (!bEnable || lpszLabel == NULL) ? _T("") : lpszLabel;

	Rebuild ();
	AdjustLayout ();
}
//*************************************************************************************
void CBCGPColorBar::SetColor (COLORREF color)
{
	if (m_ColorSelected == color)
	{
		return;
	}

	m_ColorSelected = color;

	if (GetSafeHwnd () == NULL)
	{
		return;
	}

	BOOL bIsOtherColor = !(m_ColorAutomatic != (UINT)-1 && m_ColorSelected == (COLORREF)-1);
	BOOL bWasOtherColor = FALSE;

	m_iHighlighted = -1;
	int iButton = 0;

	for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL; iButton++)
	{
		CRect rectButton;

		CBCGPToolbarButton* pButton = (CBCGPToolbarButton*) m_Buttons.GetNext (pos);
		if (pButton->m_nStyle & TBBS_SEPARATOR)
		{
			continue;
		}

		CColorButton* pColorButton = DYNAMIC_DOWNCAST (CColorButton, pButton);
		if (pColorButton == NULL)
		{
			continue;
		}

		ASSERT_VALID (pColorButton);

		if (pColorButton->m_bIsOther || pColorButton->m_bIsLabel)
		{
			continue;
		}

		if (pColorButton->m_bHighlight)
		{
			pColorButton->m_bHighlight = FALSE;
			InvalidateRect (pColorButton->Rect ());
		}

		if (pColorButton->m_bIsAutomatic && color == (COLORREF)-1)
		{
			pColorButton->m_bHighlight = TRUE;
			m_iHighlighted = iButton;
			InvalidateRect (pColorButton->Rect ());
		}
		else if (pColorButton->m_Color == color)
		{
			pColorButton->m_bHighlight = TRUE;
			m_iHighlighted = iButton;
			InvalidateRect (pColorButton->Rect ());
			bIsOtherColor = FALSE;
		}

		if (pColorButton->m_bIsOtherColor)
		{
			pColorButton->m_Color = m_ColorSelected;
			pColorButton->m_bHighlight = TRUE;

			InvalidateRect (pColorButton->Rect ());
			bWasOtherColor = !(pColorButton->Rect ().IsRectEmpty ());
		}
	}

	if (bWasOtherColor != bIsOtherColor)
	{
		AdjustLocations ();
		Invalidate ();
	}

	UpdateWindow ();
}
//***************************************************************************************
void CBCGPColorBar::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (!IsCustomizeMode () || m_bInternal)
	{
		CBCGPToolBar::OnMouseMove(nFlags, point);
	}
}
//***************************************************************************************
void CBCGPColorBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (IsCustomizeMode () && !m_bInternal)
	{
		return;
	}

	if (HitTest (point) == -1)
	{
		CBCGPToolBar::OnLButtonDown(nFlags, point);
	}
}
//***************************************************************************************
void CBCGPColorBar::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if (!IsCustomizeMode () || m_bInternal)
	{
		int iHit = HitTest (point);
		if (iHit >= 0)
		{
			m_iButtonCapture = iHit;
		}

		CBCGPToolBar::OnLButtonUp(nFlags, point);
	}
}
//***************************************************************************************
void CBCGPColorBar::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	if (!IsCustomizeMode () || m_bInternal)
	{
		CBCGPToolBar::OnLButtonDblClk(nFlags, point);
	}
}
//***************************************************************************************
BOOL CBCGPColorBar::PreTranslateMessage(MSG* pMsg)
{
	if ((m_pParentBtn != NULL || m_pWndPropList != NULL) && !m_bInCommand)
	{
		switch (pMsg->message)
		{
		case WM_LBUTTONDOWN:
		case WM_RBUTTONDOWN:
		case WM_MBUTTONDOWN:
			{
				CRect rect;
				GetClientRect (rect);

				CPoint pt (BCG_GET_X_LPARAM (pMsg->lParam), BCG_GET_Y_LPARAM (pMsg->lParam));
				if (!rect.PtInRect (pt))
				{
					GetParent ()->SendMessage (WM_CLOSE);
					return TRUE;
				}
			}
			break;

		case WM_SYSKEYDOWN:
		case WM_CONTEXTMENU:
			GetParent ()->SendMessage (WM_CLOSE);
			return TRUE;

		case WM_KEYDOWN:
			if (pMsg->wParam == VK_ESCAPE)
			{
				GetParent ()->SendMessage (WM_CLOSE);
				return TRUE;
			}
		}
	}

	return CBCGPPopupMenuBar::PreTranslateMessage(pMsg);
}
//*************************************************************************************
void CBCGPColorBar::OnDestroy() 
{
	if (m_pParentBtn != NULL)
	{
		m_pParentBtn->m_pPopup = NULL;
		m_pParentBtn->SetFocus ();
	}
	else if (m_pWndPropList != NULL)
	{
		m_pWndPropList->CloseColorPopup ();
		m_pWndPropList->SetFocus ();
	}

	CBCGPPopupMenuBar::OnDestroy();
}
//****************************************************************************************
BOOL CBCGPColorBar::OnKey(UINT nChar)
{
	POSITION posSel = 
		(m_iHighlighted < 0) ? NULL : m_Buttons.FindIndex (m_iHighlighted);
	CBCGPToolbarButton* pSelButton = 
		(posSel == NULL) ? NULL : (CBCGPToolbarButton*) m_Buttons.GetAt (posSel);

	switch (nChar)
	{
	case VK_RETURN:
		if (pSelButton != NULL)
		{
			GetOwner()->SendMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
			OnSendCommand (pSelButton);
			return TRUE;
		}
		break;
	}

	return CBCGPPopupMenuBar::OnKey (nChar);
}
//****************************************************************************************
afx_msg LRESULT CBCGPColorBar::OnMouseLeave(WPARAM wp,LPARAM lp)
{
	if (m_pParentBtn == NULL && m_pWndPropList == NULL)
	{
		return CBCGPToolBar::OnMouseLeave (wp, lp);
	}

	if (m_hookMouseHelp != NULL || 
		(m_bMenuMode && !IsCustomizeMode () && GetDroppedDownMenu () != NULL))
	{
		return 0;
	}

	m_bTracked = FALSE;
	m_ptLastMouse = CPoint (-1, -1);

	if (m_iHighlighted >= 0)
	{
		int iButton = m_iHighlighted;
		m_iHighlighted = -1;

		OnChangeHot (m_iHighlighted);

		InvalidateButton (iButton);
		UpdateWindow(); // immediate feedback

		GetOwner()->SendMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
	}

	return 0;
}
//**************************************************************************************
BOOL CBCGPColorBar::CreatePalette (const CArray<COLORREF, COLORREF>& arColors,
								CPalette& palette)
{
	if (palette.GetSafeHandle () != NULL)
	{
		::DeleteObject (palette.Detach ());
		ASSERT (palette.GetSafeHandle () == NULL);
	}

	if (globalData.m_nBitsPerPixel != 8)
	{
		return FALSE;
	}

	#define MAX_COLOURS 100
	int nNumColours = arColors.GetSize ();
	if (nNumColours == 0)
	{
		ASSERT (FALSE);
		return FALSE;
	}

	ASSERT (nNumColours <= MAX_COLOURS);
	if (nNumColours > MAX_COLOURS)
	{
		nNumColours = MAX_COLOURS;
	}

	// Create the palette
	struct 
	{
		LOGPALETTE    LogPalette;
		PALETTEENTRY  PalEntry [MAX_COLOURS];
	}
	pal;

	LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
	pLogPalette->palVersion    = 0x300;
	pLogPalette->palNumEntries = (WORD) nNumColours; 

	for (int i = 0; i < nNumColours; i++)
	{
		pLogPalette->palPalEntry[i].peRed   = GetRValue (arColors[i]);
		pLogPalette->palPalEntry[i].peGreen = GetGValue (arColors[i]);
		pLogPalette->palPalEntry[i].peBlue  = GetBValue (arColors[i]);
		pLogPalette->palPalEntry[i].peFlags = 0;
	}

	palette.CreatePalette (pLogPalette);
	return TRUE;
}
//**************************************************************************************
CPalette* CBCGPColorBar::SelectPalette (CDC* pDC)
{
	ASSERT_VALID (pDC);

	if (globalData.m_nBitsPerPixel != 8)
	{
		if (m_Palette.GetSafeHandle () != NULL)
		{
			::DeleteObject (m_Palette.Detach ());
		}

		return NULL;
	}

	CPalette* pOldPalette = NULL;

	if (m_pParentBtn != NULL && m_pParentBtn->m_pPalette != NULL)
	{
		pOldPalette = pDC->SelectPalette (m_pParentBtn->m_pPalette, FALSE);
	}
	else
	{
		if (m_Palette.GetSafeHandle () == NULL)
		{
			//----------------------------------------
			// Palette not created yet; create it now
			//----------------------------------------
			CreatePalette (m_colors, m_Palette);
		}

		pOldPalette = pDC->SelectPalette (&m_Palette, FALSE);
	}

	ASSERT (pOldPalette != NULL);
	pDC->RealizePalette ();

	return pOldPalette;
}
//****************************************************************************************
void CBCGPColorBar::SetVertMargin (int nVertMargin)
{
	ASSERT_VALID (this);

	m_nVertMargin = nVertMargin;
	AdjustLayout ();
}
//*****************************************************************************************
void CBCGPColorBar::SetHorzMargin (int nHorzMargin)
{
	ASSERT_VALID (this);

	m_nHorzMargin = nHorzMargin;
	AdjustLayout ();
}

#endif // BCG_NO_COLOR

⌨️ 快捷键说明

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