11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10. Bitmap<br />

Sample<br />

Sample 10.2\GDI demonstrates how to extract color table from DIB and convert it to DDB. It is based<br />

on sample 10.1-2\GDI.<br />

Because we need to implement logical palette, first a variable and a function are added to class<br />

CGDIDoc:<br />

……<br />

……<br />

……<br />

……<br />

class CGDIDoc : public CDocument<br />

{<br />

protected:<br />

public:<br />

}<br />

CPalette m_palDraw;<br />

CPalette *GetPalette(){return &m_palDraw;}<br />

Variable m_palDraw is added for creating logical palette and function GetPalette() is used to access it<br />

outside class CGDIDoc.<br />

In class CGDIView, some new variables are added for bitmap drawing:<br />

……<br />

class CGDIView : public CScrollView<br />

{<br />

protected:<br />

CDC m_dcMem;<br />

CPalette *m_pPalMemOld;<br />

CBitmap *m_pBmpMemOld;<br />

BITMAP m_bmInfo;<br />

CGDIView();<br />

DECLARE_DYNCREATE(CGDIView)<br />

}<br />

Variable m_dcMem will be used to implement memory DC at the initialization stage of the client<br />

window. It will be used later for drawing bitmap. By implementing memory DC this way, we don’t have to<br />

create it every time. Also, we will select the bitmap and palette into the memory DC after they are avialabe,<br />

and selet them out of the DC when the window is being destroyed. For this purpose, two pointers<br />

m_pPalMemOld and m_pBmpMemOld are declared, they will be used to select the palette and bitmap out of DC.<br />

Variable m_bmInfo is used to store the information of bitmap.<br />

The best place to create bitmap and palette is in CGDIView::OnInitialUpdate(). First, we must locate<br />

and load the bitmap resource:<br />

void CGDIView::OnInitialUpdate()<br />

{<br />

CClientDC dc(this);<br />

CGDIDoc *pDoc;<br />

CBitmap *pBmp;<br />

CPalette *pPalDraw;<br />

LPBITMAPINFO lpBi;<br />

LPLOGPALETTE lpLogPal;<br />

CPalette *pPalOld;<br />

int nSizeCT;<br />

int i;<br />

HBITMAP hBmp;<br />

HRSRC hRes;<br />

HGLOBAL hData;<br />

CScrollView::OnInitialUpdate();<br />

pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

if<br />

289

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!