Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson - 5 :Drawing on the Screen

5.5 - Building the Scroll Application

In this section, you’ll build a sample program called Scroll to experiment with a scrolling window. When Scroll first runs, it displays five lines of text. Each time you click the window, five lines of text are added to the display. When you have more lines of text than fit in the window, a vertical scrollbar appears, enabling you to scroll to the parts of the documents that you can’t see.

As usual, building the application starts with AppWizard. Choose File, New, and select the Projects tab. Fill in the project name as Scroll and fill in an appropriate directory for the project files. Make sure that MFC AppWizard (exe) is selected. Click OK.

Complete the AppWizard steps, selecting the following options:

Step 1: Select Single Document.

Step 2: Use default settings

Step 3: Use default settings.

Step 4: Deselect all check boxes.

Step 5: Use default settings.

Step 6: Select CScrollView from the Base Class drop-down box, as in Figure 5.7.

The New Project Information dialog box should resemble Figure 5.7. Click OK to create the project.


FIG. 5.7 Create a scroll application with AppWizard.

This application generates very simple lines of text. You need to keep track only of the number of lines in the scrolling view at the moment. To do this, add a variable to the document class by following these steps:

1. In ClassView, expand the classes and right-click CScrollDoc.

2. Choose Add Member Variable from the shortcut menu.

3. Fill in int as the variable type.

4. Fill in m_NumLines as the variable declaration.

5. Select Public for the Access.

Variables associated with a document are initialized in OnNewDocument(), expand CScrollDoc and double-click OnNewDocument() to expand it. Replace the TODO comments with this line of code:

m_NumLines = 5;

To arrange for this variable to be saved with the document and restored when the document is loaded, you must serialize it . Edit CScrollDoc::Serialize() as shown in Listing 5.9.

Listing 5.9 CScrollDoc::Serialize()

void CScrollDoc::Serialize(CArchive& ar)

{

if (ar.IsStoring())

{

ar << m_NumLines;

}

else

{

ar >> m_NumLines;

}

}

Now all you need to do is use m_NumLines to draw the appropriate number of lines. Expand the view class, CMyScrollView, in ClassView and double-click OnDraw(). Edit it until it’s the same as Listing 5.10. This is very similar to the ShowFonts() code from the Paint1 application earlier in this Unit.

Listing 5.10 CMyScrollView::OnDraw()

void CMyScrollView::OnDraw(CDC* pDC)

{

CScrollDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// get the number of lines from the document

int numLines = pDoc->m_NumLines;

// Initialize a LOGFONT structure for the fonts.

LOGFONT logFont;

logFont.lfHeight = 24;

logFont.lfWidth = 0;

logFont.lfEscapement = 0;

logFont.lfOrientation = 0;

logFont.lfWeight = FW_NORMAL;

logFont.lfItalic = 0;

logFont.lfUnderline = 0;

logFont.lfStrikeOut = 0;

logFont.lfCharSet = ANSI_CHARSET;

logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;

logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;

logFont.lfQuality = PROOF_QUALITY;

logFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;

strcpy(logFont.lfFaceName, "Times New Roman");

// Create a new font and select it into the DC.

CFont* font = new CFont();

font->CreateFontIndirect(&logFont);

CFont* oldFont = pDC->SelectObject(font);

// Initialize the position of text in the window.

UINT position = 0;

// Create and display eight example lines.

for (int x=0; x<numLines; ++x)

{

// Create the string to display.

char s[25];

wsprintf(s, "This is line #%d", x+1);

// Print text with the new font.

pDC->TextOut(20, position, s);

position += logFont.lfHeight;

}

// Restore the old font to the DC, and

// delete the font the program created.

pDC->SelectObject(oldFont);

delete font;

}

Build and run the Scroll application. You will see a display similar to that in Figure 5.8. No scrollbars appear because all the lines fit in the window.


FIG. 5.8 At first, the scroll application displays five lines of text and no scrollbars.

5.5.1 Adding Code to Increase Lines

To increase the number of lines whenever users click the window, you need to add a message handler to handle left mouse clicks and then write the code for the handler. Right-click CMyScrollView in ClassView and choose Add Windows Message Handler. Double-click WM_LBUTTONDOWN to add a handler and click the Edit Existing button to change the code. Listing 5.11 shows the completed handler. It simply increases the number of lines and calls Invalidate() to force a redraw. Like so many message handlers, it finishes by passing the work on to the base class version of this function.

Listing 5.11 CMyScrollView::OnLButtonDown()

void CMyScrollView::OnLButtonDown(UINT nFlags, CPoint point)

{

CScrollDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// Increase number of lines to display.

pDoc->m_NumLines += 5;

// Redraw the window.

Invalidate();

CScrollView::OnLButtonDown(nFlags, point);

}

5.5.2 Adding Code to Decrease Lines

So that you can watch scrollbars disappear as well as appear, why not implement a way for users to decrease the number of lines in the window? If left-clicking increases the number of lines, it makes sense that right-clicking would decrease it. Add a handler for WM_RBUTTONDOWN just as you did for WM_LBUTTONDOWN, and edit it until it’s just like Listing 5.12. This function is a little more complicated because it ensures that the number of lines is never negative.

Listing 5.12 CMyScrollView::OnRButtonDown()

void CMyScrollView::OnRButtonDown(UINT nFlags, CPoint point)

{

CScrollDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// Decrease number of lines to display.

pDoc->m_NumLines -= 5;

if (pDoc->m_NumLines < 0)

{

pDoc->m_NumLines = 0;

}

// Redraw the window.

Invalidate();

CScrollView::OnRButtonDown(nFlags, point);

}

If you build and run Scroll now and click the window, you can increase the number of lines, but scrollbars don’t appear. You need to add some lines to OnDraw() to make that happen. Before you do, review the way that scrollbars work. You can click three places on a vertical scrollbar: the thumb (some people call it the elevator), above the thumb, or below it. Clicking the thumb does nothing, but you can click and hold to drag it up or down. Clicking above it moves you one page (screenful) up within the data. Clicking below it moves you one page down. What’s more, the size of the thumb is a visual representation of the size of a page in proportion to the entire document. Clicking the up arrow at the top of the scrollbar moves you up one line in the document; clicking the down arrow at the bottom moves you down one line.

What all this means is that the code that draws the scrollbar and handles the clicks needs to know the size of the entire document, the page size, and the line size. You don’t have to write code to draw scrollbars or to handle clicks on the scrollbar, but you do have to pass along some information about the size of the document and the current view. The lines of code you need to add to OnDraw() are in Listing 5.13; add them after the for loop and before the old font is selected back into the DC.

Listing 5.13 Lines to Add to OnDraw()

// Calculate the document size.

CSize docSize(100, numLines*logFont.lfHeight);

// Calculate the page size.

CRect rect;

GetClientRect(&rect);

CSize pageSize(rect.right, rect.bottom);

// Calculate the line size.

CSize lineSize(0, logFont.lfHeight);

// Adjust the scrollers.

SetScrollSizes(MM_TEXT, docSize, pageSize, lineSize);

This new code must determine the document, page, and line sizes. The document size is the width and height of the screen area that could hold the entire document. This is calculated by using the number of lines in the entire document and the height of a line. (CSize is an MFC class created especially for storing the widths and heights of objects.) The page size is simply the size of the client rectangle of this view, and the line size is the height of the font. By setting the horizontal component of the line size to 0, you prevent horizontal scrolling.

These three sizes must be passed along to implement scrolling. Simply call SetScrollSizes(), which takes the mapping mode, document size, page size, and line size. MFC will set the scrollbars properly for any document and handle user interaction with the scrollbars.

Build and run Scroll again and generate some more lines. You should see a scrollbar like the one in Figure 5.9. Add even more lines and you will see the thumb shrink as the document size grows. Finally, resize the application horizontally so that the text won’t all fit. Notice how no horizontal scrollbars appear, because you set the horizontal line size to 0.


FIG. 5.9 After displaying more lines than fit in the window, the vertical scrollbar appears.

Next>>
 
© Dewsoft Overseas