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

📄 ppgcolor.cpp

📁 vc6.0完整版
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"

#ifdef AFXCTL_PAGE_SEG
#pragma code_seg(AFXCTL_PAGE_SEG)
#endif

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW

#define RGB_BUTTON_BLACK    (GetSysColor(COLOR_WINDOWFRAME))
#define RGB_BUTTON_WHITE    (GetSysColor(COLOR_BTNHIGHLIGHT))
#define RGB_BUTTON_LIGHT    (GetSysColor(COLOR_BTNFACE))
#define RGB_BUTTON_DARK     (GetSysColor(COLOR_BTNSHADOW))

//Colors : Standard colors for QcQp
#define BLACK           0
#define WHITE           1
#define RED             2
#define GREEN           3
#define BLUE            4
#define YELLOW          5
#define MAGENTA         6
#define CYAN            7
#define GRAY            8
#define LIGHTGRAY       9
#define DARKRED         10
#define DARKGREEN       11
#define DARKBLUE        12
#define LIGHTBROWN      13
#define DARKMAGENTA     14
#define DARKCYAN        15

/////////////////////////////////////////////////////////////////////////////
// Structure used to store information about the system colors

struct SysColorsInfo
{
	UINT    nStrID;         // The id of the resource describing this system color
	int     nColor;         // The system color index
};

// 3D Drawing helpers
void _AfxDraw3DFrame(CDC *pDC, CRect rcBox, COLORREF colBottomRight,
	COLORREF colTopLeft);
void _AfxDraw3DButtonFrame(CDC *pDC, CRect rcButton, BOOL fFocus);

// Function to initialize the palette
HPALETTE _AfxInitPalette(VOID);
HPALETTE hPal;

// The system colors information
AFX_STATIC_DATA const SysColorsInfo _afxSysColorsList[] =
{
	AFX_IDS_COLOR_DESKTOP, COLOR_BACKGROUND,
	AFX_IDS_COLOR_APPWORKSPACE, COLOR_APPWORKSPACE,
	AFX_IDS_COLOR_WNDBACKGND, COLOR_WINDOW,
	AFX_IDS_COLOR_WNDTEXT, COLOR_WINDOWTEXT,
	AFX_IDS_COLOR_MENUBAR, COLOR_MENU,
	AFX_IDS_COLOR_MENUTEXT, COLOR_MENUTEXT,
	AFX_IDS_COLOR_ACTIVEBAR, COLOR_ACTIVECAPTION,
	AFX_IDS_COLOR_INACTIVEBAR, COLOR_INACTIVECAPTION,
	AFX_IDS_COLOR_ACTIVETEXT, COLOR_CAPTIONTEXT,
	AFX_IDS_COLOR_INACTIVETEXT, COLOR_INACTIVECAPTIONTEXT,
	AFX_IDS_COLOR_ACTIVEBORDER, COLOR_ACTIVEBORDER,
	AFX_IDS_COLOR_INACTIVEBORDER, COLOR_INACTIVEBORDER,
	AFX_IDS_COLOR_WNDFRAME, COLOR_WINDOWFRAME,
	AFX_IDS_COLOR_SCROLLBARS, COLOR_SCROLLBAR,
	AFX_IDS_COLOR_BTNFACE, COLOR_BTNFACE,
	AFX_IDS_COLOR_BTNSHADOW, COLOR_BTNSHADOW,
	AFX_IDS_COLOR_BTNTEXT, COLOR_BTNTEXT,
	AFX_IDS_COLOR_BTNHIGHLIGHT, COLOR_BTNHIGHLIGHT,
	AFX_IDS_COLOR_DISABLEDTEXT, COLOR_GRAYTEXT,
	AFX_IDS_COLOR_HIGHLIGHT, COLOR_HIGHLIGHT,
	AFX_IDS_COLOR_HIGHLIGHTTEXT, COLOR_HIGHLIGHTTEXT,

	// End of list
	NULL, NULL
};

/////////////////////////////////////////////////////////////////////////////
// CColorButton class
// All buttons are initially unselected

CColorButton::CColorButton()
{
	m_fSelected = FALSE;
}

// Set the face color of the button
void CColorButton::SetFaceColor(COLORREF colFace)
{
	m_colFace = colFace;
}

// Return the palette reference for the button
COLORREF CColorButton::colGetFaceColor()
{
	return m_colFace;
}

// Set a color button's selection state
void CColorButton::SetState(BOOL fSelected)
{
	m_fSelected = fSelected;
}

// Static ID shows which color button generated the BN_CLICKED message
UINT CColorButton::idClicked = 0;

// Redraw the color button, and record a change in selection status
void CColorButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC *pDC;
	CPalette *pPal;
	CRect rcButton;
	CBrush *pBrush;

	// Get the palette for the item display context
	pPal = CPalette::FromHandle(hPal);
	VERIFY(pPal);

	// Get the device context from the context
	pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	VERIFY(pDC);

	VERIFY(pDC->SelectPalette(pPal,0));
	pDC->RealizePalette();

	switch(lpDrawItemStruct->itemAction)
	{
		case ODA_SELECT:
			if (CButton::GetState() & 0x0004)
			{
				idClicked = lpDrawItemStruct->CtlID;
				// Redraw done via the BN_CLICKED notification
			}
			break;

		case ODA_DRAWENTIRE:
			rcButton = lpDrawItemStruct->rcItem;
			rcButton.InflateRect(-2, -2);
			if (m_fSelected)
			{
				_AfxDraw3DFrame(pDC, rcButton, RGB_BUTTON_DARK, RGB_BUTTON_WHITE);
				rcButton.InflateRect(-1, -1);
				_AfxDraw3DFrame(pDC, rcButton, RGB_BUTTON_DARK, RGB_BUTTON_WHITE);
				rcButton.InflateRect(-1, -1);
			}

			// Then the button face in the correct color
			pBrush = new CBrush(pDC->GetNearestColor(m_colFace));
			pDC->FillRect(&rcButton, pBrush);
			delete pBrush;

			// Drop through to draw Frame.
		case ODA_FOCUS:
			rcButton = lpDrawItemStruct->rcItem;
			_AfxDraw3DButtonFrame(pDC, rcButton, GetState() & 0x0008);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CColorPropPage implementation

BEGIN_MESSAGE_MAP(CColorPropPage, CStockPropPage)
	//{{AFX_MSG_MAP(CColorPropPage)
	ON_CBN_SELCHANGE(AFX_IDC_COLORPROP, OnSelchangeColorprop)
	ON_BN_CLICKED(AFX_IDC_COLOR_BLACK, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_WHITE, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_RED, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_GREEN, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_BLUE, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_YELLOW, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_MAGENTA, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_CYAN, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_GRAY, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_LIGHTGRAY, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_DARKRED, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_DARKGREEN, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_DARKBLUE, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_LIGHTBROWN, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_DARKMAGENTA, OnSelect)
	ON_BN_CLICKED(AFX_IDC_COLOR_DARKCYAN, OnSelect)
	ON_CBN_SELCHANGE(AFX_IDC_SYSTEMCOLORS, OnSelchangeSystemcolors)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CColorPropPage::CColorPropPage() :
	CStockPropPage(IDD, AFX_IDS_COLOR_PPG_CAPTION)
{
	hPal = _AfxInitPalette();

	//{{AFX_DATA_INIT(CColorPropPage)
	//}}AFX_DATA_INIT
}

BOOL CColorPropPage::OnInitDialog()
{
	UINT iButton;
	for (iButton = 0; iButton < NBUTTONS; iButton++)
	{
		VERIFY(m_Buttons[iButton].SubclassDlgItem(AFX_IDC_COLOR_BLACK + iButton, this));
		m_Buttons[iButton].SetFaceColor(PALETTEINDEX(iButton));
	}

	m_pSelectedButton = NULL;
	CStockPropPage::OnInitDialog();
	FillSysColors();

	OnObjectsChanged();
	IgnoreApply(AFX_IDC_COLORPROP);
	return TRUE;
}

void CColorPropPage::FillSysColors()
{
	const SysColorsInfo* pInfo = &_afxSysColorsList[0];
	while (pInfo->nStrID != NULL)
	{
		CString strColor;

		strColor.LoadString(pInfo->nStrID);
		int nIndex = m_SysColors.AddString(strColor);
		if (nIndex != CB_ERR)
			m_SysColors.SetItemData(nIndex, (DWORD)pInfo->nColor);

		pInfo++;
	}

	// Initially no system color is selected
	m_SysColors.SetCurSel(-1);
}

void CColorPropPage::DoDataExchange(CDataExchange* pDX)
{
	//{{AFX_DATA_MAP(CColorPropPage)
	DDX_Control(pDX, AFX_IDC_SYSTEMCOLORS, m_SysColors);
	DDX_Control(pDX, AFX_IDC_COLORPROP, m_ColorProp);
	//}}AFX_DATA_MAP

	if (pDX->m_bSaveAndValidate)
	{
		unsigned long color = 0;
		BOOL bChanged = TRUE;

		int nIndex = m_SysColors.GetCurSel();

		// did the user pick a particular button?
		// or did they select a system colour from the dropdown?
		if (m_pSelectedButton != NULL)
		{
			PALETTEENTRY pe;
			GetPaletteEntries(hPal, m_pSelectedButton->GetDlgCtrlID() - AFX_IDC_COLOR_BLACK, 1, &pe);
			color = RGB(pe.peRed, pe.peGreen, pe.peBlue);
		}
		else if (nIndex != CB_ERR)
		{
			color = 0x80000000 | m_SysColors.GetItemData(nIndex);
		}
		else
			bChanged = FALSE;

		if (bChanged)
			SetColorProp(pDX, color, m_strPropName);
	}
	else
	{
		unsigned long color;

		GetColorProp(pDX, &color, m_strPropName);
		if (color & 0x80000000)
		{
			// System color

			// Remove any previously selected buttons
			SetButton(NULL);

			int index;
			int cSysColors = m_SysColors.GetCount();
			unsigned long iSysColor = color & 0x000000ff;

			for (index = 0; index < cSysColors; index++)
			{
				if (m_SysColors.GetItemData(index) == iSysColor)
				{
					m_SysColors.SetCurSel(index);
					break;
				}
			}
		}
		else
		{
			// Normal color
			for (int index = 0; index < NBUTTONS; index++)
			{
				PALETTEENTRY pe;

				GetPaletteEntries(hPal, index, 1, &pe);
				if (color == RGB(pe.peRed, pe.peGreen, pe.peBlue))
					SetButton(&m_Buttons[index]);
			}
		}
	}

}

BOOL CColorPropPage::SetColorProp(CDataExchange* /* pDX */,
	unsigned long color, LPCTSTR pszPropName)
{
	USES_CONVERSION;
	COleDispatchDriver PropDispDriver;

	ULONG nObjects;
	LPDISPATCH* ppDisp = GetObjectArray(&nObjects);

	for (ULONG i = 0; i < nObjects; i++)
	{
		DISPID dwDispID;

		// Get the Dispatch ID for the property and if successful set the value
		LPCOLESTR lpOleStr = T2COLE(pszPropName);

⌨️ 快捷键说明

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