1.Create Palette
struct
{
LOGPALETTE lp;
PALETTEENTRY pe[31];
}plp;
LOGPALETTE *plg=(LOGPALETTE*) &plp;//why is it converted?
plg->palVersion = 0x300;
plg->palNumEntries = 32;
for(int i=0;i<32;i++)
{
plg->palPalEntry[i].peRed = i * 8;
plg->palPalEntry[i].peGreen = 0;
plg->palPalEntry[i].peBlue = 0;
plg->palPalEntry[i].peFlags = 0;
}
CPalette palette;
palette.CreatePalette(plg);
//palette.CreateHalftonePalette(plg);
//palette.Attach(::CreateHalftonePalette(NULL));
2.Initialize Palette and Draw with it
void CMyView::OnPaint()
{
CPaintDC dc(this);
CPalette *pOldPalette=dc.SelectPalette(&m_nPalette,FALSE);
dc.RealizePalette();
CPen pen1(PS_SOLID,16,RGB(222,104,245);
CPen pen2(PS_SOLID,16,PALETTERGB(12,204,120));
CPen pen3(PS_SOLID,16,PALETTEINDEX(3));
dc.MoveTo(0,0);
CPen *pOldPen=dc.SelectObject(pen1);
dc.LineTo (300, 0); // Nearest static color
dc.SelectObject (&pen2);
dc.LineTo (150, 200); // Nearest static or palette color
dc.SelectObject (&pen3);
dc.LineTo (0, 0); // Exact palette color
dc.SelectObject (pOldPen);
dc.SelectPalette(pOldPalette,FALSE);
}
3
ON_WM_QUERYNEWPALETTE()
..............
BOOL CMainWindow::OnQueryNewPalette()
{
CPaintDC dc(this);
CPalette *pOldPalette=dc.SelectPalette(&m_nPalette,FALSE);
UINT nCount;
if(nCount=dc.RealizePalette())
Invalidate();
dc.SelectPalette(pOldPalette,FALSE);
return nCount;
}
ON_WM_PALETTECHANGE()
................
void CMainWindow::OnPaletteChange(CWnd* pFocusWnd)
{
if(m_pFocusWnd!=this)
{
CPaintDC dc(this);
CPalette *pOldPalette=dc.SelectPalette(&m_nPalette,FALSE);
if(dc.RealizePalette())
Invalidate();
dc.SelectPalette(pOldPalette,FALSE);
}
}
4.Handling Palette Messages in Document/View Applications
put the OnQueryNewPalette and OnPaletteChanged handlers in the view class, but that won't work because views don't receive palette messages—only top-level windows do.
BOOL CMainWnd::OnQueryNewPalette()
{
CDocument *pDoc=GetActiveDocument();
if(pDoc!=NULL)
pDoc->UpdateAllView(NULL);
return TRUE;
}
BOOL CMainWnd::OnPaletteChange(CWnd* pFocusWnd)
{
if(m_pFocusWnd!=this)
{
CDocument *pDoc=GetActiveDocument();
if(pDoc!=NULL)
pDoc->UpdateAllView(NULL);
}
}
5.Message Macro
ON_WM_QUERYNEWPALETTE() BOOL OnQuerryNewPalette()
ON_WM_PALETTECHANGE() void OnPaletteChange(CWnd* pFocusWnd)
ON_WM_ERASEBKGRD() BOOL OnEraseBkgnd()