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

}<br />

}<br />

}<br />

RecalcLayout();<br />

Since the maximum number of rows that can be implemented in a dynamic split window is 2, we will<br />

call the default implementation of this function (the corresponding function of the base class) if the number<br />

of rows is not 2. Otherwise, we first obtain the size of upper pane (pane 0), enlarge its vertical size, and set<br />

its current size. Then the current size of lower pane is reduced, if its ideal size is smaller than its minimum<br />

size after change, we simply call function CSplitterWnd::DeleteRow(…) to delete this row. If the panes are<br />

resized instead of being deleted, we call function CSplitterWnd::RecalcLayout() to update the new<br />

layout.<br />

Function MCSplitterWnd::DeleteColumn(int colDelete) is implemented in the same way, except<br />

that here we call all the functions dealing with column instead of row.<br />

Using the New Class<br />

Using this new class is simple, we just need to include the header file containing class MCSplitterWnd<br />

in file “MainFrm.h”, then use it to declare variable m_wndSpw in class CMainFrame as follows:<br />

……<br />

class CMainFrame : public CFrameWnd<br />

{<br />

protected:<br />

CMainFrame();<br />

DECLARE_DYNCREATE(CMainFrame)<br />

MCSplitterWnd m_wndSp;<br />

}<br />

After these changes, by compiling and executing the application again, we will see that the split bar<br />

behaves differently.<br />

3.4 Customizing the Default Appearance<br />

Drawing Functions<br />

Class CSplitterWnd has two member functions that can be overridden to customize the appearance of<br />

split bar, split box, split border, and split tracker. The functions are CSplitterWnd::OnDrawSplitter(…)<br />

and CSplitter::OnInvertTracker(…) respectively, which have the following formats:<br />

void CSplitterWnd::OnDrawSplitter(CDC *pDC, ESplitType nType, const CRect &rect);<br />

void CSplitterWnd::OnInvertTracker(const CRect &rect);<br />

Function CSplitterWnd::OnDrawSplitter(…) is called when either the split bar, split box or split<br />

border needs to be painted. It has three parameters, the first of which is a pointer to the target device DC,<br />

which will be used to draw the objects. The second parameter is an enumerate type, which indicates what<br />

type of object is being drawn. This parameter could be either CSplitterWnd::splitBox, CSplitterWnd::<br />

splitBar, or CSplitterWnd::splitBorder, which indicates different splitter window objects. The third<br />

parameter specifies a rectangle region within which the object will be drawn.<br />

Function CSplitterWnd::OnInvertTracker(…) is called when the user clicks the mouse on the split<br />

bar and drags it to resize the panes contained in the splitter window. In this case, a tracker will appear on<br />

the screen and move with the mouse. By default, the tracker is a grayed line. By overriding this function,<br />

we could let the tracker have a different appearance.<br />

64

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

Saved successfully!

Ooh no, something went wrong!