The EX20B Example -- A Double View Class SDI Static Splitter

In EX20B, we'll extend EX20A by defining a second view class and allowing a static splitter window to show the two views. (The H and CPP files are cloned from the original view class.) This time the splitter window works a little differently. Instead of starting off as a single pane, the splitter is initialized with two panes. The user can move the bar between the panes by dragging it with the mouse or by choosing the Window Split menu item.

The easiest way to generate a static splitter application is to let AppWizard generate a dynamic splitter application and then edit the generated CMainFrame::OnCreateClient function.

CHexView

The CHexView class was written to allow programmers to appreciate poetry. It is essentially the same code used for CStringView except for the OnDraw member function:

void CHexView::OnDraw(CDC* pDC) {     // hex dump of document strings     int        i, j, k, l, n, nHeight;     CString    outputLine, str;     CFont      font;     TEXTMETRIC tm;     CPoemDoc* pDoc = GetDocument();     font.CreateFont(-160, 80, 0, 0, 400, FALSE, FALSE, 0,         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,         DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");     CFont* pOldFont = pDC->SelectObject(&font);     pDC->GetTextMetrics(&tm);     nHeight = tm.tmHeight + tm.tmExternalLeading;     j = pDoc->m_stringArray.GetSize();     for (i = 0; i < j; i++) {         outputLine.Format("%02x   ", i);         l = pDoc->m_stringArray[i].GetLength();         for (k = 0; k < l; k++) {             n = pDoc->m_stringArray[i][k] & 0x00ff;             str.Format("%02x ", n);             outputLine += str;         }         pDC->TextOut(720, -i * nHeight - 720, outputLine);     }     pDC->SelectObject(pOldFont); }

This function displays a hexadecimal dump of all strings in the document's m_stringArray collection. Notice the use of the subscript operator to access individual characters in a CString object.

CMainFrame

As in EX20A, the EX20B application's main frame window class needs a splitter window data member and a prototype for an overridden OnCreateClient function. You can let AppWizard generate the code by specifying Use Split Window, as in EX20A. You won't have to modify the MainFrm.h file.

The implementation file, MainFrm.cpp, needs both view class headers (and the prerequisite document header), as shown here:

#include "PoemDoc.h" #include "StringView.h" #include "HexView.h"

AppWizard generates dynamic splitter code in the OnCreateClient function, so you'll have to do some editing if you want a static splitter. Instead of calling CSplitterWnd::Create, you'll call the CSplitterWnd::CreateStatic function, which is tailored for multiple view classes. The following calls to CSplitterWnd::CreateView attach the two view classes. As the second and third CreateStatic parameters (2, 1) dictate, this splitter window contains only two panes. The horizontal split is initially 100 device units from the top of the window. The top pane is the string view; the bottom pane is the hex dump view. The user can change the splitter bar position but the view configuration cannot be changed.

BOOL CMainFrame::OnCreateClient( LPCREATESTRUCT /*lpcs*/,     CCreateContext* pContext) {     VERIFY(m_wndSplitter.CreateStatic(this, 2, 1));     VERIFY(m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CStringView),                                     CSize(100, 100), pContext));     VERIFY(m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CHexView),                                     CSize(100, 100), pContext));     return TRUE; }

Testing the EX20B Application

When you start the EX20B application, the window should look like the one shown below. Notice the separate horizontal scroll bars for the two views.

click to view at full size.



Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net