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

📄 fodroppalettecontrol.cpp

📁 visual c++ 实例编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copyright UCanCode Software Technology Inc, All Rights Reserved
// You can contact us.
// Support@UCanCode.net
// http://www.ucancode.net
/********************************************************************/
// FODropPaletteControl.cpp: implementation of the CFODropPaletteControl class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FODropPaletteControl.h"
#include "afxpriv.h"
#include <math.h>

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

#define NUM_LEVELS			7
#define TAN30				0.57735026918962F
#define YOFFSET				(1.5F * TAN30)
#define PI					3.14159265358979

static const float cfxFOOffset[] = { -0.5, -1.0, -0.5, 0.5, 1.0, 0.5 };
static const float cfyFOOffset[] = { YOFFSET, 0.0, -YOFFSET, -YOFFSET, 0.0, YOFFSET };

static const COLORREF crWhite = RGB (255, 255, 255);
static const COLORREF crBlack = RGB (0, 0, 0);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CFODropPaletteControl, CObject, 0)
CFODropPaletteControl::CFODropPaletteControl()
{
	m_rcPosition = CRect(0,0,0,0);
	m_pParent = NULL;
	m_bPopup = FALSE;
	m_bCustomSelect = FALSE;
	strCustom = "Custom...";
	m_nRows = 16;
	m_nCols = 9;
	bHasCustomBar = TRUE;
	nMainBorderSize = 5;
	nCellBorderSize = 1;
	nBorderType = BORDER_SINGLE_OUT;
	m_crCurColor = RGB(0,0,0);
	ConvertRGBToHSL (m_crCurColor, &m_dblHue, &m_dblSat, &m_dblLum);
	rcSep = CRect(0,0,0,0);
	CFont newFont;
	HFONT hFont = NULL;
#ifdef WIN32
	hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
#endif
	if (hFont == NULL)
		hFont = (HFONT)GetStockObject(ANSI_VAR_FONT);
	newFont.Attach(hFont);
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));
	newFont.GetLogFont(&lf);
	fText.CreateFontIndirect(&lf);
}

CFODropPaletteControl::~CFODropPaletteControl()
{
	fText.DeleteObject();
}
void CFODropPaletteControl::Serialize(CArchive& ar)
{
	if(ar.IsStoring())
	{
	}
	else
	{
	}
}
/////////////////////////////////////////////////////////////////////////////
// CPrintBaseJob serialization
CFile* CFODropPaletteControl::GetFile(LPCTSTR lpszFileName, UINT nOpenFlags,
									   CFileException* pError)
{
	CMirrorFile* pFile = new CMirrorFile;
	ASSERT(pFile != NULL);
	if (!pFile->Open(lpszFileName, nOpenFlags, pError))
	{
		delete pFile;
		pFile = NULL;
	}
	return pFile;
}
void CFODropPaletteControl::ReleaseFile(CFile* pFile, BOOL bAbort)
{
	ASSERT_KINDOF(CFile, pFile);
	if (bAbort)
		pFile->Abort(); // will not throw an exception
	else
		pFile->Close();
	delete pFile;
}
BOOL CFODropPaletteControl::OpenDocument(LPCTSTR lpszPathName)
{
	
	CFileException fe;
	CFile* pFile = GetFile(lpszPathName,
		CFile::modeRead|CFile::shareDenyWrite, &fe);
	if (pFile == NULL)
	{
		return FALSE;
	}
	
	CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
	loadArchive.m_bForceFlat = FALSE;
	TRY
	{
		CWaitCursor wait;
		if (pFile->GetLength() != 0)
			Serialize(loadArchive);     // load me
		loadArchive.Close();
		ReleaseFile(pFile, FALSE);
	}
	CATCH_ALL(e)
	{
		ReleaseFile(pFile, TRUE);
		
		return FALSE;
	}
	END_CATCH_ALL
		
		
		return TRUE;
}
BOOL CFODropPaletteControl::SaveDocument(LPCTSTR lpszPathName)
{
	
	CFileException fe;
	CFile* pFile = NULL;
	pFile = GetFile(lpszPathName, CFile::modeCreate |
		CFile::modeReadWrite | CFile::shareExclusive, &fe);
	
	if (pFile == NULL)
	{
		
		return FALSE;
	}
	
	CArchive saveArchive(pFile, CArchive::store | CArchive::bNoFlushOnDelete);
	saveArchive.m_bForceFlat = FALSE;
	TRY
	{
		CWaitCursor wait;
		Serialize(saveArchive);     // save me
		saveArchive.Close();
		ReleaseFile(pFile, FALSE);
	}
	CATCH_ALL(e)
	{
		ReleaseFile(pFile, TRUE);
		
		return FALSE;
	}
	END_CATCH_ALL
		
		
		return TRUE;        // success
}

void CFODropPaletteControl::InitColorPalette()
{
	ComputeColors();
	CRect rcPos = GetRect();
	rcPos.DeflateRect(nMainBorderSize,nMainBorderSize,nMainBorderSize,nMainBorderSize);
	rcCustom.SetRectEmpty();
	if(bHasCustomBar)
	{
		rcSep = rcPos;
		rcSep.top = rcPos.bottom -22;
		rcSep.bottom = rcSep.top +4;

		rcCustom = rcPos;
		rcCustom.top = rcPos.bottom -18;
		rcCustomSave = rcCustom;
		rcPos.bottom -= 24;
	}
	AdjustCells(rcPos);
}

void CFODropPaletteControl::InvalRect(CRect rcPos)
{
	rcPos.InflateRect(2,2,2,2);
	m_pParent->InvalidateRect(rcPos);
}

void CFODropPaletteControl::Invalidate(BOOL bRedraw /* = FALSE */)
{
	m_pParent->Invalidate(bRedraw);
}

void CFODropPaletteControl::SetHLS(double hue, double luminance, double saturation)
{
	if (hue != -1)
		m_dblHue			= hue;
	
	if (saturation != -1)
		m_dblSat	= saturation;
	
	if (luminance != -1)
		m_dblLum	= luminance;
	
	COLORREF crColor = GetRGBFromHLSExtend(m_dblHue, m_dblSat, m_dblLum);
	SetRGB(crColor);
}

void CFODropPaletteControl::GetHLS(double *hue, double *luminance, double *saturation)
{
	ConvertRGBToHSL(m_crCurColor, &m_dblHue, &m_dblSat, &m_dblLum);

	*hue		= m_dblHue;
	*luminance	= m_dblLum;
	*saturation = m_dblSat;
}

void CFODropPaletteControl::SetRGB (COLORREF ref)
{
	ConvertRGBToHSL (ref, &m_dblHue, &m_dblSat, &m_dblLum);
	CFOColorCellObj* pOldSelCell = NULL;
	CFOColorCellObj* pSelCell = NULL;
	BOOL bCurSel = FALSE;
	BOOL bOldSel = FALSE;
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		if (m_crCells[i].GetCellColor() == m_crCurColor)
		{
			bOldSel = TRUE;
			pOldSelCell = &m_crCells[i];
			if(bOldSel && bCurSel)
			{
				break;
			}
		}
		if (m_crCells[i].GetCellColor() == ref)
		{
			bCurSel = TRUE;
			pSelCell = &m_crCells[i];
			if(bOldSel && bCurSel)
			{
				break;
			}
		}
	}
	
	if(pSelCell != NULL)
	{
		m_crCurColor = pSelCell->GetCellColor();
		InvalRect(pSelCell->GetRect());
	}
	else
	{
		m_crCurColor = ref;
		InvalRect(rcCustomSave);
	}

	if(pOldSelCell != NULL)
	{
		InvalRect(pOldSelCell->GetRect());
	}
}

void CFODropPaletteControl::UpdateAll()
{
	InitColorPalette();
}

void CFODropPaletteControl::Create(CWnd *pWnd,COLORREF crDefault,CRect &rcPos,BOOL bPopup)
{
	m_rcPosition = rcPos;
	m_pParent = pWnd;
	m_crCurColor = crDefault;
	m_bPopup = bPopup;
	InitColorPalette();
}

CRect CFODropPaletteControl::GetOldColorRect()
{
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		if (m_crCells[i].GetCellColor() == m_crCurColor)
		{
			return m_crCells[i].GetRect();
		}
	}
	return CRect(0,0,0,0);
}

CFOColorCellObj* CFODropPaletteControl::HitTest(CPoint point)
{
	CFOColorCellObj* pSelCell = NULL;
	CFOColorCellObj* pHitCell = NULL;
	BOOL bHit = FALSE;
	BOOL bSel = FALSE;
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		if (m_crCells[i].GetCellColor() == m_crCurColor)
		{
			bSel = TRUE;
			pSelCell = &m_crCells[i];
			if(bSel && bHit)
			{
				break;
			}
		}
		if (m_crCells[i].HitTest(point))
		{
			bHit = TRUE;
			pHitCell = &m_crCells[i];
			if(bSel && bHit)
			{
				break;
			}
		}
	}
	
	if(pHitCell != NULL)
	{
		m_crCurColor = pHitCell->GetCellColor();
		InvalRect(pHitCell->GetRect());
	}

	if(pSelCell != NULL)
	{
		InvalRect(pSelCell->GetRect());
	}
	return pHitCell;
}
// Draw a rectangle
void CFODropPaletteControl::DrawRect(CDC *pDC, RECT rect)
{
	pDC->MoveTo(rect.left,rect.top);
	pDC->LineTo(rect.right-1,rect.top);
	pDC->LineTo(rect.right-1,rect.bottom-1);
	pDC->LineTo(rect.left,rect.bottom-1);
	pDC->LineTo(rect.left,rect.top);
}

void CFODropPaletteControl::DrawRect(CDC *pDC, RECT rect, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = pDC->SelectObject(&pen);
	
	DrawRect(pDC, rect);
	
	pDC->SelectObject(oldPen);
}

void CFODropPaletteControl::DrawRect(CDC *pDC, int x, int y, int w, int h)
{
	pDC->MoveTo(x,y);
	pDC->LineTo(x+w,y);
	pDC->LineTo(x+w,y+h);
	pDC->LineTo(x,y+h);
	pDC->LineTo(x,y);
}

void CFODropPaletteControl::DrawRect(CDC *pDC, int x, int y, int w, int h, COLORREF color)
{	
	color;
	DrawRect(pDC, x, y, w, h);
}

// Draw a circle
void CFODropPaletteControl::DrawCircle(CDC *pDC, int x, int y, int radius)
{
	pDC->Arc(x-radius,y-radius,x+radius,y+radius,x,y-radius,x,y-radius);
}

void CFODropPaletteControl::DrawCircle(CDC *pDC, int x, int y, int radius, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = pDC->SelectObject(&pen);
	
	DrawCircle(pDC, x, y, radius);
	
	pDC->SelectObject(oldPen);
}

// Draw a line
void CFODropPaletteControl::DrawLine(CDC *pDC, CPoint& start, CPoint& end)
{
	pDC->MoveTo(start);
	pDC->LineTo(end);
}

void CFODropPaletteControl::DrawOtherBorder(CDC *pDC,CRect &rcPos,UINT nType)
{
	CRect rcImage;
	rcImage = rcPos;
	if(TRUE)
	{
		switch(nType)
		{
		case BORDER_NONE:
			{
			}
			break;
		case BORDER_SINGLE_IN:
			{
				rcPos.InflateRect(2,2,2,2);
				pDC->Draw3dRect(rcImage,  RGB(0,0,0),RGB(255,255,255));
				rcImage.InflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage,  gfxData.m_crBtnShadow,gfxData.m_crBtnFace);
			}
			break;
		case BORDER_SINGLE_OUT:
			{
				pDC->Draw3dRect(rcImage,  RGB(255,255,255),RGB(0,0,0));
				rcImage.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage, gfxData.m_crBtnFace,gfxData.m_crBtnShadow);
			}
			break;
			
		case BORDER_DOUBLE_IN:
			{
				pDC->Draw3dRect(rcImage,  gfxData.m_crBtnShadow,RGB(255,255,255));
				rcImage.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage,  RGB(0,0,0),gfxData.m_crBtnFace);
				rcImage.DeflateRect(2,2,2,2);
				pDC->Draw3dRect(rcImage,  RGB(255,255,255),RGB(0,0,0));
				rcImage.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage, gfxData.m_crBtnFace, gfxData.m_crBtnShadow);
			}
			break;
		case BORDER_DOUBLE_OUT:
			{
				pDC->Draw3dRect(rcImage,  RGB(255,255,255),RGB(0,0,0));
				rcImage.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage, gfxData.m_crBtnFace ,gfxData.m_crBtnShadow);
				rcImage.DeflateRect(2,2,2,2);
				pDC->Draw3dRect(rcImage,  gfxData.m_crBtnShadow,RGB(255,255,255));
				rcImage.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcImage,  RGB(0,0,0),gfxData.m_crBtnFace);
			}
			break;
			
		case BORDER_SUNKEN:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtnDkShadow, gfxData.m_crBtnLight);
			}
			break;
			
		case BORDER_HIGH:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dHiLight, gfxData.m_crBtnDkShadow);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtnLight, gfxData.m_crBtn3dShadow);
			}
			break;
			
		case BORDER_FLAT:
			{
				DrawRect(pDC, rcPos, gfxData.m_crBtnDkShadow);
				rcPos.DeflateRect(1,1,1,1);
				DrawRect(pDC, rcPos, gfxData.m_crBtnDkShadow);
			}
			break;
			
		case BORDER_LOW:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
			}
			break;
			
		case BORDER_UP:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dHiLight, gfxData.m_crBtn3dShadow);
			}
			break;
			
		case BORDER_SIMPLE:
			{
				DrawRect(pDC, rcPos, gfxData.m_crBtnDkShadow);;
			}
			break;
			
		case BORDER_FRAME:
			{
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtnDkShadow);
				rcPos.DeflateRect(1,1,1,1);
				DrawRect(pDC, rcPos, gfxData.m_cr3dFace);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtnDkShadow, gfxData.m_crBtn3dHiLight);
			}
			break;
			
		case BORDER_THIN_FRAME:
			{
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtnDkShadow);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtnDkShadow, gfxData.m_crBtn3dHiLight);
			}
			break;
			
		case BORDER_LIGHT_FRAME:
			{
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtn3dShadow);
				rcPos.DeflateRect(1,1,1,1);
				DrawRect(pDC, rcPos, gfxData.m_cr3dFace);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
			}
			break;
			
		case BORDER_THIN_LIGHT_FRAME:
			{
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtn3dShadow);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
			}
			break;
			
		case BORDER_ETCHED:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtn3dShadow);
			}
			break;
			
		case BORDER_ETCHED2:
			{
				pDC->Draw3dRect(rcPos, gfxData.m_crBtn3dShadow, gfxData.m_crBtn3dHiLight);
				rcPos.DeflateRect(1,1,1,1);
				DrawRect(pDC, rcPos, gfxData.m_crBtnDkShadow);
				rcPos.DeflateRect(1,1,1,1);
				pDC->Draw3dRect(rcPos,gfxData.m_crBtn3dHiLight, gfxData.m_crBtn3dShadow);
			}
			break;
			
		default:
			break;
		}
	}
}

void CFODropPaletteControl::DrawLine(CDC *pDC, CPoint& start, CPoint& end, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = pDC->SelectObject(&pen);
	
	DrawLine(pDC, start, end);
	
	pDC->SelectObject(oldPen);
}

void CFODropPaletteControl::DrawLine(CDC *pDC, int xStart, int yStart, int xEnd, int yEnd)
{
	pDC->MoveTo(xStart, yStart);
	pDC->LineTo(xEnd, yEnd);
}

void CFODropPaletteControl::DrawLine(CDC *pDC, int xStart, int yStart, int xEnd, int yEnd, COLORREF color)
{
	CPen pen(PS_SOLID,1,color);
	CPen* oldPen = pDC->SelectObject(&pen);
	
	DrawLine(pDC, xStart, yStart, xEnd, yEnd);
	
	pDC->SelectObject(oldPen);
}

void CFODropPaletteControl::OnDraw(CDC *pDC)
{
	CFont *pfdc;
	pfdc = pDC->SelectObject(&fText);

	CRect rectClient;
	rectClient = m_rcPosition;
	
	pDC->FillSolidRect(rectClient, gfxData.m_crBtnFace);

	DrawOtherBorder(pDC,rectClient,nBorderType);
	
	CFOColorCellObj* pSelCell = NULL;
	
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		
		m_crCells[i].OnDraw(pDC);
		
		if (m_crCells[i].GetCellColor() == m_crCurColor)
		{
			pSelCell = &m_crCells[i];
		}
	}
	
	if (pSelCell != NULL)
	{
		pSelCell->OnDrawSelect (pDC);

⌨️ 快捷键说明

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