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 3. Splitter Window<br />

WM_LBUTTONDOWN message handler. Instead of calling the message handler implemented by CSplitterWnd,<br />

we can call the default function implemented by class CWnd, which is the base class of CSplitterWnd. For a<br />

dynamic splitter window, the WM_LBUTTONDBLCLK message handler should not be overridden because after<br />

we disable the tracking, double clicking becomes the only way that can be used by the user to dynamically<br />

add or delete panes. For WM_MOUSEMOVE and WM_LBUTTONUP messages, we don’t need to modify their<br />

handlers because after message WM_LBUTTONDOWN is bypassed, the tracking will not happen anymore.<br />

Sample 3.5\Spw demonstrates how to implement splitter window that cannot be resized through<br />

tracking the split bar. It is based on sample 3.4\Spw.<br />

To let class MCSplitterWnd support both resizable and non-resizable split bars, a new Boolean type<br />

variable m_bResizable is declared in the class. Along with this variable a new member function<br />

MCSplitterWnd::SetResizable(…) is also declared, which can be called to set m_bResizable flag and<br />

indicate if tracking resize feature is currently supported. At the beginning variable<br />

MCSplitterWnd::m_bResizable is initialized to TRUE in the constructor. The following code fragment<br />

shows the modified class:<br />

class MCSplitterWnd : public CSplitterWnd<br />

{<br />

DECLARE_DYNCREATE(MCSplitterWnd)<br />

public:<br />

MCSplitterWnd();<br />

void SetResizable(BOOL bResizable){m_bResizable=bResizable;}<br />

virtual void DeleteRow(int);<br />

virtual void DeleteColumn(int);<br />

……<br />

protected:<br />

BOOL m_bResizable;<br />

}<br />

A WM_LBUTTONDOWN message handler is added to the application. This includes function declaration,<br />

adding ON_WM_LBUTTONDOWN message mapping macro, and the implementation of member function. Before<br />

adding message mapping, we need to make sure that DECLARE_MESSAGE_MAP macro is included in the class.<br />

This will enable massage mapping for the class. The following lists necessary steps for implementing the<br />

above message mapping:<br />

1) Declare an afx_msg type member function OnLButtonDown(…) in the class. This function is originally<br />

declared in class CWnd, here we must declare it again in order to override it:<br />

……<br />

……<br />

class MCSplitterWnd : public CSplitterWnd<br />

{<br />

protected:<br />

};<br />

afx_msg void OnLButtonDown(UINT, CPoint);<br />

DECLARE_MESSAGE_MAP()<br />

2) In the implementation file, add ON_WM_LBUTTONDOWN macro between BEGIN_MESSAGE_MAP and<br />

END_MESSAGE_MAP macros:<br />

BEGIN_MESSAGE_MAP(MCSplitterWnd, CSplitterWnd)<br />

//{{AFX_MSG_MAP(MCSplitterWnd)<br />

//}}AFX_MSG_MAP<br />

ON_WM_LBUTTONDOWN()<br />

END_MESSAGE_MAP()<br />

Macro ON_WM_LBUTTONDOWN maps message WM_LBUTTONDOWN to function OnLButtonDown(…).<br />

3) Implement the message handler as follows:<br />

void MCSplitterWnd::OnLButtonDown(UINT uFlags, CPoint point)<br />

{<br />

if(m_bResizable == TRUE)CSplitterWnd::OnLButtonDown(uFlags, point);<br />

67

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

Saved successfully!

Ooh no, something went wrong!