11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7. Common Dialog Boxes<br />

……<br />

}<br />

}<br />

m_pColorDlg=NULL;<br />

We need to destroy the windows by ourselves if the user exits the application without first closing the<br />

color dialog box. We can override a member function OnCloseDocument(), which will be called when the<br />

document is about to be closed. This function can be easily added through using the Class Wizard. The<br />

following shows how it is implemented in the sample:<br />

void CCDBDoc::OnCloseDocument()<br />

{<br />

if(m_pColorDlg != NULL)<br />

{<br />

if(m_pColorDlg->m_hWnd != NULL)m_pColorDlg->Detach();<br />

delete m_pColorDlg;<br />

}<br />

if(m_pColorDmDlg != NULL)delete m_pColorDmDlg;<br />

m_pColorDmDlg=NULL;<br />

m_pColorDlg=NULL;<br />

}<br />

CDocument::OnCloseDocument();<br />

Again, function CWnd::Detach() is called before the buffers are released.<br />

Applying Selected Color Instantly<br />

To make the sample more practical, another feature is added to it: when the user picks up a color, the<br />

client window of the application will be painted with that color instantly. To implement this, a new<br />

COLORREF type variable m_colorCur is added to class CDBDoc, which will be used to store the current color<br />

of the client window:<br />

……<br />

……<br />

class CCDBDoc : public CDocument<br />

{<br />

protected:<br />

}<br />

COLORREF m_colorCur;<br />

The color is initialized in the constructor as follows:<br />

……<br />

CCDBDoc::CCDBDoc()<br />

{<br />

}<br />

m_colorCur=RGB(0, 255, 255);<br />

Member function CCDBDoc::SetCurrentColor(…) and CCDBDoc::GetCurrentColor() are added to let<br />

us access variable m_colorCur outside the document:<br />

……<br />

……<br />

class CCDBDoc : public CDocument<br />

{<br />

public:<br />

void SetCurrentColor(COLORREF);<br />

COLORREF GetCurrentColor(){return m_colorCur;}<br />

}<br />

Function CCDBDoc::GetCurrentColor(…) is implemented as an inline function, and function<br />

CCDBDoc::SetCurrentColor(…) is implemented as follows:<br />

197

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

Saved successfully!

Ooh no, something went wrong!