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

📄 demo_paletteview.cpp

📁 可通过可执行文件中的使用颜色表和使用调色板在左方得到需要的颜色
💻 CPP
字号:
// Demo_PaletteView.cpp : implementation of the CDemo_PaletteView class
//

#include "stdafx.h"
#include "Demo_Palette.h"

#include "Demo_PaletteDoc.h"
#include "Demo_PaletteView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDemo_PaletteView

IMPLEMENT_DYNCREATE(CDemo_PaletteView, CFormView)

BEGIN_MESSAGE_MAP(CDemo_PaletteView, CFormView)
	//{{AFX_MSG_MAP(CDemo_PaletteView)
	ON_BN_CLICKED(IDC_COLOR, OnColor)
	ON_BN_CLICKED(IDC_PALETTE, OnPalette)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemo_PaletteView construction/destruction

CDemo_PaletteView::CDemo_PaletteView()
	: CFormView(CDemo_PaletteView::IDD)
{
	//{{AFX_DATA_INIT(CDemo_PaletteView)
	m_nRed = 0 ;
	m_nGreen = 0 ;
	m_nBlue = 0 ;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
}

CDemo_PaletteView::~CDemo_PaletteView()
{
}

void CDemo_PaletteView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemo_PaletteView)
	DDX_Text(pDX, IDC_EDIT2, m_nRed);
	DDX_Text(pDX, IDC_EDIT3, m_nGreen);
	DDX_Text(pDX, IDC_EDIT4, m_nBlue);
	//}}AFX_DATA_MAP
}

BOOL CDemo_PaletteView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CDemo_PaletteView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	//add
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if(pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString (IDD_ABOUTBOX);
		if(!strAboutMenu.IsEmpty ())
		{
			pSysMenu->AppendMenu (MF_SEPARATOR);
			pSysMenu->AppendMenu (MF_STRING, IDD_ABOUTBOX, strAboutMenu);
		}
	}
	
	LPLOGPALETTE lp;
	HANDLE hPal;
	hPal = GlobalAlloc(GMEM_MOVEABLE,sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
	lp = (LPLOGPALETTE)GlobalLock(hPal);
	lp->palVersion = 0x300;
	lp->palNumEntries = 256;

	int i;
	for(i = 0; i < 256; i++)
	{
		lp->palPalEntry[i].peBlue = 0;
		lp->palPalEntry[i].peGreen = 0;
		lp->palPalEntry[i].peRed = 0;
		lp->palPalEntry[i].peFlags = 0;

	}
	
	//create 85 red blue green color
	for(i = 0; i < 85; i++)
	{
		lp->palPalEntry[i].peRed = (BYTE)i * 3;
		lp->palPalEntry[i].peFlags = 0;
		 
		lp->palPalEntry[i + 86].peBlue = (BYTE)i * 3;
		lp->palPalEntry[i+ 86].peFlags = 0;
		
		lp->palPalEntry[i + 171].peGreen = (BYTE)i * 3;
		lp->palPalEntry[i + 171].peFlags = 0;
	}

	m_pPalette = new CPalette;
	m_pPalette->CreatePalette (lp);

	GlobalUnlock(hPal);
	GlobalFree(hPal);

	delete m_pPalette;

}

/////////////////////////////////////////////////////////////////////////////
// CDemo_PaletteView diagnostics

#ifdef _DEBUG
void CDemo_PaletteView::AssertValid() const
{
	CFormView::AssertValid();
}

void CDemo_PaletteView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CDemo_PaletteDoc* CDemo_PaletteView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDemo_PaletteDoc)));
	return (CDemo_PaletteDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDemo_PaletteView message handlers

void CDemo_PaletteView::OnView() 
{
	// TODO: Add your control notification handler code here
	this->UpdateData(TRUE);
	CClientDC dc(this);
	if(m_nFlag == 0)//使用调色板
		m_color = RGB(m_nRed, m_nGreen, m_nBlue) ;
	if(m_nFlag == 1)//使用颜色表对话框
		m_color = m_color ;
	dc.SelectPalette (m_pPalette, FALSE);
	dc.RealizePalette ();
	CRect rect;
	CBrush brush(m_color);

	this->GetClientRect (&rect);
	CGdiObject* pOldObject = dc.SelectObject (&brush);
	dc.Rectangle (rect.top ,rect.left ,rect.bottom ,rect.right);
	dc.SelectObject (pOldObject);
}

void CDemo_PaletteView::OnColor() 
{
	// TODO: Add your control notification handler code here
	m_nFlag = 1 ;
	CColorDialog dlg ;
	if(dlg.DoModal () == IDOK)
	{ 
		m_color = dlg.GetColor ();
		OnView();
	}
}

void CDemo_PaletteView::OnPalette() 
{
	// TODO: Add your control notification handler code here
	m_nFlag = 0 ;
	OnView();
}

⌨️ 快捷键说明

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