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

📄 sccolorbox.cpp

📁 Source code for EMFexplorer 1.0
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		return -1;

	m_crSelColor = crColor;
	SetCurSel((m_bCustomAllowed)? m_pLogPalette->palNumEntries : -1);

	return m_crSelColor;
}

void CSCColorBox::OnSelendok() 
{
	COLORREF crNewColor = SCSelectNewColor();

	if (crNewColor == m_crSelColor)
		return;
	m_crSelColor = crNewColor;

	// Notify parent
	CWnd *pParent = GetParent();
	
	ASSERT(pParent);
	NMHDR nmhdr;
	nmhdr.code = CBN_EDITCHANGE;
	nmhdr.hwndFrom = m_hWnd;
	nmhdr.idFrom = GetDlgCtrlID();
	pParent->SendMessage(WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
}

COLORREF CSCColorBox::SCSelectNewColor()
{
	COLORREF crNewColor;
	int iSelIdx = GetCurSel();
	if (m_bCustomAllowed && iSelIdx == m_pLogPalette->palNumEntries)
	{
		// Avoid opening the dialog twice
		if (m_bDlgOpened)
		{// the combox is just repainting itself after the previous open: do nothing
			m_bDlgOpened = FALSE;
			return m_crSelColor;		
		}

		m_bDlgOpened = TRUE;	// mark that user attempted to choose from dialog
		CColorDialog dlgColor;//(0, CC_ANYCOLOR|CC_RGBINIT);
		if (IDOK != dlgColor.DoModal())
			return m_crSelColor;

		crNewColor = dlgColor.GetColor();
	} else
	{
		PALETTEENTRY* pEntry = &m_pLogPalette->palPalEntry[iSelIdx];
		crNewColor = RGB(pEntry->peRed, pEntry->peGreen, pEntry->peBlue);
	}
	return crNewColor;
}

/////////////////////////////////////////////////////////////////////////////////////////
// CSCColorBox statics
//

LOGPALETTE *CSCColorBox::SCCreatePaletteEntries()
{
	LOGPALETTE *pLgPalette = NULL;

	HDC hDC = ::GetDC(NULL);
	if (!(RC_PALETTE & GetDeviceCaps(hDC,RASTERCAPS))) 
		pLgPalette = SCCreateVGAPaletteEntries();
	else
	{
		// Find out the number of colors on this device.   
		UINT nColors = (1 << (GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES)));  
		
		// allocate memory for palette entries
		PALETTEENTRY *pPaletteEntries = new PALETTEENTRY[nColors];
		
		// get the system palette 
		if (pPaletteEntries && GetSystemPaletteEntries(hDC, 0, nColors, pPaletteEntries))
		{
			// build the logical palette now
			pLgPalette = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + nColors*sizeof(PALETTEENTRY)];
			if (pLgPalette)
			{
				pLgPalette->palVersion = 0x300;
				pLgPalette->palNumEntries = nColors;
				for (UINT i=0;i<nColors;i++)
					pLgPalette->palPalEntry[i] = pPaletteEntries[i];
			}
		}
		delete [] pPaletteEntries;
	}
	::ReleaseDC(NULL, hDC);

	return pLgPalette;
}

PALETTEENTRY const CSCColorBox::s_VgaPalette[] =
{     
	{ 0,   0,   0,    0 },
	{ 0x80,0,   0,    0 }, 
    { 0,   0x80,0,    0 }, 
    { 0x80,0x80,0,    0 }, 
    { 0,   0,   0x80, 0 }, 
    { 0x80,0,   0x80, 0 },  
	{ 0,   0x80,0x80, 0 },
	{ 0x80,0x80,0x80, 0 },
	{ 0xC0,0xC0,0xC0, 0 }, 
    { 0xFF,0,   0,    0 }, 
    { 0,   0xFF,0,    0 }, 
    { 0xFF,0xFF,0,    0 }, 
    { 0,   0,   0xFF, 0 }, 
    { 0xFF,0,   0xFF, 0 }, 
    { 0,   0xFF,0xFF, 0 }, 
    { 0xFF,0xFF,0xFF, 0 } 
}; 

LOGPALETTE *CSCColorBox::SCCreateVGAPaletteEntries()
{
	UINT iNumEntries = sizeof(s_VgaPalette)/sizeof(PALETTEENTRY);
	LOGPALETTE *pLgPalette = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + iNumEntries*sizeof(PALETTEENTRY)];
	if (pLgPalette)
	{
		pLgPalette->palVersion = 0x300;
		pLgPalette->palNumEntries = iNumEntries;
		for (UINT i=0;i<iNumEntries;i++)
			pLgPalette->palPalEntry[i] = s_VgaPalette[i];
	}

	return pLgPalette;
}

/////////////////////////////////////////////////////////////////////////////
// CSCSysColorBox
//

typedef struct tag_SCSysColorsInfo
{
	DWORD     nColorIdx;
	LPCTSTR  lpszColorName;
} SCSysColorsInfo, *PSCSysColorsInfo;

SCSysColorsInfo s_SysColorsList[] =
{
	COLOR_3DDKSHADOW,				_T("Dark shadow of 3D elements"),
	COLOR_3DFACE,					_T("Face color of 3D elements"),
	COLOR_3DHIGHLIGHT,				_T("Highlight color of 3D elements"),
	COLOR_3DLIGHT,					_T("Light color of 3D elements"),
	COLOR_3DSHADOW,					_T("Shadow color of 3D elements"),
	COLOR_ACTIVEBORDER,				_T("Active window border"),
	COLOR_ACTIVECAPTION,			_T("Active window title bar"),
	COLOR_APPWORKSPACE,				_T("Background color of MDI applications"),
	COLOR_BACKGROUND,				_T("Desktop background"),
	COLOR_BTNTEXT,					_T("Text on push buttons"),
	COLOR_CAPTIONTEXT,				_T("Text in caption and scroll bar arrow box"),
	COLOR_GRADIENTACTIVECAPTION,	_T("Right side in active window's title bar"),
	COLOR_GRADIENTINACTIVECAPTION,	_T("Right side in inactive window's title bar"),
	COLOR_GRAYTEXT,					_T("Grayed (disabled) text"),
	COLOR_HIGHLIGHT,				_T("Item selected in a control"),
	COLOR_HIGHLIGHTTEXT,			_T("Text of selected items in a control"),
	COLOR_HOTLIGHT,					_T("Hot-tracked item"),
	COLOR_INACTIVEBORDER,			_T("Inactive window border"),
	COLOR_INACTIVECAPTION,			_T("Inactive window caption"),
	COLOR_INACTIVECAPTIONTEXT,		_T("Text of inactive caption"),
	COLOR_INFOBK,					_T("Background of tooltips"),
	COLOR_INFOTEXT,					_T("Text color of tooltips"),
	COLOR_MENU,						_T("Menu background"),
	COLOR_MENUTEXT,					_T("Text in menus"),
	COLOR_SCROLLBAR,				_T("Scroll bar gray area"),
	COLOR_WINDOW,					_T("Window background"),
	COLOR_WINDOWFRAME,				_T("Window frame"),
	COLOR_WINDOWTEXT,				_T("Text in windows")
};


CSCSysColorBox::CSCSysColorBox()
{
	m_bCustomAllowed = FALSE;
	m_crSelColor = 0x80000000 | COLOR_WINDOW;
}

CSCSysColorBox::~CSCSysColorBox()
{
}

BEGIN_MESSAGE_MAP(CSCSysColorBox, CSCColorBox)
	//{{AFX_MSG_MAP(CSCSysColorBox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


void CSCSysColorBox::InitColorsComboBox(COLORREF crDefaultColor)
{
	CSCColorBox::InitColorsComboBox(crDefaultColor);
	m_crSelColor = 0x80000000 | (crDefaultColor & 0x000000FF);

	// Enlarge combo
	CDC* pDC = GetDC();
	if (!pDC)
		return;

	int iWdt = GetDroppedWidth();
	CFont* pFont = GetFont();
	if (pFont)
		pFont = (CFont*)pDC->SelectObject(pFont);
	else
		pFont = (CFont*)pDC->SelectStockObject(DEFAULT_GUI_FONT);

	UINT iNumEntries = sizeof(s_SysColorsList)/sizeof(SCSysColorsInfo);
	for (UINT i=0; (i<iNumEntries); i++)
	{
		CSize szSize = pDC->GetTextExtent(s_SysColorsList[i].lpszColorName,
									_tcslen(s_SysColorsList[i].lpszColorName));
		
		if (szSize.cx>iWdt)
			iWdt = szSize.cx;
	}
	pDC->SelectObject(pFont);
	ReleaseDC(pDC);

	if (CB_ERR!=iWdt)
		SetDroppedWidth(iWdt);
}


// Warning: must use CBS_OWNERDRAWVARIABLE for this to work
void CSCSysColorBox::MeasureItem(LPMEASUREITEMSTRUCT lpmis) 
{
	// Code to determine the size of specified item
	CSCColorBox::MeasureItem(lpmis);
	if (lpmis->itemHeight<SC_COLORITM_HEIGHT_SYS)
		lpmis->itemHeight = SC_COLORITM_HEIGHT_SYS;
}

int CSCSysColorBox::CompareItem(LPCOMPAREITEMSTRUCT lpcis) 
{
// MFC bug: it can call us for the CB_ERR item
//		if (lpcis->itemID1<0)
//			return -1;
//		if (lpcis->itemID2<0)
//			return 1;
//	

	DWORD dwNumEntries = sizeof(s_SysColorsList)/sizeof(SCSysColorsInfo);
	DWORD itemIdx1 = lpcis->itemData1;
	DWORD itemIdx2 = lpcis->itemData2;

	if (itemIdx1 < 0 ||
		itemIdx1 >= dwNumEntries ||
		itemIdx2 < 0 ||
		itemIdx2 >= dwNumEntries)
		return 0;       // err

	return _tcsicmp(s_SysColorsList[itemIdx1].lpszColorName,
					s_SysColorsList[itemIdx2].lpszColorName);
}

void CSCSysColorBox::DrawEntireItem( 
        LPDRAWITEMSTRUCT        lpdis, 
        INT                     inflate) 
{ 
	RECT    rc; 
	HBRUSH  hbr; 

	if (!lpdis || !m_pLogPalette)
		return;
	
	/* Resize rectangle to leave space for frames */ 
	CopyRect((LPRECT)&rc, (LPRECT)&lpdis->rcItem); 
	InflateRect((LPRECT)&rc, inflate, inflate); 

	// according to the item index, create the corresponding brush
	// to draw one palette entry
	// get the entry palette corresponding to the index item
	int itemIdx = lpdis->itemData;
	if ((itemIdx>=0) && (itemIdx<m_pLogPalette->palNumEntries))
	{
		COLORREF color = RGB(m_pLogPalette->palPalEntry[itemIdx].peRed,
			m_pLogPalette->palPalEntry[itemIdx].peGreen,
			m_pLogPalette->palPalEntry[itemIdx].peBlue);
		HDC hDC = lpdis->hDC;

		// background
		hbr = (HBRUSH)CreateSolidBrush(GetSysColor(COLOR_WINDOW)); 
		FillRect(hDC, (LPRECT)&rc, hbr);
		DeleteObject(hbr);

		// create brush and fill the rectangle
		InflateRect((LPRECT)&rc, -2, -2);
		rc.right = rc.left + (rc.bottom - rc.top);
		hbr = (HBRUSH)CreateSolidBrush(color);
		HBRUSH hOldbr = (HBRUSH)SelectObject(hDC, hbr);
		Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
		SelectObject(hDC, hOldbr);
		DeleteObject(hbr);

		// Draw text
		SetBkMode(hDC, TRANSPARENT);
		::SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
		// TODO: remove font's internal leading instead of hard-coded 2
		TextOut(hDC, rc.right + 4, rc.top - 2,
			s_SysColorsList[itemIdx].lpszColorName, _tcslen(s_SysColorsList[itemIdx].lpszColorName));
		
		/* Draw or erase appropriate frames */ 
		HandleSelectionState(lpdis, inflate + 4); 
		HandleFocusState(lpdis, inflate + 2); 
		if(lpdis->itemState & ODS_COMBOBOXEDIT)
		{
			m_crSelColor = 0x80000000 | s_SysColorsList[itemIdx].nColorIdx;
		}
	}
}

COLORREF CSCSysColorBox::SCSelectNewColor()
{
	int iSelIdx = GetCurSel();
	if (CB_ERR != iSelIdx)
	{
		iSelIdx = GetItemData(iSelIdx);
		if (iSelIdx>=0 && iSelIdx < m_pLogPalette->palNumEntries)
		{
			return 0x800000 | s_SysColorsList[iSelIdx].nColorIdx;
		}
	}

	return m_crSelColor;
}

COLORREF CSCSysColorBox::SCSetCurSelColor(COLORREF crColor)
{
	// get the corresponding color
	if (!m_pLogPalette)
		return -1;

	DWORD nColorIndex = (DWORD)(crColor & 0x000000FF);
	m_crSelColor = 0x80000000 | nColorIndex;

	DWORD dwNumEntries = sizeof(s_SysColorsList)/sizeof(SCSysColorsInfo);
	for (DWORD i=0; (i<dwNumEntries); i++)
	{
		DWORD nPalIdx = GetItemData(i);
		if (nPalIdx>=0 && nPalIdx<dwNumEntries &&
			s_SysColorsList[nPalIdx].nColorIdx == nColorIndex)
		{
			SetCurSel(i);
			return m_crSelColor;
		}
	}

	SetCurSel(CB_ERR);
	return m_crSelColor;
}


// static
LOGPALETTE *CSCSysColorBox::SCCreateSysPaletteEntries()
{
	UINT iNumEntries = sizeof(s_SysColorsList)/sizeof(SCSysColorsInfo);
	LOGPALETTE *pLgPalette = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + iNumEntries*sizeof(PALETTEENTRY)];
	if (pLgPalette)
	{
		pLgPalette->palVersion = 0x300;
		pLgPalette->palNumEntries = iNumEntries;
		for (UINT i=0;i<iNumEntries;i++)
		{
			COLORREF color = GetSysColor(s_SysColorsList[i].nColorIdx);
			PALETTEENTRY* ppalentry = &pLgPalette->palPalEntry[i];
			ppalentry->peFlags = 0;
			ppalentry->peRed = GetRValue(color);
			ppalentry->peGreen = GetGValue(color);
			ppalentry->peBlue = GetBValue(color);
			
		}
	}

	return pLgPalette;
}


⌨️ 快捷键说明

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