www.pudn.com > Demo_Palette.rar > Demo_PaletteView.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
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
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_EDIT1, m_nRed);
DDX_Text(pDX, IDC_EDIT2, m_nGreen);
DDX_Text(pDX, IDC_EDIT3, 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();
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);
}
}
//chuangjiantiaoseban
PLOGPALETTE 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;
}
//创建85种红,绿,蓝颜色
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].peRed=(BYTE)i*3;
lp->palPalEntry[i+171].peFlags=0;
}
m_pPalette=new CPalette;
m_pPalette->CreatePalette(lp);
GlobalUnlock(hPal);
delete m_pPalette; //释放内存
}
/////////////////////////////////////////////////////////////////////////////
// CDemo_PaletteView printing
BOOL CDemo_PaletteView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDemo_PaletteView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDemo_PaletteView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CDemo_PaletteView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// 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::OnColor()
{
// TODO: Add your control notification handler code here
int m_nFlag =1;
CColorDialog m_Dlg(0,0,NULL);
if(m_Dlg.DoModal()==IDOK)
{
m_color=m_Dlg.GetColor();
OnView();
}
}
void CDemo_PaletteView::OnPalette()
{
// TODO: Add your control notification handler code here
m_nFlag=0;
m_color=RGB(m_nRed,m_nGreen,m_nBlue);
OnView();
}
void CDemo_PaletteView::OnView()
{
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);
}