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

📄 colorpalettebar.cpp

📁 在mfc环境下实现的绘图板程序
💻 CPP
字号:
// ColorPaletteBar.cpp : 实现文件
//

#include "stdafx.h"
#include "ImageEditor.h"
#include "ColorPaletteBar.h"


// CColorPaletteBar

IMPLEMENT_DYNAMIC(CColorPaletteBar, CDialogBar)

const int cSpace = 1;
const int cCellX = 15; 
const int cCellY = 15; 
const int cLeft = 100;
const int cTop = 5;

CColorPaletteBar::CColorPaletteBar()
{
	m_colorFore = RGB(0,0,0) ;
	m_colorBack = RGB(255,255,255);

	m_colors[0] = RGB(0,0,0);
	m_colors[1] = RGB(128,128,128);
	m_colors[2] = RGB(128,0,0);
	m_colors[3] = RGB(128,128,0);
	m_colors[4] = RGB(0,128,0);
	m_colors[5] = RGB(0,128,128);
	m_colors[6] = RGB(0,0,128);
	m_colors[7] = RGB(128,0,128);
	m_colors[8] = RGB(128,128,64);
	m_colors[9] = RGB(0,64,64);
	m_colors[10] = RGB(0,128,255);
	m_colors[11] = RGB(0,64,128);
	m_colors[12] = RGB(128,0,255);
	m_colors[13] = RGB(128,64,0);

	m_colors[14] = RGB(255,255,255);
	m_colors[15] = RGB(192,192,192);
	m_colors[16] = RGB(255,0,0);
	m_colors[17] = RGB(255,255,0);
	m_colors[18] = RGB(0,255,0);
	m_colors[19] = RGB(0,255,255);
	m_colors[20] = RGB(0,0,255);
	m_colors[21] = RGB(255,0,255);
	m_colors[22] = RGB(255,255,128);
	m_colors[23] = RGB(0,255,128);
	m_colors[24] = RGB(128,255,255);
	m_colors[25] = RGB(128,128,255);
	m_colors[26] = RGB(255,0,128);
	m_colors[27] = RGB(255,128,64);

	//前景色 背景色显示区
	int x = 2*cCellY+cSpace;
	m_rectColor.SetRect(cLeft-x-cSpace, cTop, cLeft-cSpace, cTop+x);
	CRect rect(m_rectColor);
	rect.DeflateRect(m_rectColor.Width()*0.28, m_rectColor.Height()*0.28);
	m_rectColorFore = CRect(rect);
	m_rectColorBack = CRect(rect);
	m_rectColorFore.OffsetRect(-rect.Width()*0.3, -rect.Width()*0.3);
	m_rectColorBack.OffsetRect(rect.Width()*0.3, rect.Width()*0.3);

	//颜色选择框区
	for (int i=0; i<nColorNum; i++)
	{
		m_rects[i] = CRect(cLeft, cTop, cLeft+cCellX, cTop+cCellY);
	}
	for (int i=0; i<nColorNum/2; i++)//第一排
	{		
		m_rects[i].OffsetRect((cCellX+cSpace)*i, 0);
	}
	for (int i=nColorNum/2; i<nColorNum; i++)//第二排
	{		
		m_rects[i].OffsetRect((cCellX+cSpace)*(i-nColorNum/2),cCellY+cSpace);
	}

	//字体
	m_rectFont.SetRect(5,5,50,35);
	m_font.CreateFont(-1 * 12, 0, 0, 0, 20, 
						FALSE, FALSE, 0,
						ANSI_CHARSET, OUT_DEFAULT_PRECIS,
						CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
						DEFAULT_PITCH | FF_SWISS, _T("Arial")); 
	m_bFontFoucsed = FALSE;
}

CColorPaletteBar::~CColorPaletteBar()
{
}


BEGIN_MESSAGE_MAP(CColorPaletteBar, CDialogBar)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()



// CColorPaletteBar 消息处理程序



void CColorPaletteBar::OnPaint()
{
	CPaintDC dc(this); 
	
	//前景色/背景色
	dc.DrawEdge(m_rectColor,EDGE_SUNKEN,BF_RECT);
	dc.FillRect(m_rectColorBack, &CBrush(m_colorBack));
	dc.DrawEdge(m_rectColorBack,EDGE_RAISED,BF_RECT);
	dc.FillRect(m_rectColorFore, &CBrush(m_colorFore));
	dc.DrawEdge(m_rectColorFore,EDGE_RAISED,BF_RECT);

	//颜色选择框
	for (int i=0; i<nColorNum; i++)
	{
		dc.FillRect(m_rects[i], &CBrush(m_colors[i]));
		dc.DrawEdge(m_rects[i],EDGE_SUNKEN,BF_RECT);
	}

	//font
	if (m_bFontFoucsed)
	{
		dc.DrawEdge(m_rectFont,EDGE_SUNKEN,BF_RECT);
		CRect rect(m_rectFont);
		rect.DeflateRect(2,2);
		dc.DrawFocusRect(rect);
	}
	else
		dc.DrawEdge(m_rectFont,EDGE_RAISED,BF_RECT);
	CFont* pOldFont = dc.SelectObject(&m_font);
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(m_colorText);
	CRect rect(m_rectFont);
	rect.top += 5;
	dc.DrawText(_T("字体"), rect, DT_CENTER);
	dc.SelectObject(pOldFont);
}



void CColorPaletteBar::OnLButtonDown(UINT nFlags, CPoint point)
{
	

	//font
	if (m_rectFont.PtInRect(point))
	{
		m_bFontFoucsed = TRUE;
		this->InvalidateRect(m_rectFont,FALSE);
		OnBnClickedBtnFont();
		return;
	}

	//color
	for (int i=0; i<nColorNum; i++)
	{
		if (m_rects[i].PtInRect(point))
		{
			m_colorFore = m_colors[i];
			this->InvalidateRect(m_rectColorFore,FALSE);
			//break;
			return;
		}
	}
	CDialogBar::OnLButtonDown(nFlags, point);
}

void CColorPaletteBar::OnRButtonDown(UINT nFlags, CPoint point)
{
	CDialogBar::OnRButtonDown(nFlags, point);
	
	//color
	for (int i=0; i<nColorNum; i++)
	{
		if (m_rects[i].PtInRect(point))
		{
			m_colorBack = m_colors[i];
			this->InvalidateRect(m_rectColorBack,FALSE);
			break;
		}
	}
}

void CColorPaletteBar::OnLButtonDblClk(UINT nFlags, CPoint point)
{

	int i =0;
	for (i=0; i<nColorNum; i++)
	{
		if (m_rects[i].PtInRect(point))
		{
			break;
		}
	}

	if (i<nColorNum)
	{
		CColorDialog dlgColor;
		if (dlgColor.DoModal() != IDOK)
			return;
		m_colors[i] = dlgColor.GetColor();
		m_colorFore = m_colors[i];
		this->InvalidateRect(m_rectColorFore,FALSE);
		this->InvalidateRect(m_rects[i],FALSE);
	}
	//CDialogBar::OnLButtonDblClk(nFlags, point);// 须 屏蔽
}

void CColorPaletteBar::OnBnClickedBtnFont()
{
	LOGFONT lf;
	m_font.GetLogFont(&lf);
	CFontDialog dlg(&lf);
	dlg.m_cf.rgbColors = m_colorText;
	if (dlg.DoModal() == IDOK)
	{
		m_colorText = dlg.GetColor();
		m_font.DeleteObject();
		m_font.CreateFontIndirectW(&lf);
	}
	m_bFontFoucsed = FALSE;
	this->InvalidateRect(m_rectFont);
}

⌨️ 快捷键说明

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