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

📄 paletvw.cpp

📁 C:Documents and SettingsAdministrator桌面VC++多媒体特效制作百例CHAR02CreatePalet
💻 CPP
字号:
// paletvw.cpp : implementation of the CPaletexpView class
//

#include "stdafx.h"
#include "paletexp.h"

#include "paletdoc.h"
#include "paletvw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPaletexpView

IMPLEMENT_DYNCREATE(CPaletexpView, CView)

BEGIN_MESSAGE_MAP(CPaletexpView, CView)
	//{{AFX_MSG_MAP(CPaletexpView)
	ON_WM_PAINT()
	ON_COMMAND(ID_EDIT_BLUES, OnEditBlues)
	ON_COMMAND(ID_EDIT_GREENS, OnEditGreens)
	ON_COMMAND(ID_EDIT_REDS, OnEditReds)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPaletexpView construction/destruction

CPaletexpView::CPaletexpView()
{
	//Create a blank logical palette with PALSIZE entries. The
	//program will later fill this palette with specific color values.

	LPLOGPALETTE lpLogPal;
	HANDLE hPal;

	//Allocate memory for the palette entry array.
	hPal = GlobalAlloc(GMEM_MOVEABLE, sizeof(LOGPALETTE) + PALSIZE * sizeof(PALETTEENTRY));
	if (hPal == NULL)
		{
		AfxMessageBox("Memory allocation error");
		return;
		}
	//Initialize the LPLOGPALETTE pointer to point at the
	//beginning of the locked allocated memory.
	lpLogPal = (LPLOGPALETTE) GlobalLock(hPal);

	//Fill members of the LOGPALETTE structure to indicate the
	//Windows version and the number of palette entries.
	lpLogPal->palVersion = 0x300;
	lpLogPal->palNumEntries = 256;

	//Fill all palette enties with 0.
	for (int i = 0; i < 256 ; i++)
		{
		lpLogPal->palPalEntry[i].peRed = 0;
		lpLogPal->palPalEntry[i].peGreen = 0;
		lpLogPal->palPalEntry[i].peBlue = 0;
		lpLogPal->palPalEntry[i].peFlags = 0;
		}

	//Create a logical palette from the array values.
	if (!palette.CreatePalette(lpLogPal))
		AfxMessageBox("Palette creation failed!");

	//Unlock and free the memory.
	GlobalUnlock(hPal);
	GlobalFree(hPal);

	//Set flag so the blank palette is not used.
	newPalette = FALSE;
}

CPaletexpView::~CPaletexpView()
{
}

/////////////////////////////////////////////////////////////////////////////
// CPaletexpView drawing

void CPaletexpView::OnDraw(CDC* pDC)
{
	CPaletexpDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CPaletexpView diagnostics

#ifdef _DEBUG
void CPaletexpView::AssertValid() const
{
	CView::AssertValid();
}

void CPaletexpView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CPaletexpView message handlers

void CPaletexpView::OnPaint() 
{
//Displays a grid of 256 rectangles in the 256 palette colors.

	RECT rect;
	int boxHeight, boxWidth, row, column;

	CPaintDC dc(this); // device context for painting
	
	//If a new palette has been created, select 
	//and realize it.
	if (newPalette)
		{
		if (!dc.SelectPalette(&palette, FALSE))
			MessageBox("Error selecting palette.");
		dc.RealizePalette();
		newPalette = FALSE;
		}

	//Get the window extent and calculate box sizes.
	CWnd::GetClientRect(&rect);
	boxHeight = rect.bottom / 16;
	boxWidth = rect.right / 16;

	for (long colorIndex = 0; colorIndex < 256; colorIndex++)
		{
		row = colorIndex / 16 + 1;
		column = colorIndex % 16 + 1;
		CBrush hBrush;
		hBrush.CreateSolidBrush (0x1000000 | colorIndex);
		dc.SelectObject(&hBrush);
		dc.Rectangle((column - 1) * boxWidth,
					  (row - 1) * boxHeight,
					  (column * boxWidth),
					  (row * boxHeight));
		dc.SelectObject(GetStockObject(BLACK_BRUSH));
		DeleteObject(&hBrush);
		}
}

BOOL CPaletexpView::m_ChangePalette(int colors)
{
	//Creates a logical palette with a set
	//of red, green, or blue hues.

	int r = 0, g = 0, b = 0;
	PALETTEENTRY newPalette[PALSIZE];
	
	switch (colors)
		{
		case RED:
			r = 1;
			break;
		case GREEN:
			g = 1;
			break;
		case BLUE:
			b = 1;
		}

	//Fill in the array.
	for (unsigned int i = 0; i < PALSIZE ; i++)
		{
		newPalette[i].peRed = i * r;
		newPalette[i].peGreen = i * g;
		newPalette[i].peBlue = i * b;
		newPalette[i].peFlags = 0;
		}

	if (!palette.SetPaletteEntries(0, 256, (LPPALETTEENTRY)newPalette))
		return FALSE;
		
	return TRUE;
}

void CPaletexpView::OnEditBlues() 
{
//When user selects Edit Blue.

	if (m_ChangePalette(BLUE))
		{
		newPalette = TRUE;
		InvalidateRect(NULL, TRUE);
		}
	else
		AfxMessageBox("Error changing palette.", MB_OK, NULL);
}

void CPaletexpView::OnEditGreens() 
{
//When user selects Edit Green.

	if (m_ChangePalette(GREEN))
		{
		newPalette = TRUE;
		InvalidateRect(NULL, TRUE);
		}
	else
		AfxMessageBox("Error changing palette.", MB_OK, NULL);
	
}

void CPaletexpView::OnEditReds() 
{
//When user selects Edit Reds.

	if (m_ChangePalette(RED))
		{
		newPalette = TRUE;
		InvalidateRect(NULL, TRUE);
		}
	else
		AfxMessageBox("Error changing palette.", MB_OK, NULL);
}



⌨️ 快捷键说明

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