In Ex18b, we'll extend Ex18a 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 command.
The easiest way to generate a static splitter application is to let the MFC Application Wizard generate a dynamic splitter application and then edit the generated CMainFrame::OnCreateClient function.
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.
As in Ex18a, the Ex18b application's main frame window class needs a splitter window data member and a prototype for an overridden OnCreateClient function. You can let the MFC Application Wizard generate the code by specifying Split Window, as in Ex18a. You don't have to modify the
The implementation file,
#include "PoemDoc.h" #include "StringView.h" #include "HexView.h"
The MFC Application Wizard 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 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 not the view configuration.
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; }
When you start the Ex18b application, the window should look like the one shown here. Notice the separate horizontal scroll bars for the two views.