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

📄 xcolourpopup.cpp

📁 读取XML信息
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}
		ChangeSelection(row, col);
	}

	if (nChar == VK_ESCAPE)
	{
		m_crColor = m_crInitialColor;
		EndSelection(CPN_SELENDCANCEL);
		return;
	}

	if (nChar == VK_RETURN)
	{
		EndSelection(CPN_SELENDOK);
		return;
	}

	CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

///////////////////////////////////////////////////////////////////////////////
// OnNcDestroy - auto-deletion
void CXColourPopup::OnNcDestroy()
{
	CWnd::OnNcDestroy();
	delete this;
}

///////////////////////////////////////////////////////////////////////////////
// OnPaint
void CXColourPopup::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	// Draw color cells
	for (int row = 0; row < m_nNumRows; row++)
		for (int col = 0; col < m_nNumColumns; col++)
			DrawCell(&dc, row, col);

	// Draw raised window edge (ex-window style WS_EX_WINDOWEDGE is sposed to do this,
	// but for some reason isn't
	CRect rect;
	GetClientRect(rect);
	dc.DrawEdge(rect, EDGE_RAISED, BF_RECT);
}

///////////////////////////////////////////////////////////////////////////////
// OnMouseMove
void CXColourPopup::OnMouseMove(UINT nFlags, CPoint point)
{
	int row, col;

	if (point.x < 0 || point.y < 0)
	{
		ChangeSelection(-1, -1);
		CWnd::OnMouseMove(nFlags, point);
		return;
	}

	// Translate points to be relative raised window edge
	point.x -= m_nMargin;
	point.y -= m_nMargin;

	// First check we aren't in text box
	if (m_bShowCustom && m_TextRect.PtInRect(point))
	{
		row = CUSTOM_BUTTON_ROW;        // Special value meaning Custom button (hack!)
		col = 0;
	}
	else if (m_bShowCustom && m_DividerRect.PtInRect(point))
	{
		row = DIVIDER_LINE_ROW;        // Special value meaning divider line
		col = 0;
	}
	else
	{
		// Get the row and column
		row = point.y / m_nBoxSize,
		col = point.x / m_nBoxSize;

		// In range? If not, default and exit
		if (row < 0 || row >= m_nNumRows || row == DIVIDER_LINE_ROW ||
				col < 0 || col >= m_nNumColumns)
		{
			ChangeSelection(-1, -1);
			CWnd::OnMouseMove(nFlags, point);
			return;
		}
	}

	// OK - we have the row and column of the current selection (may be TEXT_BOX_VALUE)
	// Has the row/col selection changed? If yes, then redraw old and new cells.
	if (row != m_nCurrentRow || col != m_nCurrentCol)
	{
		ChangeSelection(row, col);
	}

	CWnd::OnMouseMove(nFlags, point);
}

///////////////////////////////////////////////////////////////////////////////
// OnLButtonUp - end selection on LButtonUp
void CXColourPopup::OnLButtonUp(UINT nFlags, CPoint point)
{
	CWnd::OnLButtonUp(nFlags, point);

	DWORD pos = GetMessagePos();
	point = CPoint(GET_X_LPARAM(pos), GET_Y_LPARAM(pos));

	if (m_WindowRect.PtInRect(point))
		EndSelection(CPN_SELENDOK);
	else
		EndSelection(CPN_SELENDCANCEL);
}

///////////////////////////////////////////////////////////////////////////////
// GetCellRect - gets the dimensions of the color cell given by (row,col)
BOOL CXColourPopup::GetCellRect(int row, int col, const LPRECT & rect)
{
	if (row < 0 || row >= m_nNumRows || col < 0 || col >= m_nNumColumns)
		return FALSE;

	rect->left = col * m_nBoxSize + m_nMargin;
	rect->top  = row * m_nBoxSize + m_nMargin;

	rect->right = rect->left + m_nBoxSize;
	rect->bottom = rect->top + m_nBoxSize;

	if (row == DIVIDER_LINE_ROW)
		return FALSE;
	else
		return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
// SetWindowSize - works out an appropriate size and position of this window
void CXColourPopup::SetWindowSize()
{
	CSize TextSize;

	// If we are showing a custom text area, get the font and text size.
	if (m_bShowCustom)
	{
		// Create the font
		if (m_Font.GetSafeHandle() == NULL)
		{
			NONCLIENTMETRICS ncm;
			ncm.cbSize = sizeof(NONCLIENTMETRICS);
			VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
						sizeof(NONCLIENTMETRICS), &ncm, 0));
			m_Font.CreateFontIndirect(&(ncm.lfMessageFont));
		}

		// Get the size of the custom text
		CClientDC dc(this);
		CFont * pOldFont = (CFont *) dc.SelectObject(&m_Font);
		TextSize = dc.GetTextExtent(m_strCustomText)
			+ CSize(2 * m_nMargin, 2 * m_nMargin);
		dc.SelectObject(pOldFont);
	}

	// Get the number of columns and rows
	m_nNumColumns = 8;
	m_nNumRows = (m_nNumColors / m_nNumColumns);
	if (m_nNumColors % m_nNumColumns)
		m_nNumRows++;

	// Get the current window position, and set the new size
	CRect rect;
	GetWindowRect(rect);

	m_WindowRect.SetRect(rect.left, rect.top,
		rect.left + m_nNumColumns * m_nBoxSize + 2 * m_nMargin,
		rect.top + m_nNumRows * m_nBoxSize + 2 * m_nMargin);

	// if custom text, then expand window if necessary, and set text width as
	// window width
	if (m_bShowCustom)
	{
		//m_WindowRect.bottom += (m_nMargin + TextSize.cy);
		if (TextSize.cx > m_WindowRect.Width())
			m_WindowRect.right = m_WindowRect.left + TextSize.cx;
		TextSize.cx = m_WindowRect.Width() - 2 * m_nMargin;

		// Work out the text area
		m_TextRect.SetRect(m_nMargin,
						   m_nMargin + CUSTOM_BUTTON_ROW*m_nBoxSize,
						   m_nMargin + TextSize.cx,
						   m_nMargin + CUSTOM_BUTTON_ROW*m_nBoxSize + TextSize.cy);
		m_DividerRect.SetRect(m_nMargin,
						   m_nMargin + DIVIDER_LINE_ROW*m_nBoxSize,
						   m_nMargin + TextSize.cx,
						   m_nMargin + DIVIDER_LINE_ROW*m_nBoxSize + m_nBoxSize);
	}

	// Set the window size and position
	MoveWindow(m_WindowRect, TRUE);
}

///////////////////////////////////////////////////////////////////////////////
// CreateToolTips
void CXColourPopup::CreateToolTips()
{
	// Create the tool tip
	if (!m_ToolTip.Create(this))
		return;

	// Add a tool for each cell
	for (int row = 0; row < m_nNumRows; row++)
	{
		if (row == DIVIDER_LINE_ROW)
			continue;

		if (row == CUSTOM_BUTTON_ROW)
		{
			m_ToolTip.AddTool(this, _T("Selects a custom color"), m_TextRect, 1);
			continue;
		}

		for (int col = 0; col < m_nNumColumns; col++)
		{
			CRect rect;
			if (!GetCellRect(row, col, rect))
			{
				continue;
			}
			VERIFY(m_ToolTip.AddTool(this, GetColorName(row, col), rect, 1));
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// ChangeSelection
void CXColourPopup::ChangeSelection(int row, int col)
{
	CClientDC dc(this);        // device context for drawing

	//int row = (row1 < 5) ? row1 : row1 + 1;

	if ((m_nCurrentRow >= 0 && m_nCurrentRow < m_nNumRows &&
			m_nCurrentCol >= 0 && m_nCurrentCol < m_nNumColumns) ||
		(m_nCurrentRow == CUSTOM_BUTTON_ROW) ||
		(m_nCurrentRow == DIVIDER_LINE_ROW))
	{
		// Set Current selection as invalid and redraw old selection (this way
		// the old selection will be drawn unselected)
		int OldRow = m_nCurrentRow;
		int OldCol = m_nCurrentCol;
		m_nCurrentRow = m_nCurrentCol = -1;
		DrawCell(&dc, OldRow, OldCol);
	}

	if (row == -1 || col == -1)
		return;

	// Set the current selection as row/col and draw (it will be drawn selected)
	m_nCurrentRow = row;
	m_nCurrentCol = col;

	if (row == DIVIDER_LINE_ROW)
		return;

	DrawCell(&dc, row, col);

	// Store the current color
	if (m_nCurrentRow == CUSTOM_BUTTON_ROW)// && m_nCurrentCol == TEXT_BOX_VALUE)
	{
		m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crInitialColor, 0);
	}
	else
	{
		m_crColor = GetColor(m_nCurrentRow, m_nCurrentCol);
		m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crColor, 0);
	}
}

///////////////////////////////////////////////////////////////////////////////
// EndSelection
void CXColourPopup::EndSelection(int nMessage)
{
	ReleaseCapture();

	if (nMessage != CPN_SELENDCANCEL && m_nCurrentRow == DIVIDER_LINE_ROW)
	{
		// ignore click on divider line
		SetCapture();
		return;
	}
	else if (nMessage != CPN_SELENDCANCEL && m_nCurrentRow == CUSTOM_BUTTON_ROW)
	{
		// if custom text selected, perform a custom color selection
		COLORREF rgb[16];
		int i, row, col;
		for (i = 0, row = FIRST_CUSTOM_ROW; row < FIRST_CUSTOM_ROW+2; row++)
			for (col = 0; col < 8; col++)
				rgb[i++] = GetColor(row, col);

		CColorDialog dlg(m_crInitialColor, CC_FULLOPEN | CC_ANYCOLOR, this);
		dlg.m_cc.lStructSize  = sizeof(CHOOSECOLOR);
		dlg.m_cc.hwndOwner    = m_hWnd;
		dlg.m_cc.lpCustColors = rgb;
		dlg.m_cc.Flags        |= CC_FULLOPEN | CC_ANYCOLOR;

		if (dlg.DoModal() == IDOK)
		{
			m_crColor = dlg.GetColor();
			for (i = 0, row = FIRST_CUSTOM_ROW; row < FIRST_CUSTOM_ROW+2; row++)
				for (col = 0; col < 8; col++)
					SetColor(row, col, rgb[i++]);
			Invalidate();
		}
		SetCapture();
		return;
	}

	if (nMessage == CPN_SELENDCANCEL)
		m_crColor = m_crInitialColor;

	m_nSelectedRow = m_nCurrentRow;
	m_nSelectedCol = m_nCurrentCol;

	if (m_pCustomColors)
	{
		for (int i = 0; i < 16; i++)
		{
			m_pCustomColors[i] = m_crColors[FIRST_CUSTOM_ROW * 8 + i].crColor;
		}
	}

	m_pParent->SendMessage(nMessage, (WPARAM) m_crColor,
		MAKELONG((WORD)m_nSelectedRow, (WORD)m_nSelectedCol));

	DestroyWindow();
}

///////////////////////////////////////////////////////////////////////////////
// DrawCell
void CXColourPopup::DrawCell(CDC * pDC, int row, int col)
{
	// This is a special hack for the text box
	if (m_bShowCustom && row == CUSTOM_BUTTON_ROW)// && row == TEXT_BOX_VALUE)
	{
		pDC->FillSolidRect(m_TextRect, ::GetSysColor(COLOR_3DFACE));
		if (m_nCurrentRow == row && m_nCurrentCol == col)
			pDC->DrawEdge(m_TextRect, EDGE_RAISED, BF_RECT);

		// Draw custom text
		CFont * pOldFont = (CFont *) pDC->SelectObject(&m_Font);
		pDC->DrawText(m_strCustomText, m_TextRect,
			DT_CENTER | DT_VCENTER | DT_SINGLELINE);
		pDC->SelectObject(pOldFont);

		return;
	}

	// row/col in range?
	ASSERT(row >= 0 && row < m_nNumRows);
	ASSERT(col >= 0 && col < m_nNumColumns);

	CRect rect;
	if (!GetCellRect(row, col, rect))
	{
		pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
		if (row != DIVIDER_LINE_ROW)
			return;

		// we're trying to draw divider line
		CRect divider(rect);
		int h = divider.Height();
		GetCellRect(row, 7, rect);
		divider.right = rect.right;
		divider.top += (h/2) - 2;
		divider.bottom -= (h/2) - 2;
		UINT nFlags = BF_BOTTOM | BF_TOP;
		if (col == 0)
			nFlags |= BF_LEFT;
		if (col == 7)
			nFlags |= BF_RIGHT;
		pDC->DrawEdge(divider, EDGE_SUNKEN, nFlags);
		return;
	}

	// fill background
	pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));

	// Draw raised edge if selected
	if (m_nCurrentRow == row && m_nCurrentCol == col)
		pDC->DrawEdge(rect, EDGE_RAISED, BF_RECT);
	else if (m_nSelectedRow == row && m_nSelectedCol == col)
		pDC->DrawEdge(rect, EDGE_SUNKEN, BF_RECT);

	COLORREF rgbCell = GetColor(row, col);
	if (rgbCell == -1)
		rgbCell = RGB(255, 255, 255);
	CBrush brush(rgbCell);
	CPen pen;
	pen.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));

	CBrush * pOldBrush = (CBrush *) pDC->SelectObject(&brush);
	CPen * pOldPen = (CPen *) pDC->SelectObject(&pen);

	// Draw the cell color
	rect.DeflateRect(m_nMargin + 1, m_nMargin + 1);
	pDC->Rectangle(rect);

	// restore DC and cleanup
	pDC->SelectObject(pOldBrush);
	pDC->SelectObject(pOldPen);
	brush.DeleteObject();
	pen.DeleteObject();
}

⌨️ 快捷键说明

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