![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
And we also set up OnDraw(): void CScrollMDIView::OnDraw(CDC* pDC) { CScrollMDIDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); TEXTMETRIC tm; pDC->GetTextMetrics(&tm); for(int loop_index = 0; loop_index <= pDoc->number_lines; loop_index++){ pDC->TextOut(0, loop_index * tm.tmHeight, pDoc->text[loop_index]); } } As well as OnUpdate(): void CScrollMDIView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) { CScrollView::OnUpdate(pSender, lHint, pHint); CClientDC dc(this); TEXTMETRIC tm; CScrollMDIDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); dc.GetTextMetrics(&tm); dc.TextOut(0, (int) lHint * tm.tmHeight, pDoc->text[lHint]); } With this complete, were ready to implement scrolling. Implementing ScrollingTo be able to scroll through a document, we need to know its size on the screen. To store the documents size (in pixels), we add a new member to the document in ScrollMDIDoc.h called m_size. class CScrollMDIDoc : public CDocument { protected: // create from serialization only CScrollMDIDoc(); DECLARE_DYNCREATE(CScrollMDIDoc) . . . protected: CSize m_size; ⇐ . . .
This object is of the CSize class and has two members, cx and cy, to store the two dimensions of the document. We initialize this size to 800 × 800 pixels in the documents constructor as follows: CScrollMDIDoc::CScrollMDIDoc() { number_lines = 0; m_size = CSize(800, 800); ⇐ } You may have noticed that we made m_size a protected member of the CScrollMDIDoc class. We did so because m_size is internal data that no other part of the program should be able to change. However, other parts of the program will need to know the documents size, so we add an access function, GetDocumentSize(), that returns that size but does not allow any other part of the program to change m_size directly. class CScrollMDIDoc : public CDocument { protected: // create from serialization only CScrollMDIDoc(); DECLARE_DYNCREATE(CScrollMDIDoc) // Attributes public: int number_lines; CString text[1000]; // Operations public: CSize GetDocumentSize() {return m_size;} ⇐ Once weve given the document a size, we pass that size on to the CScrollView class. Setting the Documents SizeWe install the documents size in the view classs OnInitialUpdate() function, which AppWizard has already written for us: void CScrollMDIView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal; //TODO: calculate the total size of this view sizeTotal.cx = sizeTotal.cy = 100; SetScrollSizes(MM_TEXT, sizeTotal); } Here, the program sets the size of the document to 100 × 100 pixels as a default and then passes that size to CScrollView with the SetScrollSizes() so the scrollbars can be maintained appropriately. However, we can use the new GetDocumentSize() function to set the true size of the document this way: void CScrollMDIView::OnInitialUpdate() { CScrollView::OnInitialUpdate(); CSize sizeTotal; CScrollMDIDoc* pDoc = GetDocument(); ⇐
ASSERT_VALID(pDoc); ⇐ CSize DocumentSize = pDoc->GetDocumentSize(); ⇐ sizeTotal.cx = DocumentSize.cx; ⇐ sizeTotal.cy = DocumentSize.cy; ⇐ SetScrollSizes(MM_TEXT, sizeTotal);
The first argument in our call to SetScrollSizes() is MM_TEXT, a mapping mode, which indicates that we want to use pixels as our document measurement. The possible mapping modes appear in Table 2.1. Now weve set the document size and let CScrollView know what that scroll size is. Run the program, as shown in Figure 2.6, and enter text in a new document with several views open, as also shown in Figure 2.6. After youve entered text, you can scroll the views independently, as also shown in Figure 2.6. Weve not only let the user open several views into a document, weve let him or her move around in the document using those views. ScrollMDI is a success. ScrollMDIDoc.h and ScrollMDIDoc.cpp appear in Listing 2.3; ScrollMDIView.h and ScrollMDIView.cpp appear in Listing 2.4.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |