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

📄 fodroppalettecontrol.cpp

📁 visual c++ 实例编程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		if(bHasCustomBar)
		{
			CRect rcX = rcSep;
			rcX.bottom -= 2;
			pDC->Draw3dRect(rcX,gfxData.m_crBtnShadow,RGB(255,255,255));
			OnCustomDraw(pDC,FALSE);
			if(m_bCustomSelect)
			{
				OnCustomDrawSelect(pDC);
			}
		}
	}
	else
	{
		if(bHasCustomBar)
		{
			CRect rcX = rcSep;
			rcX.bottom -= 2;
			pDC->Draw3dRect(rcX,gfxData.m_crBtnShadow,RGB(255,255,255));
			OnCustomDraw(pDC,TRUE);
			if(m_bCustomSelect)
			{
				OnCustomDrawSelect(pDC);
			}
		}
	}
	pDC->SelectObject(pfdc);
}

int CFODropPaletteControl::GetAngleFromPoint(int nX, int nY)
{
	double dAngle = atan2(nY, nX);
	
	return (int) (dAngle * 180.0/PI);
}

void CFODropPaletteControl::ComputeColors()
{
	CRect rectClient;
	rectClient = m_rcPosition;
	rectClient.bottom = rectClient.bottom - 30;
	
	// Normalize to squere:
	if (rectClient.Height () < rectClient.Width ())
	{
		rectClient.DeflateRect ((rectClient.Width () - rectClient.Height ()) / 2, 0);
	}
	else
	{
		rectClient.DeflateRect (0, (rectClient.Height () - rectClient.Width ()) / 2);
	}
	
	ASSERT (abs (rectClient.Height () - rectClient.Width ()) <= 1);
	
	int nCellSize = rectClient.Height () / (2 * NUM_LEVELS - 1) + 1;
	
	int x = (rectClient.left + rectClient.right) / 2;
	int y = (rectClient.top + rectClient.bottom) / 2;

	// Add center cell
	m_crCells[0].SetCellColor(crWhite);
	m_crCells[0].AdjustCellPosition(x,y,nCellSize);
	// for each level
	int nNumber = 1;
	for (int nLevel = 1; nLevel < NUM_LEVELS; nLevel++)
	{
		// store the level start position
		int nPosX = x + (nCellSize * nLevel);
		int nPosY = y;
		
		// for each side
		for (int nSide = 0; nSide < NUM_LEVELS - 1; nSide++)
		{
			// set the deltas for the side
			int nDx = static_cast<int>(static_cast<float>(nCellSize) * cfxFOOffset[nSide]);
			int nDy = static_cast<int>(static_cast<float>(nCellSize) * cfyFOOffset[nSide]);
			
			// for each cell per side
			for (int nCell = 0; nCell < nLevel; nCell++)
			{
				int nAngle = GetAngleFromPoint(nPosX - x, nPosY - y);
				
				// TODO: Set the luminance and saturation properly
				double L = 1. * (NUM_LEVELS - nLevel) / NUM_LEVELS + .1;
				
				m_crCells[nNumber].SetCellColor(GetRGBFromHLSExtend ((float) nAngle, L, 1.0F));
				m_crCells[nNumber].AdjustCellPosition(nPosX,nPosY,nCellSize);
				nNumber ++;
				// offset the position
				nPosX += nDx;
				nPosY += nDy;
			}
		}
	}
	m_crCells[nNumber].SetCellColor(gfxData.m_crBtnFace);
	nNumber ++;
	m_crCells[nNumber].SetCellColor(crWhite);

	int nRGBOffset = 255 / (15 + 2);
	
	int nRGB = 255 - nRGBOffset;
	
	for (int i = 0; i < 15; i++)
	{
		COLORREF color = RGB(nRGB, nRGB, nRGB);
		m_crCells[nNumber].SetCellColor(color);
		nNumber ++;
		nRGB -= nRGBOffset;
	}
	m_crCells[nNumber].SetCellColor(crBlack);
}

void CFODropPaletteControl::AdjustCells(CRect rcPos)
{
	CRect rcTotal = rcPos;
	rcCells = rcTotal;
	
	// Determine the total width & height budgets.
	double MaxPixWide = double(rcTotal.Width());
	double maxPixHigh = double(rcTotal.Height());
	
	double dX;
	dX = 0;
	
	int *widthArray = new int[16+1];
	{
		double dlta = (double)(1/(double)16);
		for ( int i = 0 ; i < 16+1 ; i++ ) 
		{
			int x = rcTotal.left + int(double((MaxPixWide)*dlta*i));
			widthArray[i] = x;
		}
	}
	int *heightArray = new int[9+1];
	{
		double dlta = double(1/(double)9);
		for ( int i = 0 ; i < 9+1 ; i++ ) 
		{
			heightArray[i] = rcTotal.top + int(double((maxPixHigh)*dlta*i));
		}
	}
	
	CRect rcTemp;
	CRect rcOther;
	int nDay, nWeek;
	for (int x = 128 ; x < FO_TOTAL_CELLS ; x++ )
	{
		nDay = x % 16;
		nWeek = x / 16 ;
		rcTemp =  CRect(widthArray[nDay],heightArray[nWeek],widthArray[nDay+1],heightArray[nWeek+1]);
		m_crCells[x].SetFirstType(FALSE);
		m_crCells[x].SetRect(&rcTemp);
	}
	delete []heightArray;
	delete []widthArray;
}

void CFODropPaletteControl::CaptureMouse(BOOL bCapture)
{
	if(bCapture)
	{
		m_pParent->SetCapture();
	}
	else
	{
		ReleaseCapture();
	}
}

void CFODropPaletteControl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	nChar; 
	nRepCnt; 
	nFlags; 
	
}

void CFODropPaletteControl::OnLButtonUp(UINT nFlags, CPoint point) 
{
	nFlags;
	if(m_bCustomSelect)
	{
		m_bCustomSelect = FALSE;
		InvalRect(rcCustom);
	}
	for ( int i = 0 ; i < FO_TOTAL_CELLS ; i++ ) 
	{
		if ( m_crCells[i].HitTest(point) )
		{
			OnSelectOK((WPARAM)(m_crCells[i].GetCellColor()),0);
			return;
		}
	}
	if(rcCustom.PtInRect(point))
	{
		if(bHasCustomBar)
		{
			m_pParent->PostMessage( WM_FO_SELECTCOLORCUSTOM, (WPARAM)(m_crCurColor), 0 );
			if(m_bPopup)
			{
				m_pParent->ShowWindow(SW_HIDE);
			}
		}
	}
}

void CFODropPaletteControl::OnSelectOK(WPARAM wParam, LPARAM lParam)
{
	m_pParent->PostMessage( WM_FO_SELECTCOLOROK, wParam, lParam );
	if(bHasCustomBar)
	{
		UpdateCustom();
	}
	if(m_bPopup)
	{
		m_pParent->ShowWindow(SW_HIDE);
	}
}

void CFODropPaletteControl::OnSelectCancel(WPARAM wParam, LPARAM lParam)
{
	m_pParent->PostMessage( WM_FO_SELECTCOLORCANCEL, wParam, lParam );
	if(bHasCustomBar)
	{
		UpdateCustom();
	}
	if(m_bPopup)
	{
		m_pParent->ShowWindow(SW_HIDE);
	}
}

void CFODropPaletteControl::OnMouseMove(UINT nFlags, CPoint point) 
{
	nFlags;
	CFOColorCellObj* pSel = HitTest(point);
	if(pSel != NULL)
	{
		OnSelectOK((WPARAM)(pSel->GetCellColor()),0);
	}
	else if(rcCustom.PtInRect(point))
	{
		if(bHasCustomBar)
		{
			m_bCustomSelect = TRUE;
			InvalRect(rcCustom);
		}
	}
}

void CFODropPaletteControl::OnCancelMode()
{
	
}
void CFODropPaletteControl::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	nFlags; 
	point; 
}

void CFODropPaletteControl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	nFlags; 
	point; 
	
}

void CFODropPaletteControl::OnRButtonDown(UINT nFlags, CPoint point) 
{
	nFlags; 
	point; 
}

void CFODropPaletteControl::OnCustomDraw(CDC *pDC,BOOL bHasColor)
{
	ASSERT_VALID (pDC);
	pDC->FillSolidRect(rcCustom,gfxData.m_crBtnFace);
	rcCustom = rcCustomSave;
	if(bHasColor)
	{
		CRect rcItem;
		rcItem = rcCustom;
		rcItem.left = rcItem.right -21;
		rcCustom.right = rcItem.left-2;
		
		CRect rcTemp;
		rcTemp = rcCustom;
		rcTemp.DeflateRect(2,2,2,2);
		CPen pen (PS_SOLID, 1, gfxData.m_crBtnShadow);
		CPen* pOldPen = pDC->SelectObject(&pen);
		
		CString strText;
		strText = strCustom;
		if (strText.GetLength())
		{
			pDC->SetBkMode(TRANSPARENT);
			pDC->SetTextColor(0);
			pDC->DrawText(strText, rcTemp, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
		}
		
		pDC->SelectObject (pOldPen);
		pDC->Draw3dRect(rcItem,gfxData.m_crBtnShadow,RGB(255,255,255));
		rcItem.DeflateRect(4,4,4,4);
		pDC->FillSolidRect(rcItem,m_crCurColor);
		pDC->Draw3dRect(rcItem,gfxData.m_crBtnShadow,gfxData.m_crBtnShadow);
	}
	else
	{
		CRect rcPos;
		rcPos = rcCustom;
		rcPos.DeflateRect(2,2,2,2);
		CPen pen (PS_SOLID, 1, gfxData.m_crBtnShadow);
		CPen* pOldPen = pDC->SelectObject(&pen);
		
		CString strText;
		strText = strCustom;
		if (strText.GetLength())
		{
			pDC->SetBkMode(TRANSPARENT);
			pDC->SetTextColor(0);
			pDC->DrawText(strText, rcPos, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
		}
		
		pDC->SelectObject (pOldPen);
	}
}

void CFODropPaletteControl::OnCustomDrawSelect(CDC *pDC)
{
	ASSERT_VALID (pDC);
	
	CBrush* pBrWhite = CBrush::FromHandle ((HBRUSH) ::GetStockObject (WHITE_BRUSH));
	ASSERT_VALID (pBrWhite);
	
	CBrush* pBrBlack = CBrush::FromHandle ((HBRUSH) ::GetStockObject (BLACK_BRUSH));
	ASSERT_VALID (pBrBlack);
	
	CRgn rgnOne, rgnTwo, rgnThree;
	
	CRect rcPos;
	rcPos = rcCustom;
	rcPos.DeflateRect(1,1,1,1);
	
	rgnTwo.CreateRectRgnIndirect(&rcPos);
	pDC->FrameRgn(&rgnTwo, pBrWhite, 2, 2);
	
	rcPos.InflateRect(2,2,2,2);
	
	rgnThree.CreateRectRgnIndirect(&rcPos);
	pDC->FrameRgn(&rgnThree, pBrBlack, 1, 1);
	
}

void CFODropPaletteControl::SetCustomBar(const BOOL bCustom)
{
	bHasCustomBar = bCustom;
}

void CFODropPaletteControl::SetMainBorderSize(const int nBorder)
{
	nMainBorderSize = nBorder;
}

void CFODropPaletteControl::UpdateCustom()
{
	InvalRect(rcCustomSave);
}

void CFODropPaletteControl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	nFlags;
	nRepCnt;
	// Determine if the CONTROL key is pressed...
	int ks = GetKeyState( VK_CONTROL );
	ks >>= ((sizeof(int)*8)-1);

	int nCurSel  = 0;
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		if (m_crCells[i].GetCellColor() == m_crCurColor)
		{
			nCurSel = i;
			break;
		}
	}
	int nNewCell = nCurSel;
	switch(nChar) 
	{
	case VK_LEFT:
		{
			if(nCurSel == 0) 
			{
				nNewCell = FO_TOTAL_CELLS-1;
			}
			else if(nCurSel >= 0)
			{
				nNewCell --;
			}

			SetRGB(m_crCells[nNewCell].GetCellColor());
			m_pParent->PostMessage( WM_FO_SELECTCOLOROK, (WPARAM)m_crCells[nNewCell].GetCellColor(), 0 );
		}
		break;
		
	case VK_RIGHT:
		{
			if(nCurSel == (FO_TOTAL_CELLS-1)) 
			{
				nNewCell = 0;
			}
			else if(nCurSel < (FO_TOTAL_CELLS-1))
			{
				nNewCell ++;
			}

			SetRGB(m_crCells[nNewCell].GetCellColor());
			m_pParent->PostMessage( WM_FO_SELECTCOLOROK, (WPARAM)m_crCells[nNewCell].GetCellColor(), 0 );
		}
		break;
		
	case VK_DOWN:
		{
			if(nCurSel == (FO_TOTAL_CELLS-1)) 
			{
				nNewCell = 0;
			}
			else if(nCurSel < (FO_TOTAL_CELLS-1))
			{
				nNewCell ++;
			}

			SetRGB(m_crCells[nNewCell].GetCellColor());
			m_pParent->PostMessage( WM_FO_SELECTCOLOROK, (WPARAM)m_crCells[nNewCell].GetCellColor(), 0 );
		}
		break;
		
	case VK_UP:
		{
			if(nCurSel == 0) 
			{
				nNewCell = FO_TOTAL_CELLS-1;
			}
			else if(nCurSel >= 0)
			{
				nNewCell --;
			}

			SetRGB(m_crCells[nNewCell].GetCellColor());
			m_pParent->PostMessage( WM_FO_SELECTCOLOROK, (WPARAM)m_crCells[nNewCell].GetCellColor(), 0 );
		}
		break;

	case VK_RETURN:
		{
			if(m_bPopup)
			{
				OnSelectOK((WPARAM)m_crCurColor,0L);
			}
		}
		break;

	case VK_CANCEL:
	case VK_ESCAPE:
		{
			if(m_bPopup)
			{
				OnSelectCancel((WPARAM)m_crCurColor,0L);
			}
		}
		break;

	case VK_TAB:
		{
			
			// For shift tab, we move to the previous item in the Z order
			// of the parent window.
			CWnd* pParent = m_pParent;
			if( pParent != NULL )
			{
				CWnd* pPrevFocus = pParent->GetParent()->GetNextDlgTabItem(m_pParent, TRUE);
				if(pPrevFocus != NULL)
					pPrevFocus->SetFocus();
			}
			
		}
		return;
	}
}

void CFODropPaletteControl::SetCellBorderSize(const int nBorder)
{
	nCellBorderSize = nBorder;
	for (int i = 0; i < FO_TOTAL_CELLS; i ++)
	{
		m_crCells[i].SetCellBorderSize(nBorder);
	}
}

BYTE CFODropPaletteControl::GetRGBFromHue(float rm1, float rm2, float rh)
{
	if (rh > 360.0f)
		rh -= 360.0f;
	else if (rh < 0.0f)
		rh += 360.0f;
	
	if (rh <  60.0f)
		rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;   
	else if (rh < 180.0f)
		rm1 = rm2;
	else if (rh < 240.0f)
		rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;      
	
	return static_cast<BYTE>(rm1 * 255);
}

COLORREF CFODropPaletteControl::GetRGBFromHLS( double H, double L, double S )
{
	double r, g, b;
	double m1, m2;
	
	if(S==0) 
	{
		r=g=b=L;
	} 
	else 
	{
		if(L <=0.5)
			m2 = L*(1.0+S);
		else
			m2 = L+S-L*S;
		m1 = 2.0*L-m2;
		r = GetRGBFromHue((float)m1, (float)m2, (float)(H+1.0/3.0));
		g = GetRGBFromHue((float)m1, (float)m2, (float)H);
		b = GetRGBFromHue((float)m1, (float)m2, (float)(H-1.0/3.0));
	}
	return RGB((BYTE)(r*255), (BYTE)(g*255), (BYTE)(b*255));
}

COLORREF CFODropPaletteControl::GetRGBFromHLSExtend( double H, double L, double S )
{
	WORD R, G, B; // RGB component values
	
	if (S == 0.0)
	{
		R = G = B = unsigned char(L * 255.0);
	}
	else
	{
		float rm1, rm2;
		
		if (L <= 0.5f)
			rm2 = (float)(L + L * S);
		else
			rm2 = (float)(L + S - L * S);
		
		rm1 = (float)(2.0f * L - rm2);
		
		R = GetRGBFromHue(rm1, rm2, (float)(H + 120.0f));
		G = GetRGBFromHue(rm1, rm2, (float)(H));
		B = GetRGBFromHue(rm1, rm2, (float)(H - 120.0f));
	}
	
	return RGB(R, G, B);
}

void CFODropPaletteControl::ConvertRGBToHSL( COLORREF rgb, double *H, double *S, double *L )
{   
	double delta;
	double r = (double)GetRValue(rgb)/255;
	double g = (double)GetGValue(rgb)/255;
	double b = (double)GetBValue(rgb)/255;   
	double cmax = max(r, max(g, b));
	double cmin = min(r, min(g, b));   
	*L=(cmax+cmin)/2.0;   
	
	if(cmax==cmin) 
	{
		*S = 0;      
		*H = 0; // it's really undefined   
	} 
	else 
	{
		if(*L < 0.5) 
			*S = (cmax-cmin)/(cmax+cmin);      
		else
			*S = (cmax-cmin)/(2.0-cmax-cmin);      
		
		delta = cmax - cmin;
		if(r==cmax) 
			*H = (g-b)/delta;      
		else if(g==cmax)
			*H = 2.0 +(b-r)/delta;
		else          
			*H=4.0+(r-g)/delta;
		*H /= 6.0; 
		if(*H < 0.0)
			*H += 1;  
	}
}

⌨️ 快捷键说明

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